class SessionGuard implements StatefulGuard, SupportsBasicAuth

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
protected int $rememberDuration

The number of minutes that the "remember me" cookie should be valid for.

$cookie

The Illuminate cookie creator service.

protected Dispatcher|null $events

The event dispatcher instance.

protected Timebox $timebox

The timebox 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.

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, Session $session, Container $app, Timebox|null $timebox = null, bool $rehashOnLogin = true, int $timeboxDuration = 200000, string|null $hashKey = null)

Create a new authentication guard.

user()

Get the currently authenticated user.

userFromRecaller(Recaller $recaller)

Pull a user from the repository by its "remember me" cookie token.

Recaller|null
recaller()

Get the decrypted recaller cookie for the request.

bool
once(array $credentials = [])

Log a user into the application without sessions or cookies.

onceUsingId(mixed $id)

Log the given user ID into the application without sessions or cookies.

bool
validate(array $credentials = [])

Validate a user's credentials.

Response|null
basic(string $field = 'email', array $extraConditions = [])

Attempt to authenticate using HTTP Basic Auth.

Response|null
onceBasic(string $field = 'email', array $extraConditions = [])

Perform a stateless HTTP Basic login attempt.

bool
attemptBasic(Request $request, string $field, array $extraConditions = [])

Attempt to authenticate using basic authentication.

array
basicCredentials(Request $request, string $field)

Get the credential array for an HTTP Basic request.

never
failedBasicResponse()

Get the response for basic authentication.

bool
attempt(array $credentials = [], bool $remember = false)

Attempt to authenticate a user using the given credentials.

bool
attemptWhen(array $credentials = [], array|callable|null $callbacks = null, bool $remember = false)

Attempt to authenticate a user with credentials and additional callbacks.

bool
hasValidCredentials(Authenticatable|null $user, array $credentials)

Determine if the user matches the credentials.

bool
shouldLogin(array|callable|null $callbacks, Authenticatable $user)

Determine if the user should login by executing the given callbacks.

void
rehashPasswordIfRequired(Authenticatable $user, array $credentials)

Rehash the user's password if enabled and required.

loginUsingId(mixed $id, bool $remember = false)

Log the given user ID into the application.

void
login(Authenticatable $user, bool $remember = false)

Log a user into the application.

void
updateSession(mixed $id)

Update the session with the given ID and regenerate the session's token.

void
ensureRememberTokenIsSet(Authenticatable $user)

Create a new "remember me" token for the user if one doesn't already exist.

void
queueRecallerCookie(Authenticatable $user)

Queue the recaller cookie into the cookie jar.

Cookie
createRecaller(string $value)

Create a "remember me" cookie for a given ID.

string
hashPasswordForCookie(string|null $passwordHash)

Create a HMAC of the password hash for storage in cookies.

void
logout()

Log the user out of the application.

void
logoutCurrentDevice()

Log the user out of the application on their current device only.

void
clearUserDataFromStorage()

Remove the user data from the session and cookies.

void
cycleRememberToken(Authenticatable $user)

Refresh the "remember me" token for the user.

void
logoutOtherDevices(string $password)

Invalidate other sessions for the current user.

void
rehashUserPasswordForDeviceLogout(string $password)

Rehash the current user's password for logging out other devices via AuthenticateSession.

void
attempting(callable $callback)

Register an authentication attempt event listener.

void
fireAttemptEvent(array $credentials, bool $remember = false)

Fire the attempt event with the arguments.

void
fireValidatedEvent(Authenticatable $user)

Fire the validated event if the dispatcher is set.

void
fireLoginEvent(Authenticatable $user, bool $remember = false)

Fire the login event if the dispatcher is set.

void
fireAuthenticatedEvent(Authenticatable $user)

Fire the authenticated event if the dispatcher is set.

void
fireOtherDeviceLogoutEvent(Authenticatable $user)

Fire the other device logout event if the dispatcher is set.

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

Fire the failed authentication attempt event with the given arguments.

void
dispatchIfListening(string $eventClass, Closure $event)

Dispatch the given event if listeners are registered.

getLastAttempted()

Get the last user we attempted to authenticate.

string
getName()

Get a unique identifier for the auth session value.

string
getRecallerName()

Get the name of the cookie used to store the "recaller".

bool
viaRemember()

Determine if the user was authenticated via "remember me" cookie.

int
getRememberDuration()

Get the number of minutes the remember me cookie should be valid for.

setRememberDuration(int $minutes)

Set the number of minutes the remember me cookie should be valid for.

getCookieJar()

Get the cookie creator instance used by the guard.

void
setCookieJar(QueueingFactory $cookie)

Set the cookie creator instance used by the guard.

Dispatcher|null
getDispatcher()

Get the event dispatcher instance.

void
setDispatcher(Dispatcher $events)

Set the event dispatcher instance.

getSession()

Get the session store used by the guard.

getUser()

Return the currently cached user.

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 guard.

Request
getRequest()

Get the current request instance.

getTimebox()

Get the timebox instance used by the guard.

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

Get a per-request state value from Context.

void
setContextState(string $key, mixed $value)

Set a per-request state value in Context.

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

at line 236
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 99
__construct(string $name, UserProvider $provider, Session $session, Container $app, Timebox|null $timebox = null, bool $rehashOnLogin = true, int $timeboxDuration = 200000, string|null $hashKey = null)

Create a new authentication guard.

Parameters

string $name

The name of the guard. Typically "web".

UserProvider $provider
Session $session

the session used by the guard

Container $app

the container instance for lazy request resolution

Timebox|null $timebox
bool $rehashOnLogin

indicates if passwords should be rehashed on login if needed

int $timeboxDuration

the number of microseconds that the timebox should wait for

string|null $hashKey

the key used to hash recaller cookie values

at line 126
Authenticatable|null user()

Get the currently authenticated user.

The resolved user survives session rotation within the coroutine. Failed lookups are cached only for the session state they inspected.

Return Value

Authenticatable|null

at line 192
protected Authenticatable|null userFromRecaller(Recaller $recaller)

Pull a user from the repository by its "remember me" cookie token.

Parameters

Recaller $recaller

Return Value

Authenticatable|null

at line 224
protected Recaller|null recaller()

Get the decrypted recaller cookie for the request.

Return Value

Recaller|null

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

Log a user into the application without sessions or cookies.

Parameters

array $credentials

Return Value

bool

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

Log the given user ID into the application without sessions or cookies.

Parameters

mixed $id

Return Value

Authenticatable|false

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

Validate a user's credentials.

Parameters

array $credentials

Return Value

bool

at line 307
Response|null basic(string $field = 'email', array $extraConditions = [])

Attempt to authenticate using HTTP Basic Auth.

Parameters

string $field
array $extraConditions

Return Value

Response|null

Exceptions

UnauthorizedHttpException

at line 328
Response|null onceBasic(string $field = 'email', array $extraConditions = [])

Perform a stateless HTTP Basic login attempt.

Parameters

string $field
array $extraConditions

Return Value

Response|null

Exceptions

UnauthorizedHttpException

at line 342
protected bool attemptBasic(Request $request, string $field, array $extraConditions = [])

Attempt to authenticate using basic authentication.

Parameters

Request $request
string $field
array $extraConditions

Return Value

bool

at line 357
protected array basicCredentials(Request $request, string $field)

Get the credential array for an HTTP Basic request.

Parameters

Request $request
string $field

Return Value

array

at line 367
protected never failedBasicResponse()

Get the response for basic authentication.

Return Value

never

Exceptions

UnauthorizedHttpException

at line 375
bool attempt(array $credentials = [], bool $remember = false)

Attempt to authenticate a user using the given credentials.

Parameters

array $credentials
bool $remember

Return Value

bool

at line 410
bool attemptWhen(array $credentials = [], array|callable|null $callbacks = null, bool $remember = false)

Attempt to authenticate a user with credentials and additional callbacks.

Parameters

array $credentials
array|callable|null $callbacks
bool $remember

Return Value

bool

at line 442
protected bool hasValidCredentials(Authenticatable|null $user, array $credentials)

Determine if the user matches the credentials.

Parameters

Authenticatable|null $user
array $credentials

Return Value

bool

at line 456
protected bool shouldLogin(array|callable|null $callbacks, Authenticatable $user)

Determine if the user should login by executing the given callbacks.

Parameters

array|callable|null $callbacks
Authenticatable $user

Return Value

bool

at line 470
protected void rehashPasswordIfRequired(Authenticatable $user, array $credentials)

Rehash the user's password if enabled and required.

Parameters

Authenticatable $user
array $credentials

Return Value

void

at line 480
Authenticatable|false loginUsingId(mixed $id, bool $remember = false)

Log the given user ID into the application.

Parameters

mixed $id
bool $remember

Return Value

Authenticatable|false

at line 494
void login(Authenticatable $user, bool $remember = false)

Log a user into the application.

Parameters

Authenticatable $user
bool $remember

Return Value

void

at line 534
protected void updateSession(mixed $id)

Update the session with the given ID and regenerate the session's token.

Parameters

mixed $id

Return Value

void

at line 544
protected void ensureRememberTokenIsSet(Authenticatable $user)

Create a new "remember me" token for the user if one doesn't already exist.

Parameters

Authenticatable $user

Return Value

void

at line 554
protected void queueRecallerCookie(Authenticatable $user)

Queue the recaller cookie into the cookie jar.

Parameters

Authenticatable $user

Return Value

void

at line 566
protected Cookie createRecaller(string $value)

Create a "remember me" cookie for a given ID.

Parameters

string $value

Return Value

Cookie

at line 574
string hashPasswordForCookie(string|null $passwordHash)

Create a HMAC of the password hash for storage in cookies.

Parameters

string|null $passwordHash

Return Value

string

at line 586
void logout()

Log the user out of the application.

Return Value

void

at line 614
void logoutCurrentDevice()

Log the user out of the application on their current device only.

This method does not cycle the "remember" token.

Return Value

void

at line 636
protected void clearUserDataFromStorage()

Remove the user data from the session and cookies.

Return Value

void

at line 652
protected void cycleRememberToken(Authenticatable $user)

Refresh the "remember me" token for the user.

Parameters

Authenticatable $user

Return Value

void

at line 666
void logoutOtherDevices(string $password)

Invalidate other sessions for the current user.

The application must be using the AuthenticateSession middleware.

Parameters

string $password

Return Value

void

Exceptions

AuthenticationException

at line 692
protected void rehashUserPasswordForDeviceLogout(string $password)

Rehash the current user's password for logging out other devices via AuthenticateSession.

Parameters

string $password

Return Value

void

Exceptions

InvalidArgumentException

at line 713
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.

Parameters

callable $callback

Return Value

void

at line 721
protected void fireAttemptEvent(array $credentials, bool $remember = false)

Fire the attempt event with the arguments.

Parameters

array $credentials
bool $remember

Return Value

void

at line 729
protected void fireValidatedEvent(Authenticatable $user)

Fire the validated event if the dispatcher is set.

Parameters

Authenticatable $user

Return Value

void

at line 737
protected void fireLoginEvent(Authenticatable $user, bool $remember = false)

Fire the login event if the dispatcher is set.

Parameters

Authenticatable $user
bool $remember

Return Value

void

at line 745
protected void fireAuthenticatedEvent(Authenticatable $user)

Fire the authenticated event if the dispatcher is set.

Parameters

Authenticatable $user

Return Value

void

at line 753
protected void fireOtherDeviceLogoutEvent(Authenticatable $user)

Fire the other device logout event if the dispatcher is set.

Parameters

Authenticatable $user

Return Value

void

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

Fire the failed authentication attempt event with the given arguments.

Parameters

Authenticatable|null $user
array $credentials

Return Value

void

at line 769
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 779
Authenticatable|null getLastAttempted()

Get the last user we attempted to authenticate.

Return Value

Authenticatable|null

at line 787
string getName()

Get a unique identifier for the auth session value.

Return Value

string

at line 795
string getRecallerName()

Get the name of the cookie used to store the "recaller".

Return Value

string

at line 803
bool viaRemember()

Determine if the user was authenticated via "remember me" cookie.

Return Value

bool

at line 811
protected int getRememberDuration()

Get the number of minutes the remember me cookie should be valid for.

Return Value

int

at line 824
SessionGuard setRememberDuration(int $minutes)

Set the number of minutes the remember me cookie should be valid for.

The duration is stored on the worker-lifetime guard and affects every subsequent request.

Parameters

int $minutes

Return Value

SessionGuard

at line 837
QueueingFactory getCookieJar()

Get the cookie creator instance used by the guard.

Return Value

QueueingFactory

Exceptions

RuntimeException

at line 852
void setCookieJar(QueueingFactory $cookie)

Set the cookie creator instance used by the guard.

The cookie jar is stored on the worker-lifetime guard and affects every subsequent request.

Parameters

QueueingFactory $cookie

Return Value

void

at line 861
Dispatcher|null getDispatcher()

Get the event dispatcher instance.

Return Value

Dispatcher|null

at line 872
void setDispatcher(Dispatcher $events)

Set the event dispatcher instance.

The dispatcher is stored on the worker-lifetime guard and affects every subsequent request.

Parameters

Dispatcher $events

Return Value

void

at line 881
Session getSession()

Get the session store used by the guard.

Return Value

Session

at line 889
Authenticatable|null getUser()

Return the currently cached user.

Return Value

Authenticatable|null

at line 897
bool hasUser()

Determine if the guard has a user instance.

Return Value

bool

at line 907
Guard setUser(Authenticatable $user)

Set the current user.

Parameters

Authenticatable $user

Return Value

Guard

at line 923
SessionGuard forgetUser()

Forget the current user.

Return Value

SessionGuard

at line 939
array getAuthContextKeys()

Get durable authentication Context keys for the current guard.

Failed lookups, remember-cookie attempts, and last-attempted users are transient request state and are intentionally excluded.

Return Value

array

at line 953
Request getRequest()

Get the current request instance.

Resolved lazily from the container because this guard is a process-global singleton and must not store per-request state.

Return Value

Request

at line 961
Timebox getTimebox()

Get the timebox instance used by the guard.

Return Value

Timebox

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

Get a per-request state value from Context.

Parameters

string $key
mixed $default

Return Value

mixed

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

Set a per-request state value in Context.

Parameters

string $key
mixed $value

Return Value

void

at line 985
static void flushState()

Flush all static state.

Return Value

void