Appearance
Authentication
Authentication Logic
Introduction
In January 2022 we refactored our login logic to use an in-app (custom) login page rather than the universal lock login provided by Auth0. This outlines some key components of the login within the codebase.
Backend components
- app
- Auth
- AuthInterface
- PasswordAuth
- PasswordlessEmailAuth
- SocialGoogleAuth
- Http
- Controllers
- Auth
- Auth0AuthController
- AuthController
- AuthGoogleController
- AuthPasswordController
- AuthPasswordlessEmailController
- SignUpController
- Auth
- Requests
- SignUpRequest
- Controllers
- Repositories
- CustomUserRepository
- Services
- AuthService
- Auth
Controllers
Auth0AuthController
This controller handles the callback from Auth0 for authentication that doesn’t use our custom logic, but rather the redirect logic. This is NB for logins like social logins (Google) that need to be redirected once authenticated back to the App
We currently only use the callbackCode
method for the Google social login
AuthController
This handles the primary web requests for authentication
login()
: The main login page requestverify()
: The verify user email requestforgotEmail()
: The forgot email page requestlogout()
: The logout requestlogoutConfirmation()
: The logout confirm page request
AuthGoogleController
This handles the web requests for Google authentication
loginGoogle()
: The Google login redirect request
AuthPasswordController
This handles the web requests for password authentication
forgotPassword()
: The forgot password page requestforgotPasswordProcess()
: Process forgot password requestloginPassword()
: The password login page requestloginPasswordProcess()
: Process login password validation request
AuthPasswordlessEmailController
This handles the web requests for passwordless email authentication
loginPasswordlessEmail()
: Handles both the page and the process of code reques
SignUp Controller
This handles the primary web requests for sign up
signUp()
: The main sign up page requeststore()
: The verify and process new sign up request
Requests
SignUpRequest
Handles the validation for the sign up request
Repositories
CustomUserRepository
Handles the creation and upserting of new and existing users
upsertUse()
: This is the classic Auth0 method to upset a user on login if they were already logged in on auth0updateOrCreate()
: Custom method to create or update a user on sign up and store them locally
Services
AuthService
Service for all unique methods required by authentication
validateAuthEmail()
: Validates the user exists for the provided emailgetUserAuthOptions()
: Gets the login options for the given userprocessLogin()
: Process the login for a given email and Auth0 token datavalidateRedirect()
: Validate redirect url parameter and set in sessionvalidateSignUp()
: Validate if sign up is active and set in sessionunvalidateSignUp()
: Remove sign up active from sessionsuccessfulLoginRedirect()
: Handle redirect on successful loginlinkDuplicateAccounts()
: Handle linking of two authenticated accounts automatically
Managing NGINX authentication
Some of our sites ask for a username or password before they allow you to access the site. It looks something like this:
To set this up, you’ll first need SSH access into the server that hosts the site you are adding a password for. You’ll also need the sudo password for that server.
Setting the username and password
- SSH into the server:
ssh forge@<SERVER_IP>
- Install
apache2-utils
if its not installed already:sudo apt-get install apache2-utils
- Navigate to the folder for the site.
cd example.com
- Create the
.htpasswd
file:sudo htpasswd -c .htpasswd <USERNAME>
- It will prompt you to enter a password and confirm it. Make sure you save this password somewhere (preferably a password manager). The version that you will see in the
.htpasswd
file is a hashed version of the password for security reasons.
Setting up the authentication
Look for a line in the NGINX configuration for the site which looks like this:
php
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/example.com/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Change it to the following (replace example.com with the actual site domain name):
php
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/example.com/server/*;
location / {
try_files $uri $uri/ /index.php?$query_string;
auth_basic "Restricted Area";
auth_basic_user_file /home/forge/example.com/.htpasswd;
}