简体   繁体   English

Laravel手动验证:Auth ::尝试始终为false

[英]Laravel manual authentication: Auth::attempt always false

i am using laravel manual authentication system.Submitting the form redirects to this route shown below.And in the authenticate () function the name and password never matches to which i stored earlier. 我正在使用laravel手动身份验证系统。提交表单重定向到下面显示的此路由。并且在authenticate()函数中,名称和密码永远不会匹配我之前存储的。 ie Auth::attempt is always false. Auth::attempt总是错误的。

 Route::post('/logintest', 'mycontroller@authenticate');
    Route::get('/home', ['middleware' => 'auth', function() {
  echo "home page";});
}]);

authenticate function: 验证功能:

public function authenticate(Request $request)
         {
            $input=$request->all();
            $password=$input['password'];
            $name=$input['name'];

            if (Auth::attempt(['Name' => $name, 'Password' => $password]) ){
            return redirect()->intended('/home');
        }   else 
          {
                return redirect('/login')->with('message','Error logging in!');
            }
        }

I've registered the user this way. 我以这种方式注册了用户。 the password is hashed using bcrypt(). 使用bcrypt()对密码进行哈希处理。 function. 功能。 but in authenticate() function i am comparing with plain password. 但在authenticate()函数中,我正在与普通密码进行比较。 i somewhere read Auth automatically handles it. 我在某处读取Auth自动处理它。 OR Is there something should i change in config/auth.php because i've used name to authenticate instead of username? 或者我是否应该在config / auth.php中更改某些内容,因为我使用了名称来进行身份验证而不是用户名?

public function register(Request $request)
{
    $input=$request->all();
    $password=bcrypt($input['password']);
    $name=$input['name'];
    $insert= User::insert(['Name'=>$name,'Password'=>$password]);
    return redirect('/login')
            ->with('message','successfully Registered.');
}

There is a problem with the names. 这些名字有问题。 Auth@attempt takes all those credentials, except password (case sensitive), that you pass in that array and runs a where query (This is how you can add extra constraints to the attempt, as they are just where conditions). Auth@attempt将获取除password (区分大小写)以外的所有凭据,您在该数组中传递并运行where查询(这是您可以为尝试添加额外约束的方式,因为它们只是条件的位置)。 If it finds a model it then will do a hash check on the password credential (case sensitive) you passed and the model's hashed password which it gets from $model->getAuthPassword() . 如果找到模型,那么将对您传递的password凭据(区分大小写)以及从$model->getAuthPassword()获取的模型散列密码进行散列检查。

This field in the credentials is a special one as it is what Auth needs so it knows what field in the credentials is meant to be the password. 凭证中的此字段是特殊的,因为它是Auth所需要的,因此它知道凭证中的哪个字段是密码。 It does not correlate directly to the field you have used on your users table, and must be named password in the credentials array. 它与您在users表上使用的字段没有直接关联,并且必须在凭证数组中命名为password The other fields in the credentials you pass, besides 'password', do correlate directly to the fields on the users table as they are conditions for a database query on that table. 您传递的凭据中的其他字段除了“密码”之外,还会直接与用户表上的字段相关联,因为它们是该表上数据库查询的条件。

You have to declare in your User model if you are using a field other than 'password' on the table as the password. 如果您使用表格上的“密码”以外的字段作为密码,则必须在用户模型中声明。 In your case you are using 'Password'. 在您的情况下,您使用的是“密码”。 (this is all case sensitive) (这是所有区分大小写的)

class User ....
{
    ...
    public function getAuthPassword()
    {
        return $this->Password; // case sensitive
    }
    ...
}

When passing the credentials you pass the plain text password as there will be a hash_check happening, not a direct comparison. 传递凭据时,您传递纯文本密码,因为会发生hash_check ,而不是直接比较。

You can name the fields what ever you want on your actual table, you just have to make Eloquent aware of this. 您可以在实际表格中为字段命名,只需要让Eloquent知道这一点。

Check the code below 检查下面的代码

public function authenticate(Request $request)
{
     $password = $request->input('password');
     $name = $request->input('name');

     if (Auth::attempt(['name' => $name, 'password' => $password]) )
     {
          return redirect()->intended('/home');
     }   
     else 
     {
          return view('login')->withErrors('Error logging in!');
     }
 }

You should write Password's p character in small letter. 你应该用小写字母写密码的p字符。

Replace, 更换,

Auth::attempt(['Name' => $name, 'Password' => $password])

to

Auth::attempt(['Name' => $name, 'password' => $password]) // 'p' is in small letter here.

check this link also. 也查看此链接

As you're using name instead of email (default) as username to authenticate with. 因为您使用名称而不是电子邮件(默认)作为用户名进行身份验证。 You should add $username property inside your AuthController . 您应该在AuthController添加$username属性。

....

class AuthController extends Controller
{


use AuthenticatesAndRegistersUsers, ThrottlesLogins;

/**
 * Override the input name 'email'
 * Change it as the name on blade
 *
 * @var string $username
 */
protected $username = 'Name';

....
}

Alternatively, you can override loginUsername() method from Illuminate\\Foundation\\Auth\\AuthenticatesUsers trait. 或者,您可以从Illuminate\\Foundation\\Auth\\AuthenticatesUsers trait中覆盖loginUsername()方法。

....

class AuthController extends Controller
{


use AuthenticatesAndRegistersUsers, ThrottlesLogins;

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function loginUsername()
{
    return 'Name';
}

....
}

Like others said, case matters. 像其他人所说,案件很重要。 You then need to override getAuthPassword() method from Illuminate\\Auth\\Authenticatable trait on your User model 然后,您需要在User模型上覆盖Illuminate\\Auth\\Authenticatable特征的getAuthPassword()方法

....

class User extends Authenticatable
{

....

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->Password;
    }
}

Everything appears to be correct. 一切似乎都是正确的。

What are the column names in the users table? users表中的列名是什么?

Names are case-sensitive. 名称区分大小写。 So make sure that they are indeed Name and Password and not name and password . 因此,请确保它们确实是NamePassword而不是namepassword

Why don't you use the command php artisan make:auth ? 你为什么不使用命令php artisan make:auth It will make everything you need. 它将满足您的一切需求。

Route::group(['middleware' => ['web']], function () {
    Route::post('/logintest', 'mycontroller@authenticate'); 
});
  1. please check with the above change in your Routes.php, provided you are using version 5 or 5.2 如果您使用的是版本5或5.2,请检查Routes.php中的上述更改

  2. Make sure your users table field names are "Name" and "Password" else update it. 确保您的用户表字段名称为“名称”和“密码”,否则更新它。

  3. Check the field length of your Password field (in your database, users table). 检查Password字段的字段长度(在数据库中, users表)。 It should hold a lengthy, hashed password something like this $2y$10$eM.kmjTwEIykhNUqMsNzMud0E6eO6RUYAzTqirrbozY1zdhVwQmsC atleast, (varchar(60)) 它应该拥有一个冗长的,哈希的密码,类似于$2y$10$eM.kmjTwEIykhNUqMsNzMud0E6eO6RUYAzTqirrbozY1zdhVwQmsC atleast,(varchar(60))

It would be better if you could show us the users table schema 如果你能向我们展示用户表模式会更好

  1. Finally, make sure you are entering the correct password (as I can't see much mistakes in your code) 最后,确保输入正确的密码(因为我在代码中看不到很多错误)

If you want to use Name as unique username and password fiendname as Password 如果要将Name用作唯一用户名,并使用密码fiendname作为Password

You can do this way.. 你可以这样做..

In your AuthController add this method 在您的AuthController中添加此方法

public function loginUsername()
{
    return 'Name';
}

And in your User model add this method 在您的用户模型中添加此方法

public function getAuthPassword()
{
    return $this->Password; 
}

Hope this will work. 希望这会奏效。

Try to switching to lowercase indexes in this line: 尝试在此行中切换到小写索引:

//...
if (Auth::attempt(['Name' => $name, 'Password' => $password]) ) {
//...

To

//...
if (Auth::attempt(['name' => $name, 'password' => $password]) ) {
//...

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

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