首页 > 解决方案 > 使用身份验证尝试时出现问题

问题描述

在将问题带到这里之前,我真的尝试自己调试我的问题,但是我真的找不到解决我的 laravel 身份验证问题的方法,尽管这似乎是一个常见问题。

我的身份验证不会登录。它总是返回 false,我不明白为什么。

我正在尝试使用 auth:attempt 登录,但它不起作用。总是在其他情况下。

当 auth 尝试获得成功时,如果条件其他条件,它会自动进入

    <?php

           namespace App\Http\Controllers;

           use Illuminate\Http\Request;
           use Illuminate\Support\Facades\Hash;
           use App\Http\Controllers\Redirect;
           use App\User;
           use Illuminate\Support\Facades\Auth;



          class AdminCreateLogin extends Controller
         {
            public function addAdmin(Request $request)
            { 
                $name = $request->username;
                $password = $request->password;
                $password = Hash::make($password);
                 $email = $request->email;

                   
           $user= new User;

           $user->name = $name;
           $user->password = $password;
           $user->email = $email;

            $user->save();
    
        }
        public function adminLogin(Request $request)
        {
           $email = $request->email;
           $password1 = $request->password;
           $password = bcrypt($password1);
           //$password = hash:make($password1);
           $userdata = array(
                                'email' => $email,
                                'password' => $password
                        );
           if(Auth::attempt($userdata))
           {
                 echo "login";
                die;
                return redirect('/admin/post/list');    
            }
            else
            {
                   echo "login not";
                   die;
                   return back()->with('error',"Invalid Login");
            }

       }
        }

请帮助我。

标签: laravellaravel-7

解决方案


你不需要bcrypt手动。https://laravel.com/docs/7.x/authentication#authenticating-users

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

if (Auth::attempt($credentials)) {
    // Authentication passed...
    return redirect()->intended('dashboard');
}

推荐阅读