本文整理汇总了PHP中Tymon\JWTAuth\Facades\JWTAuth::setToken方法的典型用法代码示例。如果您正苦于以下问题:PHP JWTAuth::setToken方法的具体用法?PHP JWTAuth::setToken怎么用?PHP JWTAuth::setToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tymon\JWTAuth\Facades\JWTAuth
的用法示例。
在下文中一共展示了JWTAuth::setToken方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: headers
/**
* Return request headers needed to interact with the API.
*
* @return Array array of headers.
*/
protected function headers($user = null)
{
$headers = ['Accept' => 'application/json'];
if (!is_null($user)) {
$token = JWTAuth::fromUser($user);
JWTAuth::setToken($token);
$headers['Authorization'] = 'Bearer ' . $token;
}
return $headers;
}
示例2: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next, $isRest = "Rest")
{
Log::debug("CheckJWT.handle.begin");
$isAjax = $isRest == "Rest";
Log::warning("Rest call:" . $isAjax);
$jwt = DragoCookie::getCookie();
$token = null;
if ($jwt != null) {
$token = JWTAuth::setToken($jwt);
} else {
$token = JWTAuth::getToken();
}
if ($token == null) {
Log::warning("CheckJWT.handle.token invalid");
if ($isAjax) {
return response("token invalid", 401);
} else {
return redirect('login');
}
}
try {
$user = $token->authenticate();
Log::debug("CheckJWT.handle.user:" . print_r($user, true));
} catch (TokenExpiredException $e) {
Log::warning("CheckJWT.handle.token expired");
if ($isAjax) {
return response("token expired", 401);
} else {
return redirect('login');
}
} catch (JWTException $e) {
Log::warning("CheckJWT.handle.token invalid");
if ($isAjax) {
return response("token invalid", 401);
} else {
return redirect('login');
}
}
if (!$user) {
Log::warning("CheckJWT.handle.user not found");
if ($isAjax) {
return response("token invalid", 401);
} else {
return redirect('login');
}
}
Log::debug("CheckJWT.handle.end");
return $next($request);
}
示例3: setToken
/**
* Set the token.
*
* @param Token|string $token
*
* @return JwtGuard
*/
public function setToken($token)
{
$this->token = $token;
JWTAuth::setToken($token);
return $this;
}