Each time the SDK refreshes the accessToken the freshTokensCallback is called with the response. You can store this data in localStorage or any other persistant data store. When you restart your application, you can check the data store for a refreshToken and use that to authenticate with the SDK.
You need to capture the response from the authenticate function when logging in with email / password so that subsequent SDK initializations such as app restarts can use the key / secret combination stored in persistent data storage to authenticate the current user.
The package export a client you can use in combination with a proxy service. The client will throw a typed error in case you need to redirect to the login page.
Password Grant flow with two-step MFA in try / catch
import {
createOAuth2Client,
MfaRequiredError,
} from "@extrahorizon/javascript-sdk";
const exh = createOAuth2Client({
host: "",
clientId: "",
});
try {
await exh.auth.authenticate({
password: "",
username: "",
});
} catch (error) {
if (error instanceof MfaRequiredError) {
const { mfa } = error;
// Your logic to request which method the user want to use in case of multiple methods
const methodId = mfa.methods[0].id;
await exh.auth.confirmMfa({
token: mfa.token,
methodId,
code: "", // code from ie. Google Authenticator
});
}
}
Confidential Applications
If you are using a confidential application in combination with React-Native. The SDK will add btoa function to your global scope. See https://github.com/ExtraHorizon/javascript-sdk/issues/446
If you want to create an application can you use generic to determine the correct application and application version type.
ie. creating an OAuth1 application with a version.
// Will return OAuth1Application type
const app = await exh.auth.applications.create({
type: "oauth1",
name: "test",
description: "test",
});
// Will return OAuth1ApplicationVersion type
const version = await exh.auth.applications.createVersion<typeof app>(app.id, {
name: "1.0.0",
});
Typeguards
If you need a typeguard, you can use the following snippets.
import {
Application,
ApplicationVersion,
OAuth1Application,
OAuth1ApplicationVersion,
} from "@extrahorizon/javascript-sdk";
function isOAuth1Version(
version: ApplicationVersion
): version is OAuth1ApplicationVersion {
return `consumerKey` in version;
}
function isOAuth1(app: Application): app is OAuth1Application {
return !("redirectUris" in app);
}
const { data: apps } = await exh.auth.applications.get();
apps.filter(isOAuth1).forEach((app) => {
// app will have type OAuth1Application
});