Documentation

AuthService

Service class for user authentication.

Handles user registration, login, logout, session management, and API token authentication.

Tags
category

Lwt

author

HugoFara hugo.farajallah@protonmail.com

license

Unlicense http://unlicense.org/

link
https://hugofara.github.io/lwt/docs/php/
since
3.0.0

Table of Contents

Constants

API_TOKEN_EXPIRATION  = 30 * 24 * 60 * 60
API token expiration time in seconds (default: 30 days).
SESSION_TOKEN  = 'LWT_SESSION_TOKEN'
Session key for storing the session token (for CSRF protection).
SESSION_USER_ID  = 'LWT_USER_ID'
Session key for storing the user ID.

Properties

$currentUser  : User|null
Current authenticated user (cached).
$passwordService  : PasswordService
Password service instance.
$repository  : MySqlUserRepository
User repository instance.

Methods

__construct()  : mixed
Create a new AuthService.
findOrCreateWordPressUser()  : User
Find or create a user from WordPress integration.
generateApiToken()  : string
Generate a new API token for a user.
getCurrentUser()  : User|null
Get the currently authenticated user.
invalidateApiToken()  : void
Invalidate a user's API token.
login()  : User
Authenticate a user with username/email and password.
logout()  : void
Log out the current user.
register()  : User
Register a new user.
setCurrentUser()  : void
Set the current user (for session restoration).
validateApiToken()  : User|null
Validate an API token and return the associated user.
validateSession()  : bool
Validate the current session.
createSession()  : void
Create a session for the authenticated user.
destroySession()  : void
Destroy the current session.
findUserByApiToken()  : User|null
Find a user by API token.
findUserByEmail()  : User|null
Find a user by email.
findUserById()  : User|null
Find a user by ID.
findUserByUsername()  : User|null
Find a user by username.
findUserByWordPressId()  : User|null
Find a user by WordPress ID.
saveUser()  : void
Save a new user to the database.
updateUser()  : void
Update an existing user in the database.

Constants

API_TOKEN_EXPIRATION

API token expiration time in seconds (default: 30 days).

private mixed API_TOKEN_EXPIRATION = 30 * 24 * 60 * 60

SESSION_TOKEN

Session key for storing the session token (for CSRF protection).

private mixed SESSION_TOKEN = 'LWT_SESSION_TOKEN'

SESSION_USER_ID

Session key for storing the user ID.

private mixed SESSION_USER_ID = 'LWT_USER_ID'

Properties

$currentUser

Current authenticated user (cached).

private User|null $currentUser = null

Methods

findOrCreateWordPressUser()

Find or create a user from WordPress integration.

public findOrCreateWordPressUser(int $wpUserId, string $username, string $email) : User
Parameters
$wpUserId : int

The WordPress user ID

$username : string

The WordPress username

$email : string

The WordPress email

Return values
User

The found or created user

generateApiToken()

Generate a new API token for a user.

public generateApiToken(int $userId) : string
Parameters
$userId : int

The user ID

Tags
throws
InvalidArgumentException

If user not found

Return values
string

The generated API token

getCurrentUser()

Get the currently authenticated user.

public getCurrentUser() : User|null
Return values
User|null

The current user or null if not authenticated

invalidateApiToken()

Invalidate a user's API token.

public invalidateApiToken(int $userId) : void
Parameters
$userId : int

The user ID

login()

Authenticate a user with username/email and password.

public login(string $usernameOrEmail, string $password) : User
Parameters
$usernameOrEmail : string

The username or email

$password : string

The plain-text password

Tags
throws
AuthException

If authentication fails

Return values
User

The authenticated user

logout()

Log out the current user.

public logout() : void

register()

Register a new user.

public register(string $username, string $email, string $password) : User
Parameters
$username : string

The username

$email : string

The email address

$password : string

The plain-text password

Tags
throws
InvalidArgumentException

If validation fails

throws
RuntimeException

If registration fails

Return values
User

The created user

setCurrentUser()

Set the current user (for session restoration).

public setCurrentUser(User $user) : void
Parameters
$user : User

The user to set as current

validateApiToken()

Validate an API token and return the associated user.

public validateApiToken(string $token) : User|null
Parameters
$token : string

The API token to validate

Return values
User|null

The user if token is valid, null otherwise

validateSession()

Validate the current session.

public validateSession() : bool
Return values
bool

True if the session is valid

createSession()

Create a session for the authenticated user.

private createSession(User $user) : void
Parameters
$user : User

The authenticated user

destroySession()

Destroy the current session.

private destroySession() : void

findUserByApiToken()

Find a user by API token.

private findUserByApiToken(string $token) : User|null
Parameters
$token : string

The API token

Return values
User|null

The user or null if not found

findUserByEmail()

Find a user by email.

private findUserByEmail(string $email) : User|null
Parameters
$email : string

The email address

Return values
User|null

The user or null if not found

findUserById()

Find a user by ID.

private findUserById(int $id) : User|null
Parameters
$id : int

The user ID

Return values
User|null

The user or null if not found

findUserByUsername()

Find a user by username.

private findUserByUsername(string $username) : User|null
Parameters
$username : string

The username

Return values
User|null

The user or null if not found

findUserByWordPressId()

Find a user by WordPress ID.

private findUserByWordPressId(int $wpUserId) : User|null
Parameters
$wpUserId : int

The WordPress user ID

Return values
User|null

The user or null if not found

saveUser()

Save a new user to the database.

private saveUser(User $user) : void
Parameters
$user : User

The user to save

Tags
throws
RuntimeException

If save fails

updateUser()

Update an existing user in the database.

private updateUser(User $user) : void
Parameters
$user : User

The user to update


        
On this page

Search results