-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Your Name
committed
Aug 11, 2018
1 parent
8f9e841
commit 7862d4f
Showing
15 changed files
with
704 additions
and
64 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Auth; | ||
use Closure; | ||
use Tymon\JWTAuth\Exceptions\JWTException; | ||
use Tymon\JWTAuth\Exceptions\TokenExpiredException; | ||
use Tymon\JWTAuth\Http\Middleware\BaseMiddleware; | ||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; | ||
|
||
class RefreshToken extends BaseMiddleware | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
// 检查此次请求中是否带有 token,如果没有则抛出异常。 | ||
|
||
$this->checkForToken($request); | ||
|
||
|
||
// 使用 try 包裹,以捕捉 token 过期所抛出的 TokenExpiredException 异常 | ||
try { | ||
// 检测用户的登录状态,如果正常则通过 | ||
if ($this->auth->parseToken()->authenticate()) { | ||
return $next($request); | ||
} | ||
|
||
throw new UnauthorizedHttpException('jwt-auth', '未登录'); | ||
} catch (TokenExpiredException $exception) { | ||
// 此处捕获到了 token 过期所抛出的 TokenExpiredException 异常,我们在这里需要做的是刷新该用户的 token 并将它添加到响应头中 | ||
try { | ||
// 刷新用户的 token | ||
$token = $this->auth->refresh(); | ||
// 使用一次性登录以保证此次请求的成功 | ||
Auth::guard('api')->onceUsingId($this->auth->manager()->getPayloadFactory()->buildClaimsCollection()->toPlainArray()['sub']); | ||
} catch (JWTException $exception) { | ||
// 如果捕获到此异常,即代表 refresh 也过期了,用户无法刷新令牌,需要重新登录。 | ||
throw new UnauthorizedHttpException('jwt-auth', $exception->getMessage()); | ||
} | ||
} | ||
|
||
// 在响应头中返回新的 token | ||
return $this->setAuthenticationHeader($next($request), $token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Auth\Factory as Auth; | ||
|
||
class SetAuthGuard | ||
{ | ||
/** | ||
* @var | ||
*/ | ||
protected $auth; | ||
|
||
/** | ||
* SetAuthGuard constructor. | ||
* @param Auth $auth | ||
*/ | ||
public function __construct(Auth $auth) | ||
{ | ||
$this->auth = $auth; | ||
} | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next,$guard) | ||
{ | ||
$this->auth->shouldUse($guard); | ||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Foundation\Auth\User as Authenticatable; | ||
use Illuminate\Notifications\Notifiable; | ||
|
||
class User extends Authenticatable | ||
{ | ||
use Notifiable; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
database/migrations/2018_08_11_135546_create_members_table.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters