class AuthService

Supports authentication operations.

Constants

PENDING_PASSWORD_RESET

Session key holding the id of the user who has just proven their credentials and must set a new password. The reset page only acts on this user, so the user id in the URL cannot be used to reset someone else's password.

Methods

static string
confirm(Input $request)

Gets value of password confirm field. Assumes field value is "confirm".

static User|false|null
currentUser()

Checks if a user is logged in.

static string
hashPassword(string $password)

Hashes a plain text password using PHP's password_hash() with the default algorithm.

static Login
login(Input $request, Login $loginModel, string $username, bool $mailer = false)

Attempts to log a user in. If successful, resets login attempts and creates a session. Otherwise, tracks failed attempts and optionally triggers an email notification.

static Login
loginAttempts(User $user, Login $loginModel, bool $mailer = false)

Tests for login attempts and sets session messages when there is a failed attempt or when account is locked.

static void
loginUser(User $loginUser, bool $rememberMe = false)

Creates a session when the user logs in. A new record is added to the user_sessions table and a cookie is created if remember me is selected.

static User|null
loginUserFromCookie()

Logs in user from cookie.

static void
logout(Input $request)

Logs out the currently logged-in user by clearing the session and deleting any active cookies.

static bool
logoutUser(User $user)

Perform logout operation on current logged in user. The record for the current logged in user is removed from the user_session table and the corresponding cookie is deleted.

static void
passwordReset(Input $request, User $user)

Handles the complete flow of resetting a user’s password, including setting the confirmation field and updating the record.

static void
beginPasswordReset(User $user)

Records that a user has proven their credentials and must now set a new password. Call this when login detects the reset_password flag, before redirecting to the reset page.

static User|null
pendingPasswordResetUser(int $id)

Returns the user whose password may be reset in this session. The id must match the user recorded by beginPasswordReset() and that user must still have the reset_password flag set.

static Uploads|null
profileImageUpload(User $user)

Processes a profile image upload and returns an Uploads object.

Details

at line 36
static string confirm(Input $request)

Gets value of password confirm field. Assumes field value is "confirm".

Parameters

Input $request

The request.

Return Value

string

The value of the confirm field.

at line 46
static User|false|null currentUser()

Checks if a user is logged in.

Return Value

User|false|null

An object containing information about current logged in user from users table.

at line 57
static string hashPassword(string $password)

Hashes a plain text password using PHP's password_hash() with the default algorithm.

Parameters

string $password

Original password submitted on a registration or update password form.

Return Value

string

The hashed version of the password.

at line 73
static Login login(Input $request, Login $loginModel, string $username, bool $mailer = false)

Attempts to log a user in. If successful, resets login attempts and creates a session. Otherwise, tracks failed attempts and optionally triggers an email notification.

Parameters

Input $request

The request for the login.

Login $loginModel

The login model.

string $username

The user to be logged in.

bool $mailer

Sends account deactivated E-mail when user surpasses max number of login attempts before account is locked.

Return Value

Login

Model that handles logins.

at line 114
static Login loginAttempts(User $user, Login $loginModel, bool $mailer = false)

Tests for login attempts and sets session messages when there is a failed attempt or when account is locked.

Parameters

User $user

The user whose login attempts we are tracking.

Login $loginModel

The model that will be responsible for displaying messages.

bool $mailer

Sends account deactivated E-mail when user surpasses max number of login attempts before account is locked.

Return Value

Login

$loginModel The Login model after login in attempt test and session messages are assigned.

at line 143
static void loginUser(User $loginUser, bool $rememberMe = false)

Creates a session when the user logs in. A new record is added to the user_sessions table and a cookie is created if remember me is selected.

Parameters

User $loginUser

The user to be logged in.

bool $rememberMe

Value obtained from remember me checkbox found in login form. Default value is false.

Return Value

void

at line 166
static User|null loginUserFromCookie()

Logs in user from cookie.

Return Value

User|null

The user associated with previous session.

at line 185
static void logout(Input $request)

Logs out the currently logged-in user by clearing the session and deleting any active cookies.

Parameters

Input $request

The current request.

Return Value

void

at line 206
static bool logoutUser(User $user)

Perform logout operation on current logged in user. The record for the current logged in user is removed from the user_session table and the corresponding cookie is deleted.

Parameters

User $user

The user to be logged out.

Return Value

bool

Returns true if operation is successful.

at line 220
static void passwordReset(Input $request, User $user)

Handles the complete flow of resetting a user’s password, including setting the confirmation field and updating the record.

Parameters

Input $request

The request for the password reset action.

User $user

The user whose password we will reset.

Return Value

void

at line 254
static void beginPasswordReset(User $user)

Records that a user has proven their credentials and must now set a new password. Call this when login detects the reset_password flag, before redirecting to the reset page.

Parameters

User $user

The user who must reset their password.

Return Value

void

at line 267
static User|null pendingPasswordResetUser(int $id)

Returns the user whose password may be reset in this session. The id must match the user recorded by beginPasswordReset() and that user must still have the reset_password flag set.

Parameters

int $id

The user id from the reset page URL.

Return Value

User|null

The user, or null if this session has no pending reset for that id.

at line 288
static Uploads|null profileImageUpload(User $user)

Processes a profile image upload and returns an Uploads object.

Assumes the input name is profileImage.

Parameters

User $user

The user who uploaded a profile image.

Return Value

Uploads|null

The uploads object if it's created or null.