Developing a Web or Native App with OAuth2 and PKCE
Single page web applications or native apps cannot keep their client secrets private and are therefor called public clients. For such cases the client secret can be omitted by relying on PKCE.
For this guide we assume that you have read the general OAuth2 introduction as well as created an application as shown in the getting started guide.
You may find the whole source code project in this Github repository.
Preparing your ilert app
Before we can start, we need to make sure that our ilert application is configured correctly. Navigate to your application and make sure that you have entered a redirect url that fits your local or deployed webapp. Also copy the client id, as we will need it to configure your application.

Redirecting the user
Using the parameters generated by ilert during the creation of our app, we can build a redirect url for our application, that will take unauthorized users to ilert and ask them to login and grant their permission for our app. In the case of our app, we ask for the profile scope (find more information on other available scopes here), as well as the source:w scope which will grant us permission to read the user's profile data, as well as to read and edit alert sources on behalf of the authorizing user.
Also note that per default for PKCE-only clients no refresh token will be issued, unless the additional offline access scope is requested. Please keep refresh tokens secure.
PKCE generation
Before redirecting the user to the ilert authorization server, we need to generate a verifier and code challenge that can be used by the authorization server to ensure the origin of the (later) token request matches the origin of the authorization request.
Below are helper methods to deal with PKCE generation:
Which may be used like so:
With our verifier and code challenge in place, we can create our redirect url:
In case the user is known to your application or especially if the application is limited to a single account. We suggest to redirect the user directly to the tenant (account url) of the user, this way he might be able to skip the login step if logged in already. e.g. https://someTenant.ilert.com/api/developers/oauth2/authorize instead of https://app.ilert.com/api/developers/...
User grant view
The user will be able to see the scopes that you have requested and grant them.

In case of an error
In case of any error, ilert's authorization server will send the user back to the provided redirect_uri while adding an error and error_description query parameter to it.
In case of successful authorization
If the user has accepted your scopes, ilert's authorization server will redirect the user back to your provided redirect_uri while adding a code query parameter to it. This code parameter can be used in the next step to fetch the access token.
Turning a code into a token
After a successful redirect, we can detect the code query parameter and run an AJAX call to fetch our tokens.
In case of an error
You will receive an error response.
In case of successful token generation
If code and secret are valid, the authorization server will respond with an access as well as a refresh token.
Never expose your user's tokens, especially not the refresh tokens, treat them like passwords.
Using the access token
Your application should store the access and especially the refresh token as safely as possible. You can now make requests to ilert's API on behalf of the authorized user.
Do not forget the Bearer prefix in the authorization header before your access token.
Refreshing an access token
As access tokens are short lived (read more about token lifetime here), you will have to refresh them regularly, at least in case they expire and the ilert API returns a 401 HTTP status code error.
The responses will be equal to the ones of Turning a code into a token above.
Accessing token information
You could parse the JWT token yourself, however we offer a token info endpoint for convience. Note that this endpoint only accepts access tokens.
You will receive an info response like this:
Revoking a refresh token
Just like the user may at any time revoke the refresh token that was issued to your application, your application may also proactively revoke the refresh token.
Make sure to checkout further information on OAuth2 apps.
Last updated
Was this helpful?