Auth :: attempt($ credentials)始终返回false

时间:2019-07-18 22:19:49

标签: php laravel laravel-passport

每次我尝试使用Postman连接到我的API时,即使数据库中有相同的凭证,我也会得到错误的凭据。

我试图将数据库中的密码列的大小更改为60。 我打印了凭证以检查它们是否正确。

AuthController

public function login(Request $request) {
    $request->validate([
        'email' => 'required|string|email',
        'password' => 'required|string',
    ]);
    $credentials = request(['email', 'password']);
    if(!Auth::attempt($credentials))
        return response()->json([
            'message' => 'Unauthorized'
        ], 401);
    $user = $request->user();
    $tokenResult = $user->createToken('Personal Access Token');
    $token = $tokenResult->token;
    if ($request->remember_me)
        $token->expires_at = Carbon::now()->addWeeks(1);
    $token->save();
    return response()->json([
        'access_token' => $tokenResult->accessToken,
        'token_type' => 'Bearer',
        'expires_at' => Carbon::parse(
            $tokenResult->token->expires_at
        )->toDateTimeString()
    ]);
}

auth.php

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'gestionnaires',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'clients',
        'hash' => false,
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    'gestionnaires' => [
        'driver' => 'eloquent',
        'model' => App\Gestionnaire::class,
    ],

    'clients' => [
        'driver' => 'eloquent',
        'model' => App\Client::class,
    ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

模型

    use HasApiTokens, Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

api.php

Route::post('login', 'Auth\AuthController@login')->name('login');

2 个答案:

答案 0 :(得分:0)

尝试一下:

$credentials = [
 'email' => $request->email,
 'password' => $request->password
 ];
  if(!Auth::attempt($credentials))
    return response()->json([
        'message' => 'Unauthorized'
    ], 401);     

答案 1 :(得分:0)

我的模型看起来像If c = 1 Then MsgBox "No empty columns found", vbCritical Exit Sub End If 模型。

user