Tutorial 1: Basic example
In this example we will walk through how to use AuthGuard to build your own auth server for a simple web application.
#
Prerequisites- Make sure that you have already followed the instruction on how to build an AuthGuard distribution, or that you are using a standard distribution.
- Follow the guideline for the server setup.
Adapt the guideline to your use case
This guideline is written from the perspective of a web application which communicates with AuthGuard directly. The same concepts, however, are transferrable to other use cases, and you should adapt it as such.
#
ScenarioOur scenario will be a simple flow, our goals are:
- Users can register a username but also their email
- In the signup page, we inform the user of whether their chosen username and email are available or not
- Users can log in using either their username or email
- The login process will result in an access token and a refresh token (you can use sessions instead)
#
RegistrationBefore making any calls to create an account, we need first
an idempotent key. Idempotent keys are there to guarantee
that duplicate calls will not have any effect. For this purpose
we will use uuidv4 library,
and when your registration page loads, a new idempotent key should
be generated as const IDEMPOTENT_KEY = uuidv4()
.
The first step in our registration process is to give the user feedback on whether the chosen username exists. For that, you need to set the input change event to something like this
If everything is good, then we can proceed with creating an account
#
LoginWe can now move on to letting users log in using their credentials.
#
RefreshAccess tokens are short-lived, which means that your client will need to refresh the token. Possibly multiple times within a session. You can either do this periodically, or only if a request was made and the response code was 401.
#
Updating Credentials and AccountUpdating user account information and credentials cannot be done using an auth client. Instead, your client will need to make a request to your server, which will then issue the update request to AuthGuard.
#
Password ResetIf a user forgot their password, they can receive a temporary
password reset token sent to their email. You can issue a POST
request to /credentials/reset_token
to generate a reset
token.
If the Token Send plugin is part of your distribution, then AuthGuard will send the email itself. Otherwise, your server will need to send it. Typically, the email will contain a URL to a reset page of your own which will accept the token as a query parameter;
Auth client vs admin client
The reset token is returned back in the response only if the request was made by an admin client. If you want an auth client to make the request then having the Token Send plugin and an email provider plugin is a requirement. Standard distributions come with both.
Once the user has the reset token, in your reset page, you can do the following