Overview
TheAuth service in S-PHP provides a complete JWT-based authentication system with support for access tokens, refresh tokens, secure sessions, and cookie management.
Authentication Methods
TheAuth class provides static methods for user authentication:
login()- Authenticate users and generate tokenslogout()- Clear user session and tokensuser()- Get the currently authenticated usercheck()- Verify if a user is authenticatedrefresh()- Refresh expired access tokens
Login Method
Thelogin() method authenticates a user and returns JWT tokens.
Associative array containing ‘email’ and ‘password’ keys
Sphp/Services/Auth.php
Login Example
app/Controllers/AuthController.php
Login Return Value
Logout Method
Thelogout() method clears the user’s session and removes authentication cookies.
No parameters required
Sphp/Services/Auth.php
Logout Example
User Method
Theuser() method retrieves the currently authenticated user, automatically refreshing tokens if needed.
Optional access token to validate. If not provided, checks session and cookies.
Sphp/Services/Auth.php
User Method Example
Check Method
Thecheck() method verifies if a user is currently authenticated.
Optional access token to validate. If not provided, checks session and cookies.
Sphp/Services/Auth.php
Check Method Example
Token Lifecycle
Access Token
- Lifetime: 15 minutes
- Purpose: Short-lived token for API requests
- Storage: Session and HTTP-only cookie
Refresh Token
- Lifetime: 7 days
- Purpose: Long-lived token to obtain new access tokens
- Storage: Session and HTTP-only cookie
Automatic Token Refresh
When an access token expires, theuser() and check() methods automatically:
- Detect the expired access token
- Use the refresh token to generate new tokens
- Update session and cookies with new tokens
- Return the authenticated user
Security Features
JWT Tokens
Uses JSON Web Tokens for stateless authentication with automatic expiration.
HTTP-Only Cookies
Tokens stored in HTTP-only cookies prevent XSS attacks from accessing them.
Secure Sessions
Sessions configured with secure, HTTP-only, and SameSite=Strict settings.
Password Verification
Uses
password_verify() to securely compare hashed passwords.Middleware Pattern
Create authentication middleware to protect routes:app/Middleware/AuthMiddleware.php
Complete Authentication Flow
app/Controllers/AuthController.php
Best Practices
- Always validate credentials: Check both email and password before authentication
- Use HTTPS: Ensure secure cookie transmission in production
- Handle token expiration: The Auth service handles this automatically
- Protect routes: Use
Auth::check()to protect sensitive routes - Clear tokens on logout: Always call
Auth::logout()to clear all authentication data - Store tokens securely: Never expose tokens in URLs or client-side JavaScript
Environment Configuration
The Auth service relies on the JWT service and session configuration. Ensure your environment is properly configured:Next Steps
Controllers
Learn how to use Auth in your controllers
Models
Understand how to work with user models