【问题标题】:Laravel auth attempt not working or returning false always?Laravel auth 尝试不起作用或总是返回 false?
【发布时间】:2019-02-14 16:35:12
【问题描述】:

我一直在寻找所有地方(我检查了所有其他重复的问题和答案),没有运气。我不明白我做错了什么,所有的值都来自帖子,但是 Auth:attempt 一直失败/返回 false,如果我尝试手动实现登录,它不会像我期望的那样进行身份验证,我也这样做必须制作或使用单独的方法,如验证、凭据、用户名等?

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

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',
]);

【问题讨论】:

  • - Auth::attempt 使用 bcrypt,你使用哪个哈希函数?另外,检查 user_pass 字段的长度。
  • 哈希::make("123456")
  • 默认为 "user_pass" varchar(191)
  • 究竟是什么失败了?验证或登录尝试?您是否仅通过传递硬编码的正确凭据单独检查 Auth::attempt?
  • NO 不只验证 auth:attempt 不工作 其他一切工作正常,是的,我尝试硬编码仍然不工作。

标签: laravel laravel-5


【解决方案1】:

好的好的好的, 我做到了,我认为在 laravel 框架中我们只能在数据库身份验证表中为密码创建列名是“密码”字段。

我更新了以下更改:-

  • 我将 密码字段名称 从迁移架构中的 "user_pass" 重命名为 "password"。 (也在登录控制器和用户模型中更新)。
  • 在用户模型中添加了以下代码:-

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

我检查了两次以确认,所以我恢复它没有工作。

如果我对任何人有任何意义,请告诉我并帮助我理解。 我查看过类似Laravel: How can i change the default Auth Password field name 的帖子 我可以为所有 laravel 预定义的库和函数提供参考书或博客吗?我知道供应商文件夹充满了惊喜,但我仍然需要更多参考资料。

非常感谢您的宝贵时间。

【讨论】:

    猜你喜欢
    • 2013-09-28
    • 2019-06-28
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 2022-07-10
    • 2016-09-12
    • 2017-05-24
    • 2013-06-11
    相关资源
    最近更新 更多