我正在将JWT-auth与Laravel Framework一起使用来对用户进行身份验证。 Laravel用作服务器端框架,前端代码在我们自己开发的框架中。因此,我们使用api而非web来实现身份验证。登录在此环境中效果很好,而注销和刷新令牌无法按我希望的那样执行。我按照JWT-auth documentation的说明配置了所有内容。
route.php
Route::group(['middleware' => 'api', 'prefix' => 'user', 'namespace' => 'User'], function () {
Route::post('/login', 'AuthController@login'); // login
Route::post('/logout', 'AuthController@logout'); // logout (invalidate token)
Route::post('/refresh', 'AuthController@refresh'); // refresh token});
kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'password',
'model' => App\User::class,
],
/*'users' => [
'driver' => 'database',
'table' => 'user',
],*/
],
User \ AuthController
<?php
namespace App\Http\Controllers\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use AjaxResponse;
use Log;
class AuthController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
/**
* login
* @param Request $request
* @return mixed
*/
public function login(Request $request)
{
$credentials = $request->only('phone', 'password');
if (! $token = auth()->attempt($credentials)) {
return AjaxResponse::fail(4001);
}
return $this->respondWithToken($token);
}
/**
* logout(invalidate token)
* @return \Illuminate\Http\JsonResponse
*/
public function logout()
{
Log::debug('yyyyyyyyy');
auth()->logout();
return AjaxResponse::succeed(['message' => 'Successfully logged out']);
}
/**
* refresh token
* @return \Illuminate\Http\JsonResponse
*/
public function refresh()
{
return $this->respondWithToken(auth()->refresh());
}
/**
* get token structure
* @param $token
* @return mixed
*/
protected function respondWithToken($token)
{
if (Auth::user()['deleted_at'] || ! Auth::user()['is_active'])
return AjaxResponse::fail(4001);
else
return AjaxResponse::succeed([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth()->factory()->getTTL() * 60,
'user_name' => Auth::user()['name'],
'user_admin' => (bool)Auth::user()['is_admin']
]);
}
}
'yyyyyyyyy'无法记录。因此,似乎没有调用logout
中的AuthController
函数。
我写或错过了什么错吗?预先感谢。
答案 0 :(得分:0)
经过几次尝试,我已经更改了AuthController的构造函数,并且可以正常工作。
User \ AuthController
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login', 'refresh', 'logout']]);
}
我已将功能添加为'except'的值,我希望在其中使当前会话无效。