Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/AuthCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function handle()
$this->gen('SIMPLE_JWT_SECRET');
}

public function gen($key, string $value = null)
public function gen($key, ?string $value = null)
{
if (empty(env($key))) {
file_put_contents(BASE_PATH . '/.env', sprintf(PHP_EOL . '%s=%s', $key, $value ?? str_random(16)), FILE_APPEND);
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/UnauthorizedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UnauthorizedException extends AuthException

protected int $statusCode = 401;

public function __construct(string $message, AuthGuard $guard = null, Throwable $previous = null)
public function __construct(string $message, ?AuthGuard $guard = null, ?Throwable $previous = null)
{
parent::__construct($message, 401, $previous);
$this->guard = $guard;
Expand Down
12 changes: 6 additions & 6 deletions src/Guard/JwtGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ public function user(?string $token = null): ?Authenticatable
if ($result instanceof UnauthorizedException) {
throw $result;
}
return $result ?: null;
return $result ?? null;
}

try {
if ($token) {
$jwt = $this->getJwtManager()->parse($token);
$uid = $jwt->getPayload()['uid'] ?? null;
$user = $uid ? $this->userProvider->retrieveByCredentials($uid) : null;
Context::set($key, $user ?: 0);
Context::set($key, $user ?? 0);

return $user;
}
Expand Down Expand Up @@ -139,7 +139,7 @@ public function guest(?string $token = null): bool
*/
public function refresh(?string $token = null): ?string
{
$token = $token ?: $this->parseToken();
$token = $token ?? $this->parseToken();

if ($token) {
Context::set($this->resultKey($token), null);
Expand All @@ -158,7 +158,7 @@ public function refresh(?string $token = null): ?string
return null;
}

public function logout($token = null)
public function logout(?string $token = null)
{
if ($token = $token ?? $this->parseToken()) {
Context::set($this->resultKey($token), null);
Expand All @@ -170,7 +170,7 @@ public function logout($token = null)
return false;
}

public function getPayload($token = null): ?array
public function getPayload(?string $token = null): ?array
{
if ($token = $token ?? $this->parseToken()) {
return $this->getJwtManager()->justParse($token)->getPayload();
Expand All @@ -183,7 +183,7 @@ public function getJwtManager(): JWTManager
return $this->jwtManager;
}

public function id($token = null)
public function id(?string $token = null)
{
if ($token = $token ?? $this->parseToken()) {
return $this->getJwtManager()->parse($token)->getPayload()['uid'];
Expand Down
2 changes: 1 addition & 1 deletion src/Guard/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function user(): ?Authenticatable
if ($result instanceof \Throwable) {
throw $result;
}
return $result ?: null;
return $result ?? null;
}

try {
Expand Down
8 changes: 4 additions & 4 deletions src/Guard/SsoGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public function getClients(): array
return $this->config['clients'] ?? ['unknown'];
}

public function login(Authenticatable $user, array $payload = [], string $client = null)
public function login(Authenticatable $user, array $payload = [], ?string $client = null)
{
$client = $client ?: $this->getClients()[0]; // 需要至少配置一个客户端
$client = $client ?? $this->getClients()[0]; // 需要至少配置一个客户端
$token = parent::login($user, $payload);
$redisKey = str_replace('{uid}', (string) $user->getId(), $this->config['redis_key'] ?? 'u:token:{uid}');

Expand All @@ -64,12 +64,12 @@ public function login(Authenticatable $user, array $payload = [], string $client
return $token;
}

public function refresh(?string $token = null, string $client = null): ?string
public function refresh(?string $token = null, ?string $client = null): ?string
{
$token = parent::refresh($token);

if ($token) {
$client = $client ?: $this->getClients()[0]; // 需要至少配置一个客户端
$client = $client ?? $this->getClients()[0]; // 需要至少配置一个客户端
$redisKey = str_replace('{uid}', (string) $this->id($token), $this->config['redis_key'] ?? 'u:token:{uid}');
$this->redis->hSet($redisKey, $client, $token);
}
Expand Down