我有 2 个用户,即用户和总线,对于他们我有不同的模型我使用 laravel 5.5 和 tymondesigns/jwt-auth 1.0.0-rc.1 版本
用户非常适合用户我也得到令牌,但总线用户我们得到'invalid_email_or_password'
链接到完整代码完整源代码链接
这是我的用户模型:
class User extends Authenticatable implements JWTSubject{
use Notifiable;
protected $fillable = ['name', 'email', 'password'];
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}}
我的配置/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
'bus' => [
'driver' => 'session',
'provider' => 'buses',
],
'bus-api' => [
'driver' => 'jwt',
'provider' => 'buses',
],
],
我的提供者是:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
//next
'buses' => [
'driver' => 'eloquent',
'model' => App\Bus::class,
],
我 的总线登录控制器正在为用户表获取用户名和密码
public function busLogin(Request $request)
{
\Config::set('jwt.user', "App\Bus");
\Config::set('auth.providers.users.model', \App\Bus::class);
$credentials = $request->only('email', 'password');
try {
\Config::set('jwt.user', "App\Bus");
\Config::set('auth.providers.users.model', \App\Bus::class);
if (!$token = JWTAuth::attempt($credentials)) {
\Config::set('jwt.user', "App\Bus");
\Config::set('auth.providers.users.model', \App\Bus::class);
return response()->json([
'response' => 'error',
'message' => 'invalid_email_or_password',
]);
}
} catch (JWTAuthException $e) {
return response()->json([
'response' => 'error',
'message' => 'failed_to_create_token',
]);
}
return response()->json([
'response' => 'success',
'result' => [
'token' => $token,
'message' => 'I am Admin user',
'user' => '99',
],
]);
}
我的路线API:
Route::post('bus/auth/login', 'Bus\Auth@busLogin');
Route::post('bus/auth/register', 'Bus\Auth@busRegister');
每当我尝试使用 Bus 模型用户名和密码登录时,我都会在 buscontroller 登录路径中登录无效,但是如果我尝试使用用户模型凭据登录,我会得到令牌作为回报,如何使用 laravel 5.5 使用 jwtauth 设置多个身份验证