dongzi9196 2018-06-16 20:16
浏览 49

如何在登录控制器中使用Crypt加密登录

I am using Crypt:: for registration and login. My registration is successful but login is not successful. Please check the code and help me.

public function Login(Request $request)
{
    $this->validate($request, [
        'email' => 'required',
        'password' => 'required',
    ]);

    $userdata = array(
        'email' => $request->email,
        'password' => \Crypt::encrypt($request->password)
    );

    if (Auth::attempt($userdata) {
        echo "success";die();
    } 

    return "Ops! snap! seems like you provide an invalid login credentials";
}
  • 写回答

1条回答 默认 最新

  • dos8244 2018-06-16 20:38
    关注

    Originial

    You need to use Hashing, not Encryption.

    Registration

    ...
    
    $userdata = [
        'email'    => $request->email
        'password' => Hash::make($request->password)
    ];
    
    ...
    
    // User saved..
    

    Login

    $credentials = $request->only('email', 'password');
    
    if (Auth::attempt($credentials) {
        // It work
    } 
    

    Ref :


    Update

    OP : I need to Crypt::decrypt to decode the password and send on email. Using hash i couldn't decode it. Thats the reason i need Crypt.

    I really don't recommend it. That's why we have the "forgot password" feature to create new password.

    Is it secure to store passwords with 2 way encryption?

    Okay, back to the topic, How to login with Crypt encryption?

    You need to add login() method in Auth\LoginController :

    /**
     * Handle a login request to the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function login(Request $request)
    {
        $decrypted = $request->input('password'); 
        $user      = User::where('email', $request->input('email'))->first();
    
        if ($user) {
            if (Crypt::decryptString($user->password) == $decrypted) {
                Auth::login($user);
    
                return $this->sendLoginResponse($request);
            }
        }
    
        return $this->sendFailedLoginResponse($request);
    }
    

    WARNING!

    All of Laravel's encrypted values are signed using a message authentication code (MAC) so that their underlying value can not be modified once encrypted.

    You must have the same key. If you change the key (artisan key:generate), it means you will not be able to login.

    I really don't recommend it.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题