Laravel Guest Login

时间:2017-04-24 17:25:39

标签: php laravel authentication laravel-5.3 laravel-authorization

I'm currently developing a little web portal where users can register, but they should also be able to login as guests.

How I'm trying to do that is by this code:

 public function registerGuest(){
        $user = User::create([
            'name' => "Gast",
            'firstName' => "Gast",
            'sex' => "gast",
            'password' => bcrypt("pw"),
            'role' => "buyer",
            'templateURL' => "",
            'confirmation_code' => ""
        ]);
        $user->attachRole(2);

        Auth::loginUsingID($user,true);
        return redirect('/home');
    }

found in my RegisterController. So I'm basically registering a user (the same way they normally register) but with a simple password and no email address entered. Then I try to login this user using the ID. The user is created in the database but the login doesn't work.

Does anybody happen to know why this happens? And how can I fix this?

A more elaborate explanation to understand my idea: Users can normally register, entering some of their personal data. But they should also be able to click on a button saying "proceed as guest", which points to the above function. Then they are logged in like the registered user and can afterwards also register, keeping the information they entered while being logged in as guest user and also they can then set a password? The purpose: They might want to first test it and then register afterwards.

Any ideas?

1 个答案:

答案 0 :(得分:2)

该函数需要用户ID,而不是对象:

Auth::loginUsingID($user->id,true);