Laravel身份验证尝试不起作用或始终返回false?

时间:2019-02-14 16:35:12

标签: laravel laravel-5

我一直在到处看(我检查了所有其他重复的问题和答案),但不走运。我不明白我在做什么错,所有值都来自帖子,但是Auth:attempt一直失败/返回假,如果我尝试手动实现登录,它将无法按照我的期望进行身份验证,必须制定或使用单独的方法,例如用于验证,凭据,用户名..etc?

这是我的登录控制器>登录方法

public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'usermail'    => 'required|max:255',
            'password'    => 'required_with:usermail',
        ],[
            'password.required_with'  => "The password field is empty."
        ]);
        if ($validator->fails()) {
            return redirect()
                        ->route('admin.login')
                        ->withErrors($validator)
                        ->withInput();
        }
        $usermail = $request->get('usermail');
        $password = $request->get('password');
        $remember = $request->get('rememberMe');

        if(filter_var($usermail, FILTER_VALIDATE_EMAIL)) {
            $isEmailExist = User::where('user_email',$usermail)->first();
            if($isEmailExist != null){
                if(Auth::attempt([
                        'user_email' => $usermail,
                        'user_pass'  => $password
                ])){
                    return redirect()->intended('admin/dashboard');
                }else{
                    return back()->with([
                        'message'   => '<strong>ERROR</strong>: The password you entered for the email address <strong>'.$usermail.'</strong> is incorrect. <a href="'.route('admin.password.request').'">Lost your password?</a>'
                    ]);   
                }
            }else{
                return back()->with([
                    'message'   => '<strong>ERROR</strong>: Invalid email address.'
                ]);
            }
        }else{
            $isUsernameExist = User::where('user_login',$usermail)->first();
            if($isUsernameExist != null){
                if(Auth::attempt([
                        'user_login' => $usermail,
                        'user_pass'  => $password
                ],$remember)){
                    return redirect()->intended('admin/dashboard');
                }else{
                    return back()->with([
                        'message'   => '<strong>ERROR</strong>: The password you entered for the username <strong>'.$usermail.'</strong> is incorrect. <a href="'.route('admin.password.request').'">Lost your password?</a>'
                    ]);
                }
            }else{
                return back()->with([
                    'message'   => '<strong>ERROR</strong>: Invalid username. <a href="'.route('admin.password.request').'">Lost your password?</a>'
                ]);
            }
        }
    }

这是我的用户迁移方案,

Schema::create('vw_users', function (Blueprint $table) {
            $table->bigIncrements('ID');
            $table->string('user_login','60')->unique()->default('');
            $table->string('user_pass');
            $table->string('user_email','100')->unique()->default('');
            $table->rememberToken();
        });

这是我为用户播种的方式,

User::create([
    'user_login'            => 'admin',
    'user_pass'             => Hash::make("123456"),
    'user_email'            => 'admin@gmail.com',
]);

1 个答案:

答案 0 :(得分:0)

OK OK OK, 我成功了,我认为在laravel框架中,我们只能在数据库身份验证表中为密码“ password”字段创建列名。

我更新了以下更改:-

  • 我已将密码字段名称从迁移模式重命名为“ user_pass” “密码” 。 (还更新了登录控制器和用户模型)。
  • 在用户模型中添加了以下代码:-

    use Illuminate\Notifications\Notifiable;
    
    class User extends Authenticatable
    {
     use Notifiable;
     ...
    }
    

我检查了两次以确认,所以我恢复了原先的操作。

如果我对任何人都有意义,请让我知道并帮助我理解。 我看过类似Laravel: How can i change the default Auth Password field name这样的帖子 我能否拥有所有laravel预定义库和功能的参考书或博客?我知道供应商文件夹充满了惊喜,但仍然需要更多参考。

非常感谢您的宝贵时间。

相关问题