无法使用Auth :: attempt登录

时间:2016-12-11 13:42:56

标签: mysql laravel-5

RegisterController:

public function register(Request $request)
{

    $user=$request->file('cover');
    $destination ='img/user';
    $filename=$user->getClientOriginalName();
    storage::put('img/user/'.$filename,file_get_contents($request->file('cover')->getRealPath()));

    $user = new User();
    $user->username = $request->username;
    $user->email = $request->email;
    $user->name = $request->name;
    $user->password = bcrypt($request->password);
    $user->cover = $filename;
    $roles_id = DB::table('roles')->select('id')->where('rolename','admin')->first()->id;
    $user->roles_id = $roles_id;       

    $user->save();


}

Login.blade.php:

<form action="/login" method="POST">
    <input type="hidden" name="_token" value="{{{ csrf_token() }}}">

    <div class="row">      
    <div class="input-field col s6"> 
    <input type="text" class="validate" name="username" placeholder="username">                    
    </div>           
    </div>     

    <div class="row">
    <div class="input-field col s6">
    <input type="password" class="validate" name="password" placeholder="Password">                
    </div>
    </div>

    <div class="row">
    <div class="input-field col s5">
    <input type="submit" class="btn">
    </div>
    </div>

    </form>

LoginController:

 public function postlogin(Request $request)
{                

   if(Auth::attempt([
       'username' => $request->username,
       'password' => $request->password
   ])){
       return 'Cool';
   }else{
       return 'Not Cool';
   }

}
如果你想看路线,以防万一:

Route::get('/register','Auth\RegisterController@showRegistrationForm');
Route::post('/register', 'Auth\RegisterController@register');
Route::get('/login', 'Auth\LoginController@showLoginForm');
Route::post('/login', 'Auth\LoginController@postlogin');

我看过这篇文章Laravel Auth::attempt() always false?,但仍然不知道如何解决它

我试着把

'password' => Hash::make($request->password)

并且仍然'不酷'

:(

1 个答案:

答案 0 :(得分:1)

Laravel的Hash::make()在内部使用bcrypt来散列密码。

Hash::make()每次都会产生不同的哈希值。因此,您不能在同一密码上再次使用Hash::make()并比较这两个值。它将始终返回false,因为每次生成的哈希值会有所不同。但是,Laravel使用Hash::check()Hash::make()辅助函数提供bcrypt()来检查密码哈希值。

如果您想使用Hash::make()bcrypt()帮助程序功能验证密码是否已经哈希,那么您可以通过

进行验证
/*  Say you are using word secret as password while registering   */
$password = 'secret';

$hashedPassword = Hash::make('secret'); 

/*   Or   */

$hashedPassword = bcrypt('sectet');



/*   To verify the $hashedPassword - probably stored in a database   */
function verify_password($password)
{
    if(Hash::check($password, $hashedPassword)
    {
        echo "Passwords Match!";
    }
    else
    {
        echo "Passwords do not match :(";
    }
}

/*   While logging back (after registering)
    in the first attempt you have a typo in your password secrets instead of secret
 */
$attempt1 = 'secrets';
verify_password($attempt1);  /*   will echo Passwords do not match :(   */

/*   in the second attempt you use the correct password secret   */
$attempt2 = 'secret';
verify_password($attempt2);  /*   will echo Passwords Match!   */  

希望这有助于理解。