token-handler-js-assistant
A helper library to help SPAs interact with the OAuth Agent in the Token Handler pattern.
Add to project
Add to your project using npm
npm install @curity/token-handler-js-assistant
How to use in your project
Import the Assistant into your project and initialize it using Configuration
object.
import {OAuthAgentClient} from "@curity/token-handler-js-assistant";
const client = new OAuthAgentClient({oauthAgentBaseUrl: 'https://api.example.com/oauthagent/example'})
The Configuration
object contains the following options:
oauthAgentBaseUrl
- a URL with path to the token handler application created in the Curity Identity Server (this URL ends with a token handler application ID
as defined in the Curity Identity Server configuration).
Using the initialized client
- Starting the user login
const response = await this.oauthAgentClient.startLogin({
extraAuthorizationParameters: {
scope: "openid profile",
login_hint: "username",
ui_locales: "en"
}
})
location.href = response.authorizationUrl
- Finishing the user login
const url = new URL(location.href)
const response = await client.endLogin({ searchParams: url.searchParams })
if (response.isLoggedIn) {
}
Note: The endLogin
function should only be called with authorization response parameters (when the authorization
server redirected user to the SPA after a successful user login). It's recommended to call onPageLoad()
instead
on every load of the SPA. This function makes a decision based the query string and either calls endLogin()
or session()
.
- Handling page load
const sessionResponse = await client.onPageLoad(location.href)
if (sessionResponse.isLoggedIn) {
} else {
const response = await client.startLogin()
location.href = response.authorizationUrl
}
- Refreshing tokens
await client.refresh()
- Retrieving ID token claims
const sessionResponse = await client.session()
if (session.isLoggedIn === true) {
session.idTokenClaims?.sub
}
- Logging out
const logoutResponse = await client.logout()
if (logoutResponse.logoutUrl) {
location.href = logoutResponse.logoutUrl;
}