【问题标题】:Is it possible to get Auth::user in lumen?是否可以在流明中获得 Auth::user ?
【发布时间】:2020-05-04 02:49:19
【问题描述】:

我正在尝试在我的应用中制作登录屏幕。但是在流明中似乎是不可能的!

这些例子可以在 laravel 中工作,但在 lumen 中不行:

登录:

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

获取授权用户:

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

上面的例子似乎不可能用流明做,如果有办法做到这一点,请告诉我!谢谢

【问题讨论】:

  • 您应该使用 JWT 令牌在 API 中对用户进行身份验证。
  • 我不太明白。您正在尝试检查用户是否已通过身份验证。如果是这样,您是否尝试使用已登录的用户登录?
  • 检查用户是否通过身份验证后,我正在尝试获取用户数据!但没有成功!
  • Lumen 有自己的文档,特别是在 authentication 上。你读了吗?
  • 请注意,Lumen 应该是用于创建微服务的无状态 API 框架。如果您要添加屏幕并创建整个应用程序,那么切换到 Laravel 可能是一个更好的主意。

标签: php laravel lumen


【解决方案1】:

来源: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.

【讨论】:

    【解决方案2】:

    这可能有多种原因“不起作用”。

    你是否在文件开头导入了 Auth?

    use Auth;
    

    取消注释(从 // 中删除)bootstrap/app.php 中的以下行:

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

    另外,如果您有用户凭据,请尝试使用Auth::attempt()

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

    官方文档: 1.https://lumen.laravel.com/docs/6.x/authentication 2.https://laravel.com/docs/6.x/authentication

    尝试在您的问题中发布一些错误消息(如果有)。这将消除猜测部分。

    【讨论】:

      猜你喜欢
      • 2016-05-23
      • 2016-04-06
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 2013-11-25
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多