Skip to main content

@elfsquad/authentication

AuthenticationContext

The AuthenticationContext class allows you to easily authenticate with the Elfsquad API.


constructor(options)

Creates an instance of AuthenticationContext & initializes with the provided authentication options.

Method parameters


options required (IAuthenticationOptions)

const authenticationContext = new AuthenticationContext({
clientId: 'your-client-id',
redirectUri: 'https://example.com',
scope: 'Elfskot.Api offline_access',
responseMode: 'fragment',
loginUrl: 'https://login.elfsquad.io'
});

onSignIn()

This method can be used for executing logic after the user has signed in. For example, you can use this method to fetch & set the access token, change the UI, etc.

Returns (Promise<void>)


promise that resolves when the user has signed in. If the user is already signed in, the promise resolves immediately.

const authenticationContext = new AuthenticationContext();
async function onSignIn() {
console.log('User has signed in');
};
authenticationContext.onSignIn().then(onSignIn);

signIn(options)

This method starts the login flow & redirects the user to the login page.

Method parameters


options required (IOauthOptions)

oauth options that will be passed on to the authorization request. This can be used, for example, to perform a silent login.

Returns (Promise<void>)


const authenticationContext = new AuthenticationContext();
authenticationContext.signIn();

signOut(postLogoutRedirectUri)

This method signs the user out & revokes the tokens. After signing out, the user will be redirected to the postLogoutRedirectUri. If no postLogoutRedirectUri is provided, the user will be redirected to the login page.

Method parameters


postLogoutRedirectUri required (string | null)

the uri where the user will be redirected to after signing out.

const authenticationContext = new AuthenticationContext();
const postLogoutRedirectUri = 'https://example.com';
authenticationContext.signOut(postLogoutRedirectUri);

isSignedIn()

This method can be used to check if the user is signed in, for example to show a login/logout button.

Returns (Promise<boolean>)


promise that resolves with a boolean indicating if the user is signed in.

const authenticationContext = new AuthenticationContext();
async function isSignedIn(value: boolean) {
console.log('User is signed in:', value);
}
authenticationContext.isSignedIn().then(isSignedIn);

getAccessToken()

This method can be used to get the access token. This method will automatically refresh the access token if it has expired and a valid refresh token is available.

Returns (Promise<string>)


promise that resolves with the access token.

const authenticationContext = new AuthenticationContext();
authenticationContext.getAccessToken().then(accessToken => {
console.log('Access token:', accessToken);
});

getIdToken()

This method can be used to get the id token. Similar to the getAccessToken method, this method will automatically refresh the id (and access) token if it has expired and a valid refresh token is available.

Returns (Promise<string>)


promise that resolves with the id token.

const authenticationContext = new AuthenticationContext();
authenticationContext.getIdToken().then(idToken => {
console.log('Id token:', idToken);
});

setState(data)

This method can be used to persist date in local storage, which can be used to save data between sign in attempts. This can be useful, for example, to save the url the current url before the user is redirected to the login page.

Method parameters


data required (any)

the data that will be persisted in local storage.

const authenticationContext = new AuthenticationContext();

authenticationContext.setState({ url: window.location.href });
authenticationContext.onSignIn().then(() => {
const { url } = authenticationContext.getState();
window.location.href = url;
});

authenticationContext.signIn();

getState()

This method can be used to retrieve data that was persisted in local storage using the setState method.

Returns (any | null)


the data that was persisted in local storage.

const authenticationContext = new AuthenticationContext();

authenticationContext.setState({ url: window.location.href });
authenticationContext.onSignIn().then(() => {
const { url } = authenticationContext.getState();
window.location.href = url;
});

authenticationContext.signIn();