How to convert password from md5 to laravel encryption method

时光毁灭记忆、已成空白 提交于 2019-12-01 00:04:12

Is there any direct method to do it?

No there's no direct method, but you could achieve that by overriding postLogin inside Auth/AuthController.php so it will check if the password is in md5 format then recrypt it with laravel hashing method else the user will connect normally, like :

public function postLogin(Request $request)
{
    $this->validate($request, [
        'login' => 'required', 'password' => 'required',
    ]);
    $credentials = $this->getCredentials($request);

    //Get the user
    $user = User::where('login', $request->login)->first();

    //If Hached by bcrypt
    if (Auth::attempt($credentials, $request->has('remember'))) 
    {
        return redirect()->intended($this->redirectPath());
    }
    else //Else if Hached by md5
    {
        if( $user && $user->password == md5($request->password) )
        {
            $user->password = Hash::make($request->password);
            $user->save();

            if($user->authorized){
                $user->save();

                Auth::login($user);
            }else
                Auth::logout();
        }
    }

    return redirect($this->loginPath())
        ->withInput($request->only('login', 'remember'))
        ->withErrors([
            'login' => $this->getFailedLoginMessage(),
        ]);
}

Hope this helps.

Unfortunately no.

The only method to achieve it is to develop new behavior of your app (writen in laravel) that allows users to login using old, md5-hashed passwords, and then enforces password change or - because you can get users password during login process - store password using laravels hashing method by updating logged user model.

Only the user should change his password (as you can't see their password). So you should send a reset password link for them and then update the password with Laravel hash method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!