douchi8503 2016-12-06 16:14
浏览 78
已采纳

如何使用laravel 5.3手动验证电子邮件和密码?

Hello Everyone My question is how can we authenticate our email and password in laravel 5.3 ? I am not using Auth here , I am trying to create login system manually
This is user register method
public function post_register(Request $request){ $this->validate($request , [ 'username' => 'required|' , 'email' => 'required|email|unique:registers' , 'password' => 'required|min:6', 'cp' => 'required|same:password']); $data = new Register; $data->username = $request->username; $data->email = $request->email; $data->password = bcrypt($request->password); $data->save(); return Redirect::back()->with('success' , 'user registred'); }

This is login method

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

 $data = Register::where('email' , $request->email)->exists();
 if($data){
     Session::put('email' , $request->email);
     return Redirect::to('profile');

 }
 else{
    return Redirect::to('login');

 }

this code is working , but problem is that if i enter registered email and unregistered password then it redirect to profile page. i am not able to authenticate user with email and password because i am using bcrypt() hash function in password and when i try to match http request with stored password , it show error Please help me ,Thanks

  • 写回答

2条回答 默认 最新

  • dongyu4908 2016-12-06 16:53
    关注

    It wont work because you are comparing the string results of the hash which isnt correct.

    Changes you Register Function

    $data->password = Hash::make($request->password);
    

    Change Your Login function

    public function post_login(Request $request){
        $this->validate($request , [
            'email' => 'required|email' ,
            'password' => 'required']);
    
        $data = Register::where('email' , $request->email)->first();
        if($data){ 
            if(Hash::check($request->password, $data->password)){
                Session::put('email' , $request->email);
                return Redirect::to('profile');
            }
        }
        return Redirect::to('login');
    }
    

    Explanation

    These changes allow you to use Laravel's built in Hashing functionality for Generating Hashes At Registration & Calculating if a hash is valid during login.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失