JwtGuard
class JwtGuard implements Guard
Traits
These methods are typically the same across all guards.
Constants
| DEFAULT_TTL |
|
| private NO_EXPIRY |
|
Properties
| protected null|TProvider | $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 this guard's coroutine context key for the given entry.
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 current user from the token without loading the 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, invalidating the current token when the blacklist is enabled.
Invalidate the current token.
Return the currently cached user.
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.
Rehash the user's password if enabled and required.
Decode a JWT token, caching the result for the current request.
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 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 55
Authenticatable
authenticate()
Determine if the current user is authenticated. If not, throw an exception.
in
GuardHelpers at line 63
bool
check()
Determine if the current user is authenticated.
in
GuardHelpers at line 71
bool
guest()
Determine if the current user is a guest.
in
GuardHelpers at line 79
int|string|null
id()
Get the ID for the currently authenticated user.
in
GuardHelpers at line 89
UserProvider|null
getProvider()
Get the user provider used by the guard.
in
GuardHelpers at line 102
void
setProvider(UserProvider $provider)
Set the user provider used by the guard.
The provider is stored on the worker-lifetime guard and affects every subsequent request.
in
GuardHelpers at line 111
protected string
contextKey(string $key)
Get this guard's coroutine context key for the given entry.
in
Macroable at line 30
static void
macro(string $name, callable|object $macro)
Register a custom macro.
Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.
in
Macroable at line 44
static void
mixin(object $mixin, bool $replace = true)
Mix another object into the class.
Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.
in
Macroable at line 61
static bool
hasMacro(string $name)
Check if macro is registered.
in
Macroable at line 72
static void
flushMacros()
Flush the existing macros.
Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.
in
Macroable at line 83
static mixed
__callStatic(string $method, array $parameters)
Dynamically handle calls to the class.
in
Macroable at line 108
mixed
__call(string $method, array $parameters)
Dynamically handle calls to the class.
at line 61
__construct(string $name, UserProvider $provider, ManagerContract $jwtManager, ClaimFactory $claimFactory, Parser $parser, Container $app, bool $rehashOnLogin, int $timeboxDuration, int|null $ttl = self::DEFAULT_TTL, Timebox $timebox = new Timebox())
Create a new JWT authentication guard.
at line 79
string|bool
attempt(array $credentials = [], bool $login = true)
Attempt to authenticate a user using the given credentials.
at line 112
string|null
parseToken()
Parse the JWT token from the current request.
at line 124
string
login(Authenticatable $user)
Log a user into the application and return the JWT token.
at line 140
Authenticatable|null
user()
Get the currently authenticated user.
at line 198
bool
validate(array $credentials = [])
Validate a user's credentials.
at line 206
bool
once(array $credentials = [])
Log a user into the application using their credentials without persisting.
at line 222
Authenticatable|false
onceUsingId(mixed $id)
Log the given user ID into the application.
at line 236
string
fromUser(Authenticatable $user)
Create a new token for the given user without authenticating them.
at line 255
string|null
tokenById(mixed $id)
Create a new token by user ID.
at line 267
Authenticatable|false
byId(mixed $id)
Alias for onceUsingId.
at line 277
Authenticatable
userOrFail()
Get the currently authenticated user or throw an exception.
at line 289
int|string|null
getUserId()
Get the ID for the current user from the token without loading the user.
at line 311
JwtGuard
claims(array $claims)
Add custom claims to the next JWT token.
at line 324
array
getPayload()
Get the payload from the current JWT token.
at line 338
array
payload()
Alias for getPayload.
at line 346
JwtGuard
setToken(string $token)
Set the token.
at line 356
string|null
getToken()
Get the current token.
at line 371
int|null
getTTL()
Get the token TTL.
at line 385
JwtGuard
setTTL(int|null $ttl)
Set the token TTL for the next token-producing operation.
at line 395
string|null
refresh(bool $forceForever = false, bool $resetClaims = false)
Refresh the current JWT token.
at line 425
void
logout(bool $forceForever = false)
Log the user out, invalidating the current token when the blacklist is enabled.
at line 448
JwtGuard
invalidate(bool $forceForever = false)
Invalidate the current token.
at line 462
Authenticatable|null
getUser()
Return the currently cached user.
at line 481
bool
hasUser()
Determine if the guard has a user instance.
at line 489
Guard
setUser(Authenticatable $user)
Set the current user.
at line 500
JwtGuard
forgetUser()
Forget the current user.
at line 516
array
getAuthContextKeys()
Get durable authentication Context keys.
Per-request token resolution caches must not cross a request boundary.
at line 527
void
attempting(callable $callback)
Register an authentication attempt event listener.
When the dispatcher is the application's worker-shared dispatcher, listener registrations persist and affect every subsequent request.
at line 535
Dispatcher|null
getDispatcher()
Get the event dispatcher instance.
at line 546
void
setDispatcher(Dispatcher $events)
Set the event dispatcher instance.
The dispatcher is stored on the worker-lifetime guard and affects every subsequent request.
at line 555
Authenticatable|null
getLastAttempted()
Get the last user we attempted to authenticate.
at line 563
protected void
rehashPasswordIfRequired(Authenticatable $user, array $credentials)
Rehash the user's password if enabled and required.
at line 573
protected array
decodeToken(string $token)
Decode a JWT token, caching the result for the current request.
at line 584
protected void
cacheUser(Authenticatable $user)
Cache the current user without firing guard events.
at line 592
protected string
getExplicitUserContextKey()
Get the Context key for an explicitly assigned user.
at line 600
protected array
pullCustomClaims()
Pull custom claims for the next token.
at line 614
protected string
requireToken()
Require a token to be available.
at line 626
protected mixed
getContextState(string $key, mixed $default = null)
Get Context state.
at line 634
protected void
setContextState(string $key, mixed $value)
Set Context state.
at line 642
protected void
forgetContextState(string $key)
Forget Context state.
at line 650
protected string
getUserContextKey(string|null $token = null)
Get the Context key for caching the authenticated user.
at line 664
protected string
getPayloadContextKey(string $token)
Get the Context key for caching a decoded payload.
at line 672
protected void
dispatchIfListening(string $eventClass, Closure $event)
Dispatch the given event if listeners are registered.
at line 682
protected void
fireAttemptEvent(array $credentials)
Fire the attempt event.
at line 690
protected void
fireValidatedEvent(Authenticatable $user)
Fire the validated event.
at line 698
protected void
fireFailedEvent(Authenticatable|null $user, array $credentials)
Fire the failed authentication attempt event.
at line 706
protected void
fireLoginEvent(Authenticatable $user)
Fire the login event.
at line 714
protected void
fireAuthenticatedEvent(Authenticatable $user)
Fire the authenticated event.
at line 722
protected void
fireLogoutEvent(Authenticatable|null $user)
Fire the logout event.
at line 730
static void
flushState()
Flush all static state.