Overview
TheAuth service provides a complete authentication system with JWT tokens, secure sessions, and automatic token refresh. It handles user login, logout, authentication checking, and token management.
The Auth service uses JWT tokens stored in both sessions and HTTP-only cookies for security. Access tokens expire after 15 minutes, while refresh tokens last 7 days.
Methods
login()
Authenticates a user with email and password credentials.User credentials containing email and password
JWT access token (valid for 15 minutes)
JWT refresh token (valid for 7 days)
Example
Behavior
- Validates email and password are provided
- Verifies password using
password_verify() - Generates JWT access and refresh tokens
- Stores tokens in session (
$_SESSION['user_token'],$_SESSION['refresh_token']) - Sets secure HTTP-only cookies with
StrictSameSite policy - Throws
Exceptionon invalid credentials or missing fields
logout()
Logs out the current user by clearing session and tokens.Success message: “Logged out successfully”
Example
Behavior
- Destroys the current session (
session_unset(),session_destroy()) - Clears
user_tokenandrefresh_tokencookies by setting expiration to past time - Removes all authentication state
user()
Retrieves the currently authenticated user’s data from the token.Optional access token. If not provided, uses token from session or cookie
User ID
User name
Token expiration timestamp
Example
Behavior
- Returns
nullif no valid token is found - Validates the access token using JWT signature and expiration
- Automatically attempts token refresh if access token is expired but refresh token is valid
- Updates session and cookies with new tokens after successful refresh
- Logs out user if refresh token is also invalid
- Returns decoded JWT payload containing user data
check()
Checks if a user is currently authenticated.Optional access token. If not provided, uses token from session or cookie
Returns
true if user is authenticated, false otherwiseExample
Behavior
- Returns
falseif no token is found - Validates the access token
- Automatically attempts token refresh if access token is expired
- Updates session and cookies with new tokens after successful refresh
- Returns boolean indicating authentication status
refresh()
Manually refreshes the access and refresh tokens.New JWT access token (valid for 15 minutes)
New JWT refresh token (valid for 7 days)
Example
Behavior
- Looks for refresh token in session or cookie
- Throws
Exceptionif no refresh token is found - Generates new access and refresh tokens
- Updates session and cookies with new tokens
- Throws
Exceptionif token refresh fails - Returns new token pair
Token Lifetimes
| Token Type | Lifetime | Storage |
|---|---|---|
| Access Token | 15 minutes | Session + Cookie |
| Refresh Token | 7 days | Session + Cookie |
Cookie Security Settings
All cookies are set with the following security options:secure: true- Only transmitted over HTTPShttponly: true- Not accessible via JavaScriptsamesite: 'Strict'- Prevents CSRF attackspath: '/'- Available site-wide
Source Reference
Location:~/workspace/source/Sphp/Services/Auth.php