Redirection to the page preceding a connection

Hi,
In my project, certain steps require that the user is logged in to be able to continue. It is then first redirected to the connection page (if it is not) before being able to continue. Is it possible that he is redirected to the page where he was before he was forced to log in (to avoid him repeating all the ones he has already done previously)?
Thanks

A little confused by the question to be honest, but basically.

If using database type login authentication in your app with security providers and page restricts then your user would first start off at a login page which would verify their credentials and then you would redirect them to your secure page using the Wappler built in security.identity normally.
If the user is not logged in and goes to your secure URL manually then their security.identity should interact with the page security added and redirect them back to your login page where they can enter their credentials so the security provider can give them the security.identity allowing them to see your page.

If you are talking about OAuth API security via google or facebook then it all works quite differently for certain steps in the process.

Sorry for my bad english, I am french speaking.
Let’s just say that to validate his order, the user must be logged in (or create an account). How to redirect him to the validation page of his order after he has connected. But this example is valid for any page (no restriction of access to these pages)

Ok I think I get it now, so like an online shop where the user can add items to their cart without registering or logging in first. Then if they decide to verify the order, they need to create an account or login and for their same items placed in their shopping cart to be added to their profile.

Best option if some parts of your process require login and other parts don’t is to make them create an account or login to their account first, if that is not possible or desirable.

Second option is to use a unique session identifier and place all their cart items under their session, when they want to check out and need to register or login then update their session information stored in the temporary table under their user identifier that your shopping cart system uses.

@ben would possibly be better suited to answer though as he has done shopping cart type stuff with Wappler.

The example of e commerce was just to illustrate the case. I want to set up the functionality on all pages where the user will decide to connect (or would be forced to do so). Is it possible to put the current or previous url in a variable that could be reused to redirect to the previous page?

Would I be right that you’re looking for this solution?

Visitor is on a page in your site. They go to another page which is restricted (requires to be logged in) but, as they are not logged in they are bounced to the login page. But, after then entering their login details, you want them to redirect back to the page they attempted to view before the logging in?

So, the pages need to store the current URL somewhere, then bounce to the login page, then redirect to that stored URL after the logging in is successful.

Is that correct?

Yes, I need to store the current url in a variable and how to reuse it after login so I can be
redirect to this previous page

This thread may be helpful.

This assumes that we know the page to which we want to redirect the user. In my case, this can be any page …

I think this can be to any page - whatever page the user intended to go to before having to log in. (But I may have misunderstood the question.)

If I understood correctly, in this example we know in advance to which page the user should be redirected. In my case, we don’t know

To quote from one of the comments on the thread:

… you need them to be redirected to the page they previously requested which could be one of many different pages?

I thought this was the issue you wanted to find a solution to.

Absolutely. When is the url of the previous page identified and retrieved for reuse?
In this example, I understand that he wants to redirect to the page “web-user.php”, in my case, I do not know in advance to which page he should be redirected hence the need to retrieve his URL.

This is explained in the thread:

1 Like

@Djibril All you need to do is set this up in the Security Provider window. See here.

So in your case, on the ‘web-user.php’ page you will have security provider with If User Not Logged In, Go To field set as ‘login.php?return=web-user.php’ (login.php being your login page).

Then if a user hits your web-user.php and is not signed in, they will be re-directed to the login page and then finally back to the web-user page once signed in. Hope that makes sense.

2 Likes

This should really be placed in the How To section

2 Likes

Hi TomD, thanks a lot. This is a solution indeed.

1 Like

Hi,
Here is a solution I founded to redirect to the previous page (like the back button of a browser).
Sorry in advance for my mistakes, I am not a developer, just an enthusiast who learns. I am waiting for your comments!

Step 1
I created 2 pages containing only the “main”, “row” and “col” tags. I named them “first_page.php” and “second_page.php”.


Step 2
Insert in the first page, the custom php script which allows to recover the URL of the current page.

The script is bellow

<?php $uri = $_SERVER['REQUEST_URI']; $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $query = $_SERVER['QUERY_STRING']; ?>

Step 3
Insert the “Browser” component from the “App structure” panel. Select “App” in the “App Structure” pane, click on “+”. In the new window, select “Component”, then click on “Browser”


Step 4
Insert the “Session Storage Manager” component from the “App structure” panel.
Select “App”, then click on “+”. In the new window, select “State Management”, then click on “Session Storage Manager”.

Step 5
With “Session Storage Manager” selected in “App Structure”, click on “Define Session Storage Items”

Step 6
In the new window, right click on “Session Stoarage” and select “Add variable”

Step 7
Rename the variable “previouspage” and click on “Select”

Step 8
We will use a form with an “imput” tag to retrieve the URL of the current page
Select the “column” tag and insert a “form” tag inside from the “App structure” panel

Step 9
The “form” tag still selected, set method to “post”

Step 10
The “form” tag still selected, insert in “action” the path of the file to which we should be redirected after submission of the form

Step 11
Insert an “imput” tag inside. Select the form, click on “+” in “App Structure”. In the new window, select “insert child”, then “form”, then select “Text input”.

Step 12
Set “imput” tag type to “hidden”

Step 13
Insert the following custom php script for the static value of the “imput” tag : <?php echo $url; ?>


Step 14
Insert a button after the “imput” tag

Step 15
For the functionality of retrieving the URL of the current page, let’s select the “form” tag and define a dynamic event

Step 16
In the new window, select “form” and then “submit” event

Step 17
Click on the thunder in front of the “submit” event to define the properties

Step 18
In the new window, select “set” under the “Session Storage Manager” pane, then “+”, finally the session variable that we have defined before

Step 19
Then click on the thunder in front of “value”

Step 20
In the new window, select “value” under the “form” tag and click on “select”

Step 21
Back on the previous window, define the redirection property.
Select “Go To” in the “Browser” pane, then click on “+”, select the new action that has just been added and finally define the path of the desired page in the “Go To Properties” section.
That’s it. The first page is ready. Do not forget to save it before going to the second page.

Step 22
On the second page, we will add the “Browser” component by selecting “App” in the “App structure” pane, click on “+”, select “component”, then “Browser”.

Step 23
While keeping “App” selected in the “App structure” pane, let’s add “Session Storage Manager” to the page to retrieve the URL of the previous page.
Click on “+”, and in the new window, select “State Management” and click on “Session Storage Manager”

Step 24
Make sure that the ID of “Session Storage Manager” is identical to that of the previous page

Step 25
Let’s add the button which will be used to redirect to the previous page. Select the “input” tag, in the “App Structure” pane, click on “+”, then “insert child”.
In the new window, click on “Button”

Step 26
Let’s define the redirect functionality. Select “Button” and “+” in front of “Dynamic event” under the “Properties” tab

Step 27
In the new window, select “Mouse”, then “Click”

Step 28
Click on the thunder placed in front of the new action which is under the section “Dynamic event”

Step 29
In the new window, select “Go To” under the “Browser” tab, then click on “+”, then select the new action that has just been created and finally on the lightning in the “Go To properties” section

Step 30
In the new window, click on the “previouspage” variable under the “Session Storage Manager” tab then on “select”

Step31
Back to the previous window, click on “select”, save your page and that’s it.