简体   繁体   English

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

[英]How to authenticate email and password with laravel 5.3 manually?

Hello Everyone My question is how can we authenticate our email and password in laravel 5.3 ? 大家好我的问题是我们如何在laravel 5.3中验证我们的电子邮件和密码? I am not using Auth here , I am trying to create login system manually 我在这里没有使用Auth,而是尝试手动创建登录系统
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 我无法使用电子邮件和密码对用户进行身份验证,因为我在密码中使用了bcrypt()哈希函数,并且当我尝试将http请求与存储的密码匹配时,它显示错误,请帮帮我,谢谢

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. 这些更改使您可以使用Laravel的内置哈希功能在注册时生成哈希并计算在登录期间哈希是否有效。

Change your code to this. 将代码更改为此。

$data = Register::where('email' , $request->email)
                ->where('password' ,bcrypt($request->password))
                ->exists();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM