JwtGuard
class JwtGuard implements Guard
Traits
These methods are typically the same across all guards.
Constants
| DEFAULT_TTL |
|
| protected GUARD_CONTEXT_KEY_PREFIX |
|
| private NO_EXPIRY |
|
Properties
| protected UserProvider|null | $provider | The user provider implementation. |
from GuardHelpers |
| static protected array | $macros | The registered string macros. |
from Macroable |
| protected Dispatcher|null | $events | The event dispatcher instance. |
Methods
Determine if the current user is authenticated. If not, throw an exception.
Get the ID for the currently authenticated user.
Mix another object into the class.
Dynamically handle calls to the class.
Dynamically handle calls to the class.
Create a new JWT authentication guard.
Attempt to authenticate a user using the given credentials.
Parse the JWT token from the current request.
Get the currently authenticated user.
Validate a user's credentials.
Log a user into the application using their credentials without persisting.
Log the given user ID into the application.
Create a new token by user ID.
Alias for onceUsingId.
Get the currently authenticated user or throw an exception.
Get the ID for the currently authenticated user.
Get the payload from the current JWT token.
Alias for getPayload.
Get the current token.
Get the token TTL.
Refresh the current JWT token.
Log the user out by invalidating the current token.
Invalidate the current token.
Determine if the guard has a user instance.
Forget the current user.
Get durable authentication Context keys.
Register an authentication attempt event listener.
Get the event dispatcher instance.
Get the last user we attempted to authenticate.
Decode a JWT token, caching the result per coroutine.
Return the currently cached user.
Get the Context key for an explicitly assigned user.
Pull custom claims for the next token.
Require a token to be available.
Get Context state.
Set Context state.
Forget Context state.
Get a Context state key.
Get the Context key for caching the authenticated user.
Get the Context key for caching a decoded payload.
Dispatch the given event if listeners are registered.
Fire the attempt event.
Fire the failed authentication attempt event.
Flush all static state.
Details
in
GuardHelpers at line 32
Authenticatable
authenticate()
Determine if the current user is authenticated. If not, throw an exception.
in
GuardHelpers at line 40
bool
check()
Determine if the current user is authenticated.
in
GuardHelpers at line 48
bool
guest()
Determine if the current user is a guest.
at line 272
int|string|null
id()
Get the ID for the currently authenticated user.
in
GuardHelpers at line 64
UserProvider|null
getProvider()
Get the user provider used by the guard.
in
GuardHelpers at line 75
void
setProvider(UserProvider $provider)
Set the user provider used by the guard.
Boot or tests only. The provider is stored on the worker-lifetime guard and affects every subsequent request.
in
Macroable at line 28
static void
macro(string $name, callable|object $macro)
Register a custom macro.
Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.
in
Macroable at line 41
static void
mixin(object $mixin, bool $replace = true)
Mix another object into the class.
Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.
in
Macroable at line 57
static bool
hasMacro(string $name)
Check if macro is registered.
in
Macroable at line 68
static void
flushMacros()
Flush the existing macros.
Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.
in
Macroable at line 78
static mixed
__callStatic(string $method, array $parameters)
Dynamically handle calls to the class.
in
Macroable at line 103
mixed
__call(string $method, array $parameters)
Dynamically handle calls to the class.
at line 58
__construct(string $name, UserProvider $provider, ManagerContract $jwtManager, ClaimFactory $claimFactory, Parser $parser, Container $app, int|null $ttl = self::DEFAULT_TTL)
Create a new JWT authentication guard.
at line 73
string|bool
attempt(array $credentials = [], bool $login = true)
Attempt to authenticate a user using the given credentials.
at line 94
string|null
parseToken()
Parse the JWT token from the current request.
at line 106
string
login(Authenticatable $user)
Log a user into the application and return the JWT token.
at line 122
Authenticatable|null
user()
Get the currently authenticated user.
at line 180
bool
validate(array $credentials = [])
Validate a user's credentials.
at line 188
bool
once(array $credentials = [])
Log a user into the application using their credentials without persisting.
at line 202
Authenticatable|false
onceUsingId(mixed $id)
Log the given user ID into the application.
at line 216
string|null
tokenById(mixed $id)
Create a new token by user ID.
at line 228
Authenticatable|false
byId(mixed $id)
Alias for onceUsingId.
at line 238
Authenticatable
userOrFail()
Get the currently authenticated user or throw an exception.
at line 250
int|string|null
getUserId()
Get the ID for the currently authenticated user.
at line 280
JwtGuard
claims(array $claims)
Add custom claims to the next JWT token.
at line 293
array
getPayload()
Get the payload from the current JWT token.
at line 307
array
payload()
Alias for getPayload.
at line 315
JwtGuard
setToken(string $token)
Set the token.
at line 325
string|null
getToken()
Get the current token.
at line 335
int|null
getTTL()
Get the token TTL.
at line 349
JwtGuard
setTTL(int|null $ttl)
Set the token TTL for the next token-producing operation.
at line 359
string|null
refresh(bool $forceForever = false, bool $resetClaims = false)
Refresh the current JWT token.
at line 389
void
logout(bool $forceForever = false)
Log the user out by invalidating the current token.
at line 411
JwtGuard
invalidate(bool $forceForever = false)
Invalidate the current token.
at line 425
bool
hasUser()
Determine if the guard has a user instance.
at line 433
Guard
setUser(Authenticatable $user)
Set the current user.
at line 444
JwtGuard
forgetUser()
Forget the current user.
at line 460
array
getAuthContextKeys()
Get durable authentication Context keys.
Per-request token resolution caches must not cross a request boundary.
at line 471
void
attempting(callable $callback)
Register an authentication attempt event listener.
Boot-only. Listener registrations persist on the worker-lifetime dispatcher and affect every subsequent request.
at line 479
Dispatcher|null
getDispatcher()
Get the event dispatcher instance.
at line 490
void
setDispatcher(Dispatcher $events)
Set the event dispatcher instance.
Boot or tests only. The dispatcher is stored on the worker-lifetime guard and affects every subsequent request.
at line 498
Authenticatable|null
getLastAttempted()
Get the last user we attempted to authenticate.
at line 506
protected string
makeTokenForUser(Authenticatable $user)
Create a token for the given user.
at line 525
protected array
decodeToken(string $token)
Decode a JWT token, caching the result per coroutine.
at line 536
protected Authenticatable|null
cachedUser()
Return the currently cached user.
at line 555
protected void
cacheUser(Authenticatable $user)
Cache the current user without firing guard events.
at line 563
protected string
getExplicitUserContextKey()
Get the Context key for an explicitly assigned user.
at line 571
protected array
pullCustomClaims()
Pull custom claims for the next token.
at line 585
protected string
requireToken()
Require a token to be available.
at line 597
protected mixed
getContextState(string $key, mixed $default = null)
Get Context state.
at line 605
protected void
setContextState(string $key, mixed $value)
Set Context state.
at line 613
protected void
forgetContextState(string $key)
Forget Context state.
at line 621
protected string
getContextStateKey(string $key)
Get a Context state key.
at line 629
protected string
getUserContextKey(string|null $token = null)
Get the Context key for caching the authenticated user.
at line 643
protected string
getPayloadContextKey(string $token)
Get the Context key for caching a decoded payload.
at line 651
protected void
dispatchIfListening(string $eventClass, Closure $event)
Dispatch the given event if listeners are registered.
at line 661
protected void
fireAttemptEvent(array $credentials)
Fire the attempt event.
at line 669
protected void
fireValidatedEvent(Authenticatable $user)
Fire the validated event.
at line 677
protected void
fireFailedEvent(Authenticatable|null $user, array $credentials)
Fire the failed authentication attempt event.
at line 685
protected void
fireLoginEvent(Authenticatable $user)
Fire the login event.
at line 693
protected void
fireAuthenticatedEvent(Authenticatable $user)
Fire the authenticated event.
at line 701
protected void
fireLogoutEvent(Authenticatable|null $user)
Fire the logout event.
at line 709
static void
flushState()
Flush all static state.