Thursday, March 16, 2017

Bug in Migrating md5 to hash in Laravel

The algorithm works, but once the password has been converted to hash and saved into the database, it doesn't redirect to the homepage. Instead, it redirects to the login page saying that the login credentials is incorrect. But if I tried logging in, it is ok. What am I doing wrong?

AuthenticatesUsers.php

protected function attemptLogin(Request $request)
{
    $check = $this->guard()->attempt(
        $this->credentials($request), $request->has('remember')
    );
    if ($check === false)
    {
        $user = User::where('username','=',$request->input('username'))->first();

        if(isset($user)) {
            if($user->password == md5($request->input('password'))) { // If their password is still MD5
                $hashed_password = Hash::make($request['password']); // Convert to new format
                $user->password = $hashed_password;
                $user->save();

                return $this->guard()->attempt(
                    array(
                        'username'=>$request->input('username'),
                        'password'=>$hashed_password
                    ), $request->has('remember')
                );

            } else {
                // Redirect to the login page.
                return false;
            }
        }
    }
    return $check;
}



via EatCodePlaySleep

Advertisement