Use SDK to call your application server
How to make authorized request to your application server after login with Authgear
Using Authgear SDK to call your application server
In this section, we are going to explain how to make an authorized request to your application server by using Authgear SDK. If you are using Cookie-based authentication in your web application, you can skip this section as session cookies are handled by the browser automatically.
Setup Overview
To determine which user is calling your server, you will need to include the Authorization header in every request that send to your application server.
You will need to set up Backend integration, Authgear will help you to resolve the Authorization header to determine whether the incoming HTTP request is authenticated or not.
In the below section, we will explain how to set up SDK to include Authorization header to your application requests.
SDK setup
Configuration
Configure the Authgear SDK with the Authgear endpoint and client id. The SDK must be properly configured before use, you can call configure multiple times but you should only need to call it once. No network call will be triggered during configure.
authgear
.configure({
clientID: "<YOUR_APPLICATION_CLIENT_ID>",
endpoint: "<YOUR_AUTHGEAR_ENDPOINT>",
})
.then(() => {
// configured successfully
})
.catch((e) => {
// failed to configured
});let authgear = Authgear(clientId: clientId, endpoint: endpoint)
authgear.configure() { result in
switch result {
case .success():
// configured successfully
case let .failure(error):
// failed to configured
}
}ConfigureOptions configureOptions = new ConfigureOptions();
Authgear authgear = new Authgear(getApplication(), clientID, endpoint);
authgear.configure(configureOptions, new OnConfigureListener() {
@Override
public void onConfigured() {
// configured successfully
}
@Override
public void onConfigurationFailed(@NonNull Throwable throwable) {
// failed to configured
}
});Get the latest session state
sessionState reflect the user logged in state. Right after configure, the session state only reflects the SDK local state. That means even sessionState is AUTHENTICATED, the session may be invalid if it is revoked remotely. You will only know that after calling the server, call fetchUserInfo as soon as it is proper to do so, e.g. when the device goes online.
Calling an API
Use fetch function from the SDK (JavaScript Only)
fetch function from the SDK (JavaScript Only)Javascript Only. Authgear SDK provides fetch function for you to call your application server. The fetch function will include Authorization header in your application request, and handle refresh access token automatically. authgear.fetch implement fetch. If you are using another networking library and want to include the Authorization header yourself. You can skip this and go to the next step.
Add the access token to the HTTP request header
You will need to include the Authorization header in your application request. Call refreshAccessTokenIfNeeded every time before using the access token, the function will check and make the network call only if the access token has expired. Include the access token into the Authorization header of your application request.
Handle revoked sessions
If the session is revoked from the management portal, the client will call your application server with an invalid access token. Your application server can check that by looking at the resolver headers.
For example, you application may return HTTP status code 401 for unauthorized requests. Depending on your application flow, you may want to show your user login page again or reset the SDK sessionState to NO_SESSION locally. To clear the sessionState, you can use clearSessionState function.
Last updated
Was this helpful?