Back

Laravel 5.3 Auth - Possibility to register and log in but also to continue as guest


nameless posted 6 months ago

I'm currently developing a website with Laravel, where users can register and afterwards login in order to do something on my page.

Now, I'm planning to introduce the possibility to proceed as a guest, so they can "Login as Guest" and then proceed like a normal user (but without the possibility to permanently save information they enter there). Also, they should be always able to change this Guest account to a permanent account by entering their mail-address as well as a password. I thought about simply creating a new user in db when clicking on login as guest, like a normal registered user and afterwards deleting it, but I'm not sure if that is possible to easy... Is there any way to "easily" do this or do you have any ideas on how to do it?

The registered user should be able to do lots of things like changing their addresses, informations, submit a ticket, sign up for newsletter, etc..., guests will only be able to put something in their shopping cart and buy it (and while buying they should be able to create a account as well..)

Also I know that I might need to implement a custom guard to authentificate a guest user (because normally they will need a password, I only want them to enter their mail adress to "log in". But ofcourse I need a session key to be generated etc., so this is the guards part then... But what do I need to do to create such a custom guard that fullfills my issues? I found the laravel doc for this (https://laravel.com/docs/5.3/authentication#adding-custom-guards), but can't really combine it with my problem....

PS: I'm using Laratrust for different roles within the page, so with that I might be able to show different menus or other information for a guest user after logging in with their mail adress. But still, the question is how to do it...

Thx for any help!

Edit: After thinking about it again, I want to get it a bit clearer. I basically plan to change the login page and add a button like "proceed as guest" and define a route like guestLogin for it. This route would point to a controller that somehow logs in using a (propably selfmade) auth. When clicking the button, a entry is added into the users table in my database, with only an idea and a remember token. Then, the guest user can place products in his shopping cart and can order something. In the order process, he needs to enter his personal information like his adress, email-adress, etc. He then also has the option check a checkbox to automatically register then by entering a password. Then his information is saved into the db as well (on the row created for the guest user).

PS: Again on Laratrust, I would create a guest role to show exactly the pages I want him to show, as well as custom checkout pages. So the only real problem is the login (by a custom auth guard, which I have no idea how to implement it) as well as later on registering for a user basically already existing (even if its just the ID in db so far).

I hope you got my idea.

nameless replied 6 months ago

Nobody has ideas?

santigarcor replied 5 months ago

I released a new version of Laratrust that supports multiple user models, i think it would help you with your problem.

whatarewe replied 5 months ago

nameless said: Edit: After thinking about it again, I want to get it a bit clearer. I basically plan to change the login page and add a button like "proceed as guest" and define a route like guestLogin for it. This route would point to a controller that somehow logs in using a (propably selfmade) auth. When clicking the button, a entry is added into the users table in my database, with only an idea and a remember token. Then, the guest user can place products in his shopping cart and can order something. In the order process, he needs to enter his personal information like his adress, email-adress, etc. He then also has the option check a checkbox to automatically register then by entering a password. Then his information is saved into the db as well (on the row created for the guest user).

Just brainstorming here.

  1. You don't really need a "continue as guest", because anyone who doesn't log in is already a guest. You'll never have a "non-guest non-user" on your site. So all you're really looking for is something like a session ID/remember token that uniquely identifies them so that you can associate a shopping cart with them. Having said that: is your shopping cart always associated with a user? You may want to change that. Maybe a flag that indicates whether it's a user or a guest shopping cart, and then a field that indicates the user_id or session_id/remember_token. Maybe a pivot table. Whatever you'd like.

  2. Your guests should have their own table. You really don't want to pollute your user table with people who never make an account. If they never check out, they should never be users. If they never choose to put in a password, they should never be users. Migrate your new user info to the users table when they put in the password.

There are other aspects of this you should look at, like your invoice/shipping methods, but it looks like your problem can be solved by taking a good look at the way your data is being organized and putting it in a way that works rather than trying to force the Auth system to do extra things.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us