class SanctumGuard implements Guard

Sanctum authentication guard.

Implements GuardContract directly instead of using Laravel's RequestGuard wrapper. This is intentional: RequestGuard stores user state on $this->user which is process-global and unsafe under Swoole. This guard uses coroutine Context for per-request user caching, keyed by token fingerprint.

Token lookup and tokenable resolution are delegated to PersonalAccessToken, which owns all cache logic (token caching, tokenable caching, last_used_at write throttling). This keeps caching co-located with the model rather than split across the guard and model.

Traits

These methods are typically the same across all guards.

Properties

protected null|TProvider $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.

string
contextKey(string $key)

Get this guard's coroutine context key for the given entry.

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, array $sessionGuards, Dispatcher|null $events = null, int|null $expiration = null, bool $trackLastUsedAt = true)

Create a new guard instance.

user()

Get the currently authenticated user.

bool
supportsTokens(Authenticatable|null $tokenable = null)

Determine if the tokenable model supports API tokens.

string|null
getTokenFromRequest()

Get the token from the request.

bool
isValidAccessToken(PersonalAccessToken|null $accessToken)

Determine if the provided access token is valid.

bool
hasValidProvider(Authenticatable|null $tokenable)

Determine if the tokenable model matches the provider's model type.

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.

bool
validate(array $credentials = [])

Validate a user's credentials (not supported for token-based auth).

string
getExplicitUserContextKey()

Get the Context key for an explicitly assigned user.

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 55
Authenticatable authenticate()

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

Return Value

Authenticatable

Exceptions

AuthenticationException

in GuardHelpers at line 63
bool check()

Determine if the current user is authenticated.

Return Value

bool

in GuardHelpers at line 71
bool guest()

Determine if the current user is a guest.

Return Value

bool

in GuardHelpers at line 79
int|string|null id()

Get the ID for the currently authenticated user.

Return Value

int|string|null

in GuardHelpers at line 89
UserProvider|null getProvider()

Get the user provider used by the guard.

Return Value

UserProvider|null

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.

Parameters

UserProvider $provider

Return Value

void

in GuardHelpers at line 111
protected string contextKey(string $key)

Get this guard's coroutine context key for the given entry.

Parameters

string $key

Return Value

string

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.

Parameters

string $name
callable|object $macro

Return Value

void

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.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

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

Check if macro is registered.

Parameters

string $name

Return Value

bool

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.

Return Value

void

in Macroable at line 83
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 108
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 53
__construct(string $name, UserProvider $provider, Container $app, array $sessionGuards, Dispatcher|null $events = null, int|null $expiration = null, bool $trackLastUsedAt = true)

Create a new guard instance.

Parameters

string $name
UserProvider $provider
Container $app
array $sessionGuards
Dispatcher|null $events
int|null $expiration

the number of minutes tokens should be allowed to remain valid

bool $trackLastUsedAt

at line 72
Authenticatable|null user()

Get the currently authenticated user.

RequestContext keeps resolved users local to the coroutine and clears them when its request changes. Token fingerprints separate credentials, and a sentinel caches "no user found" to avoid redundant lookups.

Return Value

Authenticatable|null

at line 162
protected bool supportsTokens(Authenticatable|null $tokenable = null)

Determine if the tokenable model supports API tokens.

Parameters

Authenticatable|null $tokenable

Return Value

bool

at line 170
protected string|null getTokenFromRequest()

Get the token from the request.

Return Value

string|null

at line 190
protected bool isValidAccessToken(PersonalAccessToken|null $accessToken)

Determine if the provided access token is valid.

Parameters

PersonalAccessToken|null $accessToken

Return Value

bool

at line 212
protected bool hasValidProvider(Authenticatable|null $tokenable)

Determine if the tokenable model matches the provider's model type.

Parameters

Authenticatable|null $tokenable

Return Value

bool

at line 226
bool hasUser()

Determine if the guard has a user instance.

Return Value

bool

at line 242
Guard setUser(Authenticatable $user)

Set the current user.

Parameters

Authenticatable $user

Return Value

Guard

at line 252
SanctumGuard forgetUser()

Forget the current user.

Return Value

SanctumGuard

at line 267
array getAuthContextKeys()

Get durable authentication Context keys.

Per-request token resolution caches must not cross a request boundary.

Return Value

array

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

Validate a user's credentials (not supported for token-based auth).

Parameters

array $credentials

Return Value

bool

at line 283
protected string getExplicitUserContextKey()

Get the Context key for an explicitly assigned user.

Return Value

string

at line 291
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 303
static void flushState()

Flush all static state.

Return Value

void