为什么在Laravel 5.3中Auth::attempt总是返回false ?


Why is Auth::attempt always returns false in Laravel 5.3?

我是Laravel的新手。我尝试在Laravel 5.3中使用多重认证,我的Auth .php文件是:

<?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作为密码(Bring也使用Hash::make()函数作为密码)。例如,我的模型Courier是:

<?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);
    }

我有一个名为authenticate的方法,但方法尝试总是返回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'));
    }
我不知道我做错了什么。我做错什么了吗?

你已经在你的模型和控制器上加密了两次密码。

只删除其中一个

e。g:不要在你的控制器上使用bcrypt,因为你已经在你的模型上使用了bcrypt