【问题标题】:How do I manually authenticate user in Laravel 5.4如何在 Laravel 5.4 中手动验证用户
【发布时间】:2018-01-29 02:41:08
【问题描述】:

我正在尝试在 laravel 5.4 中对用户进行身份验证,使用尝试()我能够对用户进行身份验证,但我还想检查用户是否处于活动状态,并根据用户是否处于非活动状态显示自定义消息然后不同的消息,如果用户名密码不同,则不同的消息。

我的验证码:

public function checklogin(Request $request)
    {
        $password = bcrypt($request->password);
        $userData = User::where('email',$request->email)->where('password',$password)->first();
        if($userData!=NULL)
        {
            $credentials=array('email' => $request->input('email'),'password' => $request->input('password'),'status' => 'active');

            if(Auth::guard('users')->attempt($credentials)) 
                return redirect()->intended('/dashboard');
             else
             {
                Mail::to($request->email)->send(new VerifyUser($userData));
                return redirect('/')->with('error','Your account is unverified - check your email for account verification link');
             }
        }
        else
            return redirect('/')->with('error','Invalid username or password');


    }

在这里,我首先要检查登录凭据是否正确,如果它们无效则直接显示错误消息“无效的用户名或密码”,如果假设登录凭据正确,那么我正在尝试登录,如果用户处于非活动状态,那么我想显示“您需要验证您的帐户”的消息,如果用户处于活动状态,那么它会将用户重定向到仪表板。

但在我的情况下,即使我 bcrypt 密码并与 db 表中的密码进行交叉检查,我也无法验证用户身份。

【问题讨论】:

  • 这不是我想要的,我的查询使用 bcrypt 我无法交叉验证存储在表中的密码,还有其他方法吗?
  • 您将$request->input('password') 传递给credentials ,这不是使用bcrypt 散列的密码。
  • 你好,请检查第 3 行和第 4 行,我想要一个解决方案
  • 好的。您在数据库中存储ActiveInactive 的位置.. 给我字段名称..

标签: php laravel authentication laravel-5


【解决方案1】:

通知评论。这应该可以。

  public function checklogin(Request $request)
    {
      $password = bcrypt($request->password);
      $userData = User::where('email',$request->email)->where('password',$password);
      if($userData->get()->count() > 0) //Check if User exists
      {
        if($userData->where('status','inactive')->get()->count() > 0) // Check if User is inactive
        {
          Mail::to($request->email)->send(new VerifyUser($userData->get()->first()));
          return redirect('/')->with('error','Your account is unverified - check your email for account verification link');
        }

        $credentials=array('email' => $request->input('email'),'password' => $password); // passing hashed password

        if(Auth::guard('users')->attempt($credentials)) 
          return redirect()->intended('/dashboard');
      }
      else
        return redirect('/')->with('error','Invalid username or password');
    }

【讨论】:

    【解决方案2】:

    有很多方法可以做到这一点,但是由于您在评论中提到您有兴趣让代码中的第 4 行工作

    $userData = User::where('email',$request->email)->where('password',$password);
    

    上面的行将不起作用,因为bcrypt 生成的新哈希与数据库中的哈希密码不匹配。

    您可以这样做:

    $userData = User::where('email',$request->email)->first();
    
    if ($userData && Hash::check($request->password, $userData->password))
    {
        // The passwords match...
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 2018-05-30
      • 2017-12-17
      • 1970-01-01
      • 2011-01-28
      相关资源
      最近更新 更多