Laravel:auth无法登录

时间:2016-07-07 09:06:46

标签: php laravel authentication

我正在尝试在laravel中实现身份验证

这是我的帖子登录功能

public function postLogin(Request $request){

    if (Auth::attempt(['email' => $request->get('email'), 'password' => $request->get('password')])){
        return redirect()->route('index');}
else{
        return 'not entered ';
    }
}

但是,我每次都会收到not entered

我的密码在数据库中存储为纯文本(未散列)。

1 个答案:

答案 0 :(得分:2)

您知道吗,当您使用Auth::attempt()并传递密码init时,laravel将为您散列密码...例如,您的表中有123456密码。然后你尝试用Auth::attempt()登录..你的输入密码将变成“kjdfklsjgkjfglkjdgkjdfg”这个..现在你意识到了?为什么你的方法返回'未输入'而不是重定向到管理路由

尝试编写种子,你必须使用Hash facade Hash::make($password)或全局函数bcrypt($password),你将在数据库中获得一个哈希的passoword。之后,您将能够成功使用Auth :: attempt()

并且最好使用$ request-> input(),你知道它看起来不错! :)

尝试使用$ request-> input()而不是$ request-> get()

public function postLogin(Request $request){

  if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')])){
        return redirect()->route('admin');
    }else{
        return 'not entered ';
    }
}

快乐的编码!