2
我是Laravel的新人。我尝试在Laravel 5.3使用多个身份验证和我auth.php文件是:为什么Auth :: attempt在Laravel 5.3中总是返回false?
<?php
return [
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'courier' => [
'driver' => 'session',
'provider' => 'couriers',
],
'client' => [
'driver' => 'session',
'provider' => 'clients',
]
],
'providers' => [
'couriers' => [
'driver' => 'eloquent',
'model' => App\Courier::class,
],
'clients' => [
'driver' => 'eloquent',
'model' => App\Client::class,
],
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
]
],
'passwords' => [
'couriers' => [
'provider' => 'couriers',
'table' => 'password_resets',
'expire' => 60,
],
'clients' => [
'provider' => 'clients',
'table' => 'password_resets',
'expire' => 60,
],
],
];
然后,当我存储的客户端或快递在DB,我用bcrypt
密码(也带来了使用密码功能Hash::make()
)。例如,我的模型快递是:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Courier extends Authenticatable
{
[..]
public function setPasswordAttribute($pass){
$this->attributes['password'] = bcrypt($pass);
}
[..]
}
当更新的信使,在我的控制,我有:
public function update(Request $request, $id) {
$fieldsCourier = $request->all();
$courier = Courier::find($id);
if(isset($fieldsCourier['password']))
$fieldsCourier['password'] = bcrypt($fieldsCourier['password']);
if($courier->update($fieldsCourier))
$courier = Courier::find($id);
}
我有一个方法叫进行验证,但是该方法尝试总是返回false(invalid_credentials) 。即使如此发送有效凭证..这是我的代码:
public function authenticate(Request $request) {
$credentials = $request->only('email', 'password');
try {
if (auth()->guard('courier')->attempt($credentials)){
$user = Auth::guard('courier')->user();
} else {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json(compact('user'));
}
我不知道我在做什么错。我做错了什么?