class SessionGuard implements StatefulGuard, SupportsBasicAuth

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

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.

logoutOtherDevices(string $password)

Invalidate other sessions for the current user.

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.

string
getContextKey()

Get the Context key for caching the authenticated user.

string
getUnstartedContextKey()

Get the Context key for user set before session starts.

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.

string
getContextStateKey(string $key)

Get a per-request state Context key.

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 216
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 89
__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 115
Authenticatable|null user()

Get the currently authenticated user.

Uses coroutine Context to cache the resolved user per-request, since this guard is a process-global singleton. A sentinel value caches "no user found" so repeated calls don't trigger redundant provider lookups.

Return Value

Authenticatable|null

at line 180
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 204
protected Recaller|null recaller()

Get the decrypted recaller cookie for the request.

Return Value

Recaller|null

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

Log a user into the application without sessions or cookies.

Parameters

array $credentials

Return Value

bool

at line 250
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 264
bool validate(array $credentials = [])

Validate a user's credentials.

Parameters

array $credentials

Return Value

bool

at line 287
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 308
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 322
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 337
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 347
protected never failedBasicResponse()

Get the response for basic authentication.

Return Value

never

Exceptions

UnauthorizedHttpException

at line 355
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 390
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 422
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 436
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 450
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 460
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 474
void login(Authenticatable $user, bool $remember = false)

Log a user into the application.

Parameters

Authenticatable $user
bool $remember

Return Value

void

at line 498
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 508
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 518
protected void queueRecallerCookie(Authenticatable $user)

Queue the recaller cookie into the cookie jar.

Parameters

Authenticatable $user

Return Value

void

at line 530
protected Cookie createRecaller(string $value)

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

Parameters

string $value

Return Value

Cookie

at line 538
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 550
void logout()

Log the user out of the application.

Return Value

void

at line 578
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 600
protected void clearUserDataFromStorage()

Remove the user data from the session and cookies.

Return Value

void

at line 616
protected void cycleRememberToken(Authenticatable $user)

Refresh the "remember me" token for the user.

Parameters

Authenticatable $user

Return Value

void

at line 630
Authenticatable|null logoutOtherDevices(string $password)

Invalidate other sessions for the current user.

The application must be using the AuthenticateSession middleware.

Parameters

string $password

Return Value

Authenticatable|null

Exceptions

AuthenticationException

at line 653
protected Authenticatable|null rehashUserPasswordForDeviceLogout(string $password)

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

Parameters

string $password

Return Value

Authenticatable|null

Exceptions

InvalidArgumentException

at line 676
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 684
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 692
protected void fireValidatedEvent(Authenticatable $user)

Fire the validated event if the dispatcher is set.

Parameters

Authenticatable $user

Return Value

void

at line 700
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 708
protected void fireAuthenticatedEvent(Authenticatable $user)

Fire the authenticated event if the dispatcher is set.

Parameters

Authenticatable $user

Return Value

void

at line 716
protected void fireOtherDeviceLogoutEvent(Authenticatable $user)

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

Parameters

Authenticatable $user

Return Value

void

at line 724
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 732
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 742
Authenticatable|null getLastAttempted()

Get the last user we attempted to authenticate.

Return Value

Authenticatable|null

at line 750
string getName()

Get a unique identifier for the auth session value.

Return Value

string

at line 758
string getRecallerName()

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

Return Value

string

at line 766
bool viaRemember()

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

Return Value

bool

at line 774
protected int getRememberDuration()

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

Return Value

int

at line 785
SessionGuard setRememberDuration(int $minutes)

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

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

Parameters

int $minutes

Return Value

SessionGuard

at line 797
QueueingFactory getCookieJar()

Get the cookie creator instance used by the guard.

Return Value

QueueingFactory

Exceptions

RuntimeException

at line 812
void setCookieJar(QueueingFactory $cookie)

Set the cookie creator instance used by the guard.

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

Parameters

QueueingFactory $cookie

Return Value

void

at line 820
Dispatcher|null getDispatcher()

Get the event dispatcher instance.

Return Value

Dispatcher|null

at line 831
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 839
Session getSession()

Get the session store used by the guard.

Return Value

Session

at line 847
Authenticatable|null getUser()

Return the currently cached user.

Return Value

Authenticatable|null

at line 855
bool hasUser()

Determine if the guard has a user instance.

Return Value

bool

at line 877
Guard setUser(Authenticatable $user)

Set the current user.

Uses coroutine Context for per-request isolation. Routes to the "unstarted" key if the session hasn't started yet (e.g. during middleware before session initialization).

Parameters

Authenticatable $user

Return Value

Guard

at line 895
SessionGuard forgetUser()

Forget the current user.

Return Value

SessionGuard

at line 911
array getAuthContextKeys()

Get durable authentication Context keys for the current guard.

Transient request state such as remember-cookie attempts and last-attempted users is intentionally excluded.

Return Value

array

at line 926
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 934
Timebox getTimebox()

Get the timebox instance used by the guard.

Return Value

Timebox

at line 942
protected string getContextKey()

Get the Context key for caching the authenticated user.

Return Value

string

at line 950
protected string getUnstartedContextKey()

Get the Context key for user set before session starts.

Return Value

string

at line 958
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 966
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 974
protected string getContextStateKey(string $key)

Get a per-request state Context key.

Parameters

string $key

Return Value

string

at line 982
static void flushState()

Flush all static state.

Return Value

void