Login New User After Registration

I am new with Wappler, so I am just learning the basics; however, I am stuck on something that seems quite simple. I have followed the tutorial for Register New User and Auto Login on Success, but the tutorial seems pretty out of date and hard to follow for a new user of Wappler. Can someone provide some instructions on how you would set up the auto login part after successful registration?

This is where I am right now, I have a registration form setup and when the user clicks the button to submit the form the user is entered into the database without any issue. I have a dynamic event setup on the button to go to the dashboard page upon success. The dashboard page is secured, so a non-logged in user would be redirected to the login page. However, when a new user submits the form they are taken right away to the login page. I assume because the dashboard does not see them as a logged in user. Here are my server actions:

I have attempted to follow the tutorial as close as possible, but something is definitely missing. When I look at the debug, it appears that I am seeing a ‘Status Code 302 / Found’ error. It just looks like my new user is being treated as a logged out user. When the new user is transferred to the login page, if the user’s info is entered there, the login works fine.

Any ideas or does anyone have some better instructions for setting this up correctly? Thanks for your help!

This is what mine looks like

image

Notice that the insert step does not have an output.

Take a look at the wappler YouTube channel. There are a few video tutorials covering login etc including a full webinar

Hello,

I believe the logic of wappler make sense although it is may be not what you wanted. If I do understand your issue, you are trying to use one form to register the user or login the user.
If that is the case, I believe you should try the conditional step that will check if the user exit then login. Otherwise, register.
Beside that, if the dashboard board is already protected then it will definitely send the user to login page because the insert form is not a login form, I believe the server will have a different behavior with that.

Hello @Bradley_Mitchell
you should structure your server connect structure based on your exact needs. As @mamdi122003 indicates.
You should first check if the user already exists.
If yes you log him in.
If not then you first subscribe him and then log him in.

Thank you!

If you want an application that includes the login procedure, have a look at https://github.com/Wappler/wCart.

Feedback shows that new Wapplers learn a lot when they have installed it.

@ben - Thanks so much for sharing your steps. I added the output on insert when playing around to try to figure it out, but didn’t have it initially. I removed it, but still didn’t seem to help. I think I’ve figured out that my issue is likely in the query step. Can you share any insight as to what I might be doing wrong there, if anything? Here is what I have in the query builder.

Then I set up a condition to find a userID with the same id as the inserted user.

I thought that this is what was needed, but I am not completely sure that my step is finding the user. The insert into the database is successful, so I am thinking that when it then goes to the dashboard page it is showing the user as logged out and thus transfers them to the login page instead of actually being logged in. Is there a way to tell what data the query that I have created is actually finding, if anything? I would love to be able to see if it is pulling the right data, or any data, somehow, but am unsure of how to do this other than to run the app and see what happens.

Thanks again for your help!

@mamdi122003 Thanks for your help! I probably didn’t explain myself fully. I have a login page that works great for logging in registered users, so no issues there. What I am attempting to do here is to create a separate page for user registration. Upon successful registration, I want the user to be automatically logged in and taken to the main dashboard page.

So, what I have attempted to do is to insert the new user into the database, then query to find that new user and log them in. I think I’ve messed something up with my query step. I have a theory my query step is not actually finding the new user just created and thus no user is logged in, which causes my dashboard page to redirect to the login page since there is no authorized user logged in.

Any ideas on how to best create a query that will find the newly created user? Thanks so much for your help and ideas.

I am not sure yet why - We have to see your input value. However, one of the main reason I can see sometimes when this happen is your password salt being different from your login salt password .
If that happen the system will reject your demand to the login page for restriction. Try to see if all the password salt match in your database.
In the other hand don’t forget the conditional step @t11 and I told you about. You have to include the validation to the step too.
But check first the password salt to see if there are all the same.

What would the validation step look like? I have validation on the form itself to check password strength, etc; however, I don’t have any validation in my steps, should I? Would it be a database validation to check to see if the user exists in the database? I’m not quite sure how I would use this or set it up, yet.

I have a conditional step set up on the query that is supposed to find a user with the userID equal to the inserted user’s id. I am wondering if I have an issue with the id? I create the id in the insert step using a hash to try to create a unique value. Then I have set the userID as the identity value in the security provider.

Could there be an issue there? It does insert just fine, but am wondering if I might have messed something up there. Thanks!

Use @ben example at the top to see how your server connect can work. For the validation to see if user exists check this link.

Check if a Database Record Already Exists Before Inserting a New Entry

For the condition step:
Go under core action and add conditions:

@mamdi122003 - thanks so much for your thorough help in getting this step completed. I have successfully added a validation step as you suggested. This is working great and now checks to see if the email address and username are unique values, since I want both to be unique for the app that I am working on. So, all appears to be working fine there.

I am still stuck with the issue of not being logged in, though, after successful user registration. I’ve reviewed all that I can find, but can’t seem to put my finger on exactly what is causing the issue. As I mentioned, I suspect that my query might be off and isn’t actually finding the new user that was created and thus no user is logged in, but when the button is clicked it successfully inserts a record, so attempts to go to the secured dashboard page, but then instantly kicks over to the login page since it doesn’t recognized an authorized user.

Here are the steps that I have so far based on all of your feedback and what I have seen through other tutorials:

After validation, I do an insert into the database and that seems to work as the new record is created successfully, but maybe I am missing something here?

After the insert, I do the query where I am trying to locate the user that was just created in the database through the following database query step:

Then after this step, I added a repeat to get the username, password and created timestamp from the query step, then my security provider and then the login step.

Can you see what I might be missing? I feel like it is an obvious omission and likely something pretty simple since I am just learning wappler.

I have checked my password hash and salt and that seems to be okay. I am using sha512 encryption and the created timestamp as the salt (maybe there is a better way to do this too, but seems to work). Thanks for your help!

Hello Bradley,
I see that you connected the users.userID = {{insert1.identity}}
I always use the security provider identity to identify the users in my database:
try to users.usersID = {{security1.identity}} because security1 is your security provider name.
I am sure you did already but check again if the hash salt of your password in your security login is identical to the hash password when you were inserting the data.

You are not performing the second (get user details) query at an optimal time
You can use your registration credentials to login then you can reference the user via the security provider identity

your form passes $_POST.username and $_POST.username

in the login stage login using $_POST.username as the login name and $_POST.username.sha512(your-salt-here) as the password

This should log you in.

You can then retrieve the ID of the current user via security1.identity and use this to get the logged in users details afterwards with a data query (your query1)
security1.identity is not set until after the login stage

Aha! I finally got it to work correctly. For anyone who is searching for a way to do this, here are the steps that I did that seems to work for me with some explanation below:

Step 1: Database Connection - Create your connection to the database.
Step 2: Validate data - this step is to check to be sure that the user you are about to insert into the database doesn’t already exist to reduce user duplication. For this step, I added a check to see if the username was unique and the email address was unique following these steps: Check if a Database Record Already Exists Before Inserting a New Entry
Step 3: Database Insert - insert the fields from your form into the database.
Step 4: Database Query - For this step, I selected the users table and then added a condition step that looked for the users.username equal to my forms username field ( {{$_POST.username}} ).
Step 5: Security Provider - I added my security provider that I setup previously.
Step 6: Security Login - I used the {{$_POST.username}} as the username, {{$_POST.confirmpassword.sha512(enteryoursalt)}} as the password, and then selected my security provider as the security step.

Then in the design panel, I added the browser component and on the form added a dynamic event and selected Server Connect -> Success and then for the action I selected Got To under the browser component and then selected my dashboard or secured page.

This may not be a perfect way to do it, but it does work for me and seems to include the major steps necessary to insert the new user into the database if they don’t already exist and then go to the dashboard page upon successful registration.

Thank so much for everyone’s advice. It really helped me think through this problem and get this working on my end. I appreciate all of the responses and support!

6 Likes

Yes that is how to do it as I said previously. I again re-iterate this technique is all available (and a lot more) via the Wappler YouTube channel, in this case webinar 3. I cannot implore users enough to check out the Wappler YouTube channel, it has so much info that is repeatedly asked on this forum

1 Like