我正在使用Laravel 5.4和Passport4。我只想使用First-Party-App
。因此,正如该答案所建议的那样,我想避免将ClientID和ClientSecret放入应用程序中。我输入了以下boot()
方法AuthServiceProvider
:
Passport::routes();
Passport::tokensExpireIn(Carbon::now()->addDays(30));
Passport::refreshTokensExpireIn(Carbon::now()->addDays(60));
Run Code Online (Sandbox Code Playgroud)
我添加了自己的路线api.php
以接受来自App的登录:
Route::post('login', 'Auth\LoginController@apiLogin');
Run Code Online (Sandbox Code Playgroud)
这是我的动作:
public function apiLogin(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed...
$user = Auth::user();
$token = $user->createToken('API Access')->accessToken;
return response()->json(["token_type" =>"Bearer","expires_in" => 2592000,"access_token" => $token]);
}
return response()->json(["error" => "invalid_credentials", "message" => "The user credentials were incorrect."], 401);
}
Run Code Online (Sandbox Code Playgroud)
是否有任何方法可以检索expires_in
(30天=> 2592000s)的秒数或日期时间,以便我可以自动进行计算?
我正在将 Laravel 5.7 用于某些 api。我还使用包https://github.com/tymondesigns/jwt-auth生成 JWT 令牌来验证用户。我很久以前就配置了所有东西,它运行良好。
我在RouteServiceProvider.php
以下注册路由组routes/api_v1.php
:
Route::prefix('api/v1')
->middleware('api')
->namespace($this->namespace.'\API\V1')
->group(base_path('routes/api_v1.php'));
Run Code Online (Sandbox Code Playgroud)
在config.auth.php
我有带有驱动程序 jwt 的 api_v1 防护:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api_v1' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
Run Code Online (Sandbox Code Playgroud)
我已经App/User.php
实现Tymon\JWTAuth\Contracts\JWTSubject
并实现了这两种方法。
但是当app/Http/Kernel.php
我在中间件数组中添加时:
protected $routeMiddleware = [
// previous middlewares,
'jwt.auth' => \App\Http\Middleware\JWTAuthenticate::class
];
Run Code Online (Sandbox Code Playgroud)
routes/api_v1.php
组下的路线jwt.auth
:
路由::中间件(['jwt.auth'])->组(函数(){
// Keep auto resource route at bottom …
Run Code Online (Sandbox Code Playgroud)