Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

php - Laravel 7 auth attempt returns false

I just cant seem to find a solution, my Auth::attempt() function always return false and I cant find what I am doing wrong. This is my migration for tblusers:

public function up()
{
    Schema::create('tblusers', function (Blueprint $table) {
        $table->id();
        $table->string('Username')->unique();
        $table->string('FirstName');
        $table->string('LastName');
        $table->string('ContactNo')->unique();
        $table->string('Email')->unique();
        $table->timestamp('EmailVerifiedOn')->nullable();
        $table->string('Password');
        $table->integer('AddedBy');
        $table->rememberToken();
        $table->timestamps();
    });
}

I customized the User model:

class User extends Authenticatable
{

    protected $table = 'tblusers';
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'Password', 'remember_token',
    ];
}

Therefore the password is no longer visible on User:: queries, so I use Auth::attempt instead. This is how I insert my hashed password:

    $password = $request->Password;
    $hash = Hash::make($password);

    $insertData = array(
        ...
        'Email' => $email,
        'Password' => $hash,
        'AddedBy' => 0,
        'created_at' => date('Y-m-d H:i:s')
    );

This is how I use Auth::attempt, can somebody point out what I'm doing wrong as it always returns false.

        $credentials = array(
            "Username" => $username,
            "Password" => Hash::make($password)
        );
        
        return dd(Auth::attempt($credentials));
        if (Auth::attempt($credentials)) {
            $response = true;
        } else {
            $response = false;
        }   
question from:https://stackoverflow.com/questions/65830306/laravel-7-auth-attempt-returns-false

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The 'password' field that is passed to attempt MUST be named password (exactly like this). Also you will need to adjust the getAuthPassword method on your Model since by default it expects the password field in the database to be 'password'. If this is a new project and database I highly suggest you follow the conventions, but if you want to keep things the way they are, these are the changes you will need to make:

$credentials = [
    'Username' => $username,
    'password' => $password,
];

Notice the key is password and we are not hashing the password, as it is expecting the plain-text version of the password so it can eventually do a hash check on it.

On this Model you will need to override the getAuthPassword method:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...