【问题标题】:Auth Attempt password Laravel验证尝试密码 Laravel
【发布时间】:2017-03-14 21:17:55
【问题描述】:

在一个项目中,我正在注册这样的用户

private function insertUserCredentials(User $user, Request $request){
    ...
    $user->email = $request->email
    $user->password = Hash::make($request->password );
    ...
}

我是这样登录的

if (!Auth::attempt([
        'email' => $request->emailLogin,
        'password' => $request->passwordLogin,
     ], $remember)
)
...

密码是用hasing插入到db中的,但在尝试中我只需要检索用户输入的密码,一切正常。

现在,如果我像这样注册用户

    $Blowfish_Pre = '$2a$05$';
    $Blowfish_End = '$';
    $Allowed_Chars ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';
    $Chars_Len = 63;
    $Salt_Length = 21;
    $salt = "";
    for($i=0; $i<$Salt_Length; $i++)
    {
        $salt .= $Allowed_Chars[mt_rand(0,$Chars_Len)];
    }
    $bcrypt_salt = $Blowfish_Pre . $salt . $Blowfish_End;
    $user->email = $request->registeremail;
    $user->password = crypt($request->password, $bcrypt_salt);

我如何使用 Auth 尝试?我必须进行哪些更改才能使其正常工作?

if (!Auth::attempt([
        'email' => $request->emailLogin,
        'password' => **here is my question**,
     ], $remember)
)

【问题讨论】:

    标签: php laravel authentication hash


    【解决方案1】:

    我想最简单的方法是为此创建自己的方法并使用login() 方法手动验证用户:

    public function customAttempt($username, $password) {
        $user = User::find('username', $username);
        if (....checking for credentials.....) {
            // Authorize user.
            Auth::login($user, true);
        }
    }
    

    【讨论】:

    • 我这样做了,但后来我无法调用 Auth 函数,例如 Auth::user() var_dumps null
    • 哦!我不知道 Auth::login。这会给我 Auth 功能吗?
    • 好的!谢谢 :) 我会在 5 分钟内接受答复
    • 很高兴它有帮助。 )
    猜你喜欢
    • 2013-07-31
    • 2014-04-07
    • 2012-04-18
    • 1970-01-01
    • 2018-02-24
    • 2017-07-26
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    相关资源
    最近更新 更多