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

authenticate()

Determine if the current user is authenticated. If not, throw an exception.

bool
check()

Determine if the current user is authenticated.

bool
guest()

Determine if the current user is a guest.

int|string|null
id()

Get the ID for the currently authenticated user.

getProvider()

Get the user provider used by the guard.

void
setProvider(UserProvider $provider)

Set the user provider used by the guard.

static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

__construct(string $name, UserProvider $provider, ManagerContract $jwtManager, ClaimFactory $claimFactory, Parser $parser, Container $app, int|null $ttl = 120)

Create a new JWT authentication guard.

string|bool
attempt(array $credentials = [], bool $login = true)

Attempt to authenticate a user using the given credentials.

string|null
parseToken()

Parse the JWT token from the current request.

string
login(Authenticatable $user)

Log a user into the application and return the JWT token.

user()

Get the currently authenticated user.

bool
validate(array $credentials = [])

Validate a user's credentials.

bool
once(array $credentials = [])

Log a user into the application using their credentials without persisting.

onceUsingId(mixed $id)

Log the given user ID into the application.

string|null
tokenById(mixed $id)

Create a new token by user ID.

byId(mixed $id)

Alias for onceUsingId.

userOrFail()

Get the currently authenticated user or throw an exception.

int|string|null
getUserId()

Get the ID for the currently authenticated user.

claims(array $claims)

Add custom claims to the next JWT token.

array
getPayload()

Get the payload from the current JWT token.

array
payload()

Alias for getPayload.

setToken(string $token)

Set the token.

string|null
getToken()

Get the current token.

int|null
getTTL()

Get the token TTL.

setTTL(int|null $ttl)

Set the token TTL for the next token-producing operation.

string|null
refresh(bool $forceForever = false, bool $resetClaims = false)

Refresh the current JWT token.

void
logout(bool $forceForever = false)

Log the user out by invalidating the current token.

invalidate(bool $forceForever = false)

Invalidate the current token.

bool
hasUser()

Determine if the guard has a user instance.

setUser(Authenticatable $user)

Set the current user.

forgetUser()

Forget the current user.

void
attempting(callable $callback)

Register an authentication attempt event listener.

Dispatcher|null
getDispatcher()

Get the event dispatcher instance.

void
setDispatcher(Dispatcher $events)

Set the event dispatcher instance.

getLastAttempted()

Get the last user we attempted to authenticate.

string
makeTokenForUser(Authenticatable $user)

Create a token for the given user.

array
decodeToken(string $token)

Decode a JWT token, caching the result per coroutine.

cachedUser()

Return the currently cached user.

void
cacheUser(Authenticatable $user)

Cache the current user without firing guard events.

array
pullCustomClaims()

Pull custom claims for the next token.

string
requireToken()

Require a token to be available.

mixed
getContextState(string $key, mixed $default = null)

Get Context state.

void
setContextState(string $key, mixed $value)

Set Context state.

void
forgetContextState(string $key)

Forget Context state.

string
getContextStateKey(string $key)

Get a Context state key.

string
getUserContextKey(string|null $token = null)

Get the Context key for caching the authenticated user.

string
getPayloadContextKey(string $token)

Get the Context key for caching a decoded payload.

void
dispatchIfListening(string $eventClass, Closure $event)

Dispatch the given event if listeners are registered.

void
fireAttemptEvent(array $credentials)

Fire the attempt event.

void
fireValidatedEvent(Authenticatable $user)

Fire the validated event.

void
fireFailedEvent(Authenticatable|null $user, array $credentials)

Fire the failed authentication attempt event.

void
fireLoginEvent(Authenticatable $user)

Fire the login event.

void
fireAuthenticatedEvent(Authenticatable $user)

Fire the authenticated event.

void
fireLogoutEvent(Authenticatable|null $user)

Fire the logout event.

static void
flushState()

Flush all static state.

Details

in GuardHelpers at line 32
Authenticatable authenticate()

Determine if the current user is authenticated. If not, throw an exception.

Return Value

Authenticatable

Exceptions

AuthenticationException

in GuardHelpers at line 40
bool check()

Determine if the current user is authenticated.

Return Value

bool

in GuardHelpers at line 48
bool guest()

Determine if the current user is a guest.

Return Value

bool

at line 260
int|string|null id()

Get the ID for the currently authenticated user.

Return Value

int|string|null

in GuardHelpers at line 64
UserProvider|null getProvider()

Get the user provider used by the guard.

Return Value

UserProvider|null

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.

Parameters

UserProvider $provider

Return Value

void

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.

Parameters

string $name
callable|object $macro

Return Value

void

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.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

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.

Return Value

void

in Macroable at line 78
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Macroable at line 103
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

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.

Parameters

string $name
UserProvider $provider
ManagerContract $jwtManager
ClaimFactory $claimFactory
Parser $parser
Container $app
int|null $ttl

token time-to-live in minutes, or null for no expiration

at line 71
string|bool attempt(array $credentials = [], bool $login = true)

Attempt to authenticate a user using the given credentials.

Parameters

array $credentials
bool $login

Return Value

string|bool

at line 92
string|null parseToken()

Parse the JWT token from the current request.

Return Value

string|null

at line 104
string login(Authenticatable $user)

Log a user into the application and return the JWT token.

Parameters

Authenticatable $user

Return Value

string

at line 118
Authenticatable|null user()

Get the currently authenticated user.

Return Value

Authenticatable|null

at line 168
bool validate(array $credentials = [])

Validate a user's credentials.

Parameters

array $credentials

Return Value

bool

at line 176
bool once(array $credentials = [])

Log a user into the application using their credentials without persisting.

Parameters

array $credentials

Return Value

bool

at line 190
Authenticatable|false onceUsingId(mixed $id)

Log the given user ID into the application.

Parameters

mixed $id

Return Value

Authenticatable|false

at line 204
string|null tokenById(mixed $id)

Create a new token by user ID.

Parameters

mixed $id

Return Value

string|null

at line 216
Authenticatable|false byId(mixed $id)

Alias for onceUsingId.

Parameters

mixed $id

Return Value

Authenticatable|false

at line 226
Authenticatable userOrFail()

Get the currently authenticated user or throw an exception.

Return Value

Authenticatable

Exceptions

UserNotDefinedException

at line 238
int|string|null getUserId()

Get the ID for the currently authenticated user.

Return Value

int|string|null

at line 268
JwtGuard claims(array $claims)

Add custom claims to the next JWT token.

Parameters

array $claims

Return Value

JwtGuard

at line 281
array getPayload()

Get the payload from the current JWT token.

Return Value

array

at line 295
array payload()

Alias for getPayload.

Return Value

array

at line 303
JwtGuard setToken(string $token)

Set the token.

Parameters

string $token

Return Value

JwtGuard

at line 313
string|null getToken()

Get the current token.

Return Value

string|null

at line 323
int|null getTTL()

Get the token TTL.

Return Value

int|null

at line 337
JwtGuard setTTL(int|null $ttl)

Set the token TTL for the next token-producing operation.

Parameters

int|null $ttl

Return Value

JwtGuard

at line 347
string|null refresh(bool $forceForever = false, bool $resetClaims = false)

Refresh the current JWT token.

Parameters

bool $forceForever
bool $resetClaims

Return Value

string|null

at line 377
void logout(bool $forceForever = false)

Log the user out by invalidating the current token.

Parameters

bool $forceForever

Return Value

void

at line 399
JwtGuard invalidate(bool $forceForever = false)

Invalidate the current token.

Parameters

bool $forceForever

Return Value

JwtGuard

at line 409
bool hasUser()

Determine if the guard has a user instance.

Return Value

bool

at line 421
Guard setUser(Authenticatable $user)

Set the current user.

Parameters

Authenticatable $user

Return Value

Guard

at line 432
JwtGuard forgetUser()

Forget the current user.

Return Value

JwtGuard

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.

Parameters

callable $callback

Return Value

void

at line 454
Dispatcher|null getDispatcher()

Get the event dispatcher instance.

Return Value

Dispatcher|null

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.

Parameters

Dispatcher $events

Return Value

void

at line 473
Authenticatable|null getLastAttempted()

Get the last user we attempted to authenticate.

Return Value

Authenticatable|null

at line 481
protected string makeTokenForUser(Authenticatable $user)

Create a token for the given user.

Parameters

Authenticatable $user

Return Value

string

at line 500
protected array decodeToken(string $token)

Decode a JWT token, caching the result per coroutine.

Parameters

string $token

Return Value

array

at line 511
protected Authenticatable|null cachedUser()

Return the currently cached user.

Return Value

Authenticatable|null

at line 523
protected void cacheUser(Authenticatable $user)

Cache the current user without firing guard events.

Parameters

Authenticatable $user

Return Value

void

at line 531
protected array pullCustomClaims()

Pull custom claims for the next token.

Return Value

array

at line 545
protected string requireToken()

Require a token to be available.

Return Value

string

Exceptions

JwtException

at line 557
protected mixed getContextState(string $key, mixed $default = null)

Get Context state.

Parameters

string $key
mixed $default

Return Value

mixed

at line 565
protected void setContextState(string $key, mixed $value)

Set Context state.

Parameters

string $key
mixed $value

Return Value

void

at line 573
protected void forgetContextState(string $key)

Forget Context state.

Parameters

string $key

Return Value

void

at line 581
protected string getContextStateKey(string $key)

Get a Context state key.

Parameters

string $key

Return Value

string

at line 589
protected string getUserContextKey(string|null $token = null)

Get the Context key for caching the authenticated user.

Parameters

string|null $token

Return Value

string

at line 603
protected string getPayloadContextKey(string $token)

Get the Context key for caching a decoded payload.

Parameters

string $token

Return Value

string

at line 611
protected void dispatchIfListening(string $eventClass, Closure $event)

Dispatch the given event if listeners are registered.

Parameters

string $eventClass
Closure $event

Return Value

void

at line 621
protected void fireAttemptEvent(array $credentials)

Fire the attempt event.

Parameters

array $credentials

Return Value

void

at line 629
protected void fireValidatedEvent(Authenticatable $user)

Fire the validated event.

Parameters

Authenticatable $user

Return Value

void

at line 637
protected void fireFailedEvent(Authenticatable|null $user, array $credentials)

Fire the failed authentication attempt event.

Parameters

Authenticatable|null $user
array $credentials

Return Value

void

at line 645
protected void fireLoginEvent(Authenticatable $user)

Fire the login event.

Parameters

Authenticatable $user

Return Value

void

at line 653
protected void fireAuthenticatedEvent(Authenticatable $user)

Fire the authenticated event.

Parameters

Authenticatable $user

Return Value

void

at line 661
protected void fireLogoutEvent(Authenticatable|null $user)

Fire the logout event.

Parameters

Authenticatable|null $user

Return Value

void

at line 669
static void flushState()

Flush all static state.

Return Value

void