【发布时间】:2018-08-19 02:34:06
【问题描述】:
我正在使用...
- Laravel 5.4
- tymon/jwt-auth : 1.0.0-rc.2
我的应用程序有两个身份验证 API,一个是 customers,另一个是 drivers,每个都有自己的表。
现在让我简要介绍一下JWT 软件包的安装以及我对它所做的更新。
- 我完全按照JWT 文档中的说明安装了该软件包。
- 现在来到quick start,这里我更新了两个
Models,一个是User,第二个是Driver。 -
再次来到
Configure Auth guard这里我使用了两个guards的配置让我展示一下我的auth.php的快照。'defaults' => [ 'guard' => 'api', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'jwt', 'provider' => 'users', ], 'driver' => [ 'driver' => 'session', 'provider' => 'drivers', ], 'driver-api' => [ 'driver' => 'jwt', 'provider' => 'drivers', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], 'drivers' => [ 'driver' => 'eloquent', 'model' => App\Models\Driver::class, ], ], 现在继续使用
authentication routes申请,这是我的Routes两个Models
这是User 和Driver 路由
Route::group( [
'prefix' => 'auth',
'middleware' => 'api'
], function () {
.......
});
Route::group( [
'prefix' => 'driver',
'middleware' => 'api'
], function () {
.......
});
- 现在是
AuthController
在JWT 文档中,construct 是这样写的。
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
我发现一些文章建议在我们拥有的两种模型之间切换。
所以现在我的控制器看起来像现在这样。
public function __construct() {
$this->user = new User;
$this->driver = new Driver;
}
public function userLogin( Request $request ) {
Config::set( 'jwt.user', 'App\Models\User' );
Config::set( 'auth.providers.users.model', User::class );
$credentials = $request->only( 'email', 'password' );
$token = null;
try {
if ( $token = $this->guard()->attempt( $credentials ) ) {
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 front user',
],
] );
}
public function driverLogin( Request $request ) {
Config::set( 'jwt.user', 'App\Models\Driver' );
Config::set( 'auth.providers.users.model', Driver::class );
$credentials = $request->only( 'email', 'password' );
$token = null;
try {
if ( ! $token = $this->guard()->attempt( $credentials ) ) {
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 driver user',
],
] );
}
public function me() {
return response()->json( $this->guard()->user() );
}
public function logout() {
$this->guard()->logout();
return response()->json( [ 'message' => 'Successfully logged out' ] );
}
public function refresh() {
return $this->respondWithToken( $this->guard()->refresh() );
}
protected function respondWithToken( $token ) {
return response()->json( [
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => $this->guard()->factory()->getTTL() * 60
] );
}
public function guard() {
return Auth::guard();
}
现在正在发生以及我面临的问题
现在驱动程序 api 仅作为 login 工作。
localhost:8000/api/driver/login 工作正常
但是当尝试像这样获取驱动程序用户id
localhost:8000/api/driver/me它返回空array
第二期来了。
使用来自interface 的登录名,例如。 http://localhost:8000/login 它返回登录屏幕没有任何错误,因为登录信息是正确的,但是 auth.php 中的 defaults 是 'guard'=>'api' 如果我将其更改为 'guard'=>'web' 它会正确登录。
即使是 User API 对于 Ex。 localhost:8000/api/auth/login 总是返回
{
"response": "error",
"message": "invalid_email_or_password"
}
更新
我解决了一半 我将
AuthController更新为类似这样。
public function __construct() {
if ( Request()->url() == '/api/driver/me' ) {
$this->middleware( 'auth:driver-api', [ 'except' => [ 'login' ] ] );
} elseif ( Request()->url() == '/api/customer/me' ) {
$this->middleware( 'auth:api', [ 'except' => [ 'login' ] ] );
}
}
和登录功能是这样的。
public function login() {
if ( Request()->url() == '/api/driver' ) {
Config::set( 'auth.providers.users.model', Driver::class );
$credentials = request( [ 'email', 'password' ] );
if ( ! $token = auth()->attempt( $credentials ) ) {
return response()->json( [ 'error' => 'Unauthorized' ], 401 );
}
return $this->respondWithToken( $token );
}
Config::set( 'auth.providers.users.model', User::class );
$credentials = request( [ 'email', 'password' ] );
if ( ! $token = auth()->attempt( $credentials ) ) {
return response()->json( [ 'error' => 'Unauthorized' ], 401 );
}
return $this->respondWithToken( $token );
}
但是auth.php 还是有问题
'defaults' => [
'guard' => 'driver-api',
'passwords' => 'users',
],
在这里我需要将'guard'=>'api' 切换为'guard'=>'driver-api' 以防URL 请求为localhost:8000/api/driver/login 和'guard'=>'api' 以防URL 请求为localhost:8000/api/customer/login 以任何方式执行此操作。
更新 2
这里是driver Model
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Driver extends Authenticatable implements JWTSubject {
protected $guard = 'driver';
protected $fillable = [
...
'email',
'password',
...
];
protected $hidden = [
'password',
'remember_token',
];
public function getJWTIdentifier() {
return $this->getKey();
}
public function getJWTCustomClaims() {
return [];
}
}
还有UserModel
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject {
use Notifiable;
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
public function getJWTIdentifier() {
return $this->getKey();
}
public function getJWTCustomClaims() {
return [];
}
我需要一些帮助,请出主意。
【问题讨论】:
标签: php laravel authentication jwt laravel-5.4