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条)

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂