JwtGuard
class JwtGuard implements Guard
Traits
These methods are typically the same across all guards.
Constants
| 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.
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.
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 260
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 56
__construct(string $name, UserProvider $provider, ManagerContract $jwtManager, ClaimFactory $claimFactory, Parser $parser, Container $app, int|null $ttl = 120)
Create a new JWT authentication guard.
at line 71
string|bool
attempt(array $credentials = [], bool $login = true)
Attempt to authenticate a user using the given credentials.
at line 92
string|null
parseToken()
Parse the JWT token from the current request.
at line 104
string
login(Authenticatable $user)
Log a user into the application and return the JWT token.
at line 118
Authenticatable|null
user()
Get the currently authenticated user.
at line 168
bool
validate(array $credentials = [])
Validate a user's credentials.
at line 176
bool
once(array $credentials = [])
Log a user into the application using their credentials without persisting.
at line 190
Authenticatable|false
onceUsingId(mixed $id)
Log the given user ID into the application.
at line 204
string|null
tokenById(mixed $id)
Create a new token by user ID.
at line 216
Authenticatable|false
byId(mixed $id)
Alias for onceUsingId.
at line 226
Authenticatable
userOrFail()
Get the currently authenticated user or throw an exception.
at line 238
int|string|null
getUserId()
Get the ID for the currently authenticated user.
at line 268
JwtGuard
claims(array $claims)
Add custom claims to the next JWT token.
at line 281
array
getPayload()
Get the payload from the current JWT token.
at line 295
array
payload()
Alias for getPayload.
at line 303
JwtGuard
setToken(string $token)
Set the token.
at line 313
string|null
getToken()
Get the current token.
at line 323
int|null
getTTL()
Get the token TTL.
at line 337
JwtGuard
setTTL(int|null $ttl)
Set the token TTL for the next token-producing operation.
at line 347
string|null
refresh(bool $forceForever = false, bool $resetClaims = false)
Refresh the current JWT token.
at line 377
void
logout(bool $forceForever = false)
Log the user out by invalidating the current token.
at line 399
JwtGuard
invalidate(bool $forceForever = false)
Invalidate the current token.
at line 409
bool
hasUser()
Determine if the guard has a user instance.
at line 421
Guard
setUser(Authenticatable $user)
Set the current user.
at line 432
JwtGuard
forgetUser()
Forget the current user.
at line 446
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 454
Dispatcher|null
getDispatcher()
Get the event dispatcher instance.
at line 465
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 473
Authenticatable|null
getLastAttempted()
Get the last user we attempted to authenticate.
at line 481
protected string
makeTokenForUser(Authenticatable $user)
Create a token for the given user.
at line 500
protected array
decodeToken(string $token)
Decode a JWT token, caching the result per coroutine.
at line 511
protected Authenticatable|null
cachedUser()
Return the currently cached user.
at line 523
protected void
cacheUser(Authenticatable $user)
Cache the current user without firing guard events.
at line 531
protected array
pullCustomClaims()
Pull custom claims for the next token.
at line 545
protected string
requireToken()
Require a token to be available.
at line 557
protected mixed
getContextState(string $key, mixed $default = null)
Get Context state.
at line 565
protected void
setContextState(string $key, mixed $value)
Set Context state.
at line 573
protected void
forgetContextState(string $key)
Forget Context state.
at line 581
protected string
getContextStateKey(string $key)
Get a Context state key.
at line 589
protected string
getUserContextKey(string|null $token = null)
Get the Context key for caching the authenticated user.
at line 603
protected string
getPayloadContextKey(string $token)
Get the Context key for caching a decoded payload.
at line 611
protected void
dispatchIfListening(string $eventClass, Closure $event)
Dispatch the given event if listeners are registered.
at line 621
protected void
fireAttemptEvent(array $credentials)
Fire the attempt event.
at line 629
protected void
fireValidatedEvent(Authenticatable $user)
Fire the validated event.
at line 637
protected void
fireFailedEvent(Authenticatable|null $user, array $credentials)
Fire the failed authentication attempt event.
at line 645
protected void
fireLoginEvent(Authenticatable $user)
Fire the login event.
at line 653
protected void
fireAuthenticatedEvent(Authenticatable $user)
Fire the authenticated event.
at line 661
protected void
fireLogoutEvent(Authenticatable|null $user)
Fire the logout event.
at line 669
static void
flushState()
Flush all static state.