class TokenGuard implements Guard

Traits

These methods are typically the same across all guards.

Properties

protected UserProvider|null $provider

The user provider implementation.

from  GuardHelpers
static protected array $macros

The registered string macros.

from  Macroable

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, Container $app, string $inputKey = 'api_token', string $storageKey = 'api_token', bool $hash = false)

Create a new authentication guard.

user()

Get the currently authenticated user.

string|null
getTokenForRequest()

Get the token for the current request.

bool
validate(array $credentials = [])

Validate a user's credentials.

bool
hasUser()

Determine if the guard has a user instance.

setUser(Authenticatable $user)

Set the current user.

forgetUser()

Forget the current user.

array
getAuthContextKeys()

Get durable authentication Context keys for the current request.

string
getContextKeyForToken(string|null $token)

Get the Context key for caching the authenticated user, keyed by token.

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

in GuardHelpers at line 56
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 33
__construct(string $name, UserProvider $provider, Container $app, string $inputKey = 'api_token', string $storageKey = 'api_token', bool $hash = false)

Create a new authentication guard.

Parameters

string $name

the name of the guard for Context keying

UserProvider $provider
Container $app
string $inputKey

the name of the query string item from the request containing the API token

string $storageKey

the name of the token "column" in persistent storage

bool $hash

indicates if the API token is hashed in storage

at line 52
Authenticatable|null user()

Get the currently authenticated user.

Uses coroutine Context to cache the resolved user per-request, keyed by token fingerprint so different tokens don't collide. A sentinel value caches "no user found" so repeated calls don't trigger redundant provider lookups.

Return Value

Authenticatable|null

at line 84
string|null getTokenForRequest()

Get the token for the current request.

Return Value

string|null

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

Validate a user's credentials.

Parameters

array $credentials

Return Value

bool

at line 122
bool hasUser()

Determine if the guard has a user instance.

Return Value

bool

at line 134
Guard setUser(Authenticatable $user)

Set the current user.

Parameters

Authenticatable $user

Return Value

Guard

at line 144
TokenGuard forgetUser()

Forget the current user.

Return Value

TokenGuard

at line 156
array getAuthContextKeys()

Get durable authentication Context keys for the current request.

Return Value

array

at line 164
protected string getContextKeyForToken(string|null $token)

Get the Context key for caching the authenticated user, keyed by token.

Parameters

string|null $token

Return Value

string

at line 176
static void flushState()

Flush all static state.

Return Value

void