UserApiHandler
in package
implements
ApiRoutableInterface
uses
ApiRoutableTrait
API handler for user operations.
Handles authentication endpoints via REST API.
Provides endpoints for:
- POST /api/v1/user/login - Authenticate and get token
- POST /api/v1/user/register - Create account and get token
- POST /api/v1/user/refresh - Refresh API token
- POST /api/v1/user/logout - Invalidate token
- GET /api/v1/user/me - Get current user info
Tags
Table of Contents
Interfaces
- ApiRoutableInterface
- Interface for API handlers that participate in route dispatch.
Properties
- $userFacade : UserFacade
- User facade instance.
Methods
- __construct() : mixed
- Create a new UserApiHandler.
- formatLogin() : array<string, mixed>
- Handle user login and return API token.
- formatLogout() : array{success: bool, error?: string}
- Log out the current user and invalidate their API token.
- formatMe() : array{success: bool, user?: array, error?: string}
- Get current authenticated user information.
- formatRefresh() : array<string, mixed>
- Refresh the current user's API token.
- formatRegister() : array<string, mixed>
- Handle user registration and return API token.
- getUserFacade() : UserFacade
- Get the UserFacade instance.
- isAuthenticated() : bool
- Check if the current request is authenticated.
- routeDelete() : JsonResponse
- routeGet() : JsonResponse
- Handle a GET request for this resource.
- routePost() : JsonResponse
- Handle a POST request for this resource.
- routePut() : JsonResponse
- validateBearerToken() : User|null
- Validate API token from Authorization header.
- validateSession() : bool
- Validate session authentication.
- frag() : string
- Extract a fragment from the fragments array.
- createDefaultFacade() : UserFacade
- Create a default UserFacade instance.
- formatUserData() : array{id: int, username: string, email: string, role: string, created: string, last_login: ?string, has_wordpress: bool}
- Format user data for API response.
Properties
$userFacade
User facade instance.
private
UserFacade
$userFacade
Methods
__construct()
Create a new UserApiHandler.
public
__construct([UserFacade|null $userFacade = null ]) : mixed
Parameters
- $userFacade : UserFacade|null = null
-
User facade (optional for BC)
formatLogin()
Handle user login and return API token.
public
formatLogin(array<string, mixed> $params) : array<string, mixed>
Parameters
- $params : array<string, mixed>
-
Login credentials (username or email, password)
Return values
array<string, mixed>formatLogout()
Log out the current user and invalidate their API token.
public
formatLogout() : array{success: bool, error?: string}
Return values
array{success: bool, error?: string}formatMe()
Get current authenticated user information.
public
formatMe() : array{success: bool, user?: array, error?: string}
Return values
array{success: bool, user?: array, error?: string}formatRefresh()
Refresh the current user's API token.
public
formatRefresh() : array<string, mixed>
Requires valid authentication (either session or current token).
Return values
array<string, mixed>formatRegister()
Handle user registration and return API token.
public
formatRegister(array<string, mixed> $params) : array<string, mixed>
Parameters
- $params : array<string, mixed>
-
Registration data (username, email, password, password_confirm)
Return values
array<string, mixed>getUserFacade()
Get the UserFacade instance.
public
getUserFacade() : UserFacade
Useful for access to additional user functionality.
Return values
UserFacadeisAuthenticated()
Check if the current request is authenticated.
public
isAuthenticated() : bool
Tries both token and session authentication.
Return values
bool —True if request is authenticated
routeDelete()
public
routeDelete(array<string|int, mixed> $fragments, array<string|int, mixed> $params) : JsonResponse
Parameters
- $fragments : array<string|int, mixed>
- $params : array<string|int, mixed>
Return values
JsonResponserouteGet()
Handle a GET request for this resource.
public
routeGet(array<string|int, mixed> $fragments, array<string|int, mixed> $params) : JsonResponse
Parameters
- $fragments : array<string|int, mixed>
-
URL path segments (resource name already consumed)
- $params : array<string|int, mixed>
-
Query parameters
Return values
JsonResponseroutePost()
Handle a POST request for this resource.
public
routePost(array<string|int, mixed> $fragments, array<string|int, mixed> $params) : JsonResponse
Parameters
- $fragments : array<string|int, mixed>
-
URL path segments (resource name already consumed)
- $params : array<string|int, mixed>
-
POST/JSON body parameters
Return values
JsonResponseroutePut()
public
routePut(array<string|int, mixed> $fragments, array<string|int, mixed> $params) : JsonResponse
Parameters
- $fragments : array<string|int, mixed>
- $params : array<string|int, mixed>
Return values
JsonResponsevalidateBearerToken()
Validate API token from Authorization header.
public
validateBearerToken() : User|null
This method extracts and validates a Bearer token from the Authorization header. If valid, it sets up the user context.
Return values
User|null —The authenticated user or null
validateSession()
Validate session authentication.
public
validateSession() : bool
Checks if a valid session exists and sets up user context.
Return values
bool —True if session is valid
frag()
Extract a fragment from the fragments array.
protected
frag(array<int, string> $fragments, int $index) : string
Parameters
- $fragments : array<int, string>
-
The URL path fragments
- $index : int
-
The index to extract
Return values
string —The fragment at the index, or empty string if not present
createDefaultFacade()
Create a default UserFacade instance.
private
createDefaultFacade() : UserFacade
Return values
UserFacadeformatUserData()
Format user data for API response.
private
formatUserData(User $user) : array{id: int, username: string, email: string, role: string, created: string, last_login: ?string, has_wordpress: bool}
Parameters
- $user : User
-
The user entity