简体   繁体   English

Laravel API无法登录

[英]Laravel API having trouble logging in

I have the following 2 methods to create and login a user in my API . 我有以下两种方法可以在我的API创建和登录用户。 I cannot login the user. 我无法登录用户。 These files are in the api.php within the routes folder. 这些文件位于路由文件夹中的api.php中。 If I leave off the bcrypt($password) within the User::Create() method it still seems to hash the password somehow does the User::Create() method automatically hash the password. 如果我不User::Create()方法中的bcrypt($password) ,它似乎仍然会以某种方式对密码进行哈希处理: User::Create()方法会自动对密码进行哈希处理。 I am wondering if it is getting double hashed somehow. 我想知道它是否以某种方式被双散列。

Route::post('/register', function( Request $request){
    $rules = [
        'name' => 'required|max:255|alpha_dash|unique:users',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ];
    $input = $request->only(
        'name',
        'email',
        'password',
        'password_confirmation'
    );
    $validator = Validator::make($input, $rules);
    if($validator->fails()) {
        $error = $validator->messages();
        return response()->json(['success'=> false, 'error'=> $error]);
    }

    $name = $request->name;
    $email = $request->email;
    $password = $request->password;
    $user = User::create(['name' => $name, 'email' => $email, 'password' => bcrypt($password), 'api_token' => md5($email)]);

    return response()->json(['success'=> true, 'data'=> $user]);

});

Route::post("/login", function (Request $request){

    $rules = [
        'email' => 'required|email',
        'password' => 'required',
    ];

    $input = $request->only('email', 'password');

    $validator = Validator::make($input, $rules);

    if($validator->fails()) {
        $error = $validator->messages();
        return response()->json(['success'=> false, 'error'=> $error]);
    }


        $email = $request->email;
        $password =  bcrypt($request->password);



    if(\Auth::Attempt(['email' => $email, 'password' => $password])){
        ///How to get USER to return with Response
        return response()->json(['success' => true]);

    }else{
        return response()->json(['success' => false, 'error' => 'Invalid Credentials', 'password' => $password, 'email' => $email]);
    }

});

You bcrypt($request->password) but if you Auth::Attempt(['email' => $email, 'password' => $password]) , laravel automatically hash the value. bcrypt($request->password)但如果您Auth::Attempt(['email' => $email, 'password' => $password]) ,laravel会自动对值进行哈希处理。 So try in your login route just $password = $request->password; 因此,只需在您的登录路径中尝试$password = $request->password;

From the docs: 从文档:

The attempt method accepts an array of key / value pairs as its first argument. try方法接受键/值对的数组作为其第一个参数。 The values in the array will be used to find the user in your database table. 数组中的值将用于在数据库表中查找用户。 So, in the example above, the user will be retrieved by the value of the email column. 因此,在上面的示例中,将通过email列的值来检索用户。 If the user is found, the hashed password stored in the database will be compared with the password value passed to the method via the array. 如果找到用户,则将存储在数据库中的哈希密码与通过数组传递给该方法的密码值进行比较。 You should not hash the password specified as the password value, since the framework will automatically hash the value before comparing it to the hashed password in the database. 您不应该对指定为密码值的密码进行哈希处理,因为在将其与数据库中的哈希密码进行比较之前,框架会自动对其进行哈希处理。 If the two hashed passwords match an authenticated session will be started for the user. 如果两个哈希密码匹配,则将为用户启动经过身份验证的会话。

So this is what i came up with to "log the user in". 这就是我想出的“登录用户”的方法。

Route::post("/login", function (Request $request){

    $rules = [
        'email' => 'required|email',
        'password' => 'required',
    ];

    $input = $request->only('email', 'password');

    $validator = Validator::make($input, $rules);

    if($validator->fails()) {
        $error = $validator->messages();
        return response()->json(['success'=> false, 'error'=> $error]);
    }


   $email = $request['email'];
   $password = $request['password'];
   $user = User::where('email', $email)->first();
    if($user){
       if(\Hash::check($password, $user->password)){
           return response()->json(['success' => true, 'user' => $user]);
       }else{
           return response()->json(['success' => false, 'error' => 'Invalid Credentials']);
       }
    }else{
        return response()->json(['success' => false, 'error' => 'Invalid Credentials']);
    }

});

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

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