简体   繁体   中英

Is it possible to get Auth::user in lumen?

I'm trying to make a login screen in my app. But it seems impossible to do it in lumen!

These example would work in laravel, but wont in lumen:

login:

// Not working
Auth::login($user);

getting auth user:

if(Auth::check()){
  $user = Auth::user();
}

Above example seems impossible to do in lumen, if there's a way to do that please show me! Thank you

Source: https://lumen.laravel.com/docs/5.8/authentication

   Note: If you would like to use Auth::user() to access the currently authenticated user, 
   you should uncomment the $app->withFacades() method in your bootstrap/app.php file.

   Note: Before using Lumen's authentication features, you should uncomment the call to 
   register the AuthServiceProvider service provider in your bootstrap/app.php file.

There could be multiple reasons why this is "not working".

Did you import Auth at the beginning of the file?

use Auth;

Uncomment (remove // from) the following line in bootstrap/app.php :

// $app->register(App\Providers\AuthServiceProvider::class);

Also if you have user's credentials, try using Auth::attempt() :

if (Auth::attempt(['email' => $email, 'password' => $password]))
        {
            return redirect()->intended('dashboard');
        }

Official docs: 1. https://lumen.laravel.com/docs/6.x/authentication 2. https://laravel.com/docs/6.x/authentication

Try posting some error messages in you question, if there are any. That would take out the guessing part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM