c-andrews's avatar

Forcing page flow

Hi, I'm using Laravel, Inertia and React. For my front end I would like to make a strict user flow, so users can only view page B after they have viewed page A, so that users cant skip past the important bits. Basically the experience I am building needs the users location.

These users do not have accounts and it is open to the masses.

How can i prevent users from going straight to a particular route e.g. myroute/two if they haven't been to myroute/one?

Thanks in advance

0 likes
7 replies
tykus's avatar
tykus
Best Answer
Level 104

Use the Session. Prevent viewing B if there is not some state in the Session that should be put there whenever visiting A

1 like
kokoshneta's avatar

The easiest way would be to save a value in your user’s session data (or in a cookie), and then check in your controller whether the value is present and satisfies the required minimum of pages you’ve viewed.

You could either save that value when loading the page (i.e., when you load page 1, you save a value that corresponds to “user has opened page 1”), or when the user clicks on a “Next” button at the end of the page, if you want to ensure that the user has not only opened the page, but actually scrolled to the end of it as well, the way Google does on their terms and conditions popovers.

Personally, I would implement the check through an enum class that you do bitwise comparisons on to check, something like this:

enum PageStatus : int {
	case page1 	= 1 << 0; // 1
	case page2	= 1 << 1; // 2
	case page3	= 1 << 2; // 4
	case page4	= 1 << 3; // 8
	case page5	= 1 << 4; // 16

	public function satisfies($value) {
		return ($value + 1) & $this->value == $this->value;
	}
}

Then in your controller, you’d load the status from the session/cookie, and then check whether the current page’s status is satisfied by that.

2 likes
c-andrews's avatar

Thanks I've added a middleware function to check the session contains a location id and if not returns to the "/". I've then grouped the routes using the middleware. Seems to be working so far.

Thanks

tykus's avatar

@c-andrews mark the thread closed in that case 😜

c-andrews's avatar

@tykus Thanks, I had a bit of fun with the session between the api routes and the web routes. Can I use the same session across both route controllers or is it best to avoid using the session on the api routes?

tykus's avatar

@c-andrews there is no Session in the API routes by default. This is an Inertia app with Inertia pages; what are you doing with API routes in the context of Page A, Page B etc.

c-andrews's avatar

@tykus So my page routes for Page A and Page B were in my web.php routes, however I was using the api.php routes to post data too. I have now moved these routes into the web.php and its now working.

Please or to participate in this conversation.