Skip to main content

JavaScript Quickstart

Welcome to the JavaScript Quickstart guide! In this document, you will learn to build a single-page JavaScript app, add user login and display user profile information using WSO2 Identity Platform.

What You Will Learn​

  • Create new single-page JavaScript app
  • Install WSO2 Identity Platform JavaScript SDK
  • Add user login and logout
  • Display user profile information

Prerequisites​

Before you start, ensure you have the following:

Example Source Code​

JavaScript App Sample

Configure an Application in WSO2 Identity Platform​

  • Sign into WSO2 Identity Platform console and navigate to Applications > New Application.
  • Select Single Page Application and complete the wizard popup by providing a suitable name and an authorized redirect URL.
Example

name: asgardeo-javascript

Authorized redirect URL: http://localhost:5173

Note down the following values from the Protocol tab of the registered application. You will need them to configure the WSO2 Identity Platform Browser SDK.

  • client-id from the Protocol tab.
  • The name of your WSO2 Identity Platform organization
info

The authorized redirect URL determines where WSO2 Identity Platform should send users after they successfully log in. Typically, this will be the web address where your app is hosted. For this guide, we'll use http://localhost:5173, as the sample app will be accessible at this URL.

Create a JavaScript app using Vite​

Create (a.k.a scaffold) your new JavaScript app using Vite.

npm create vite@latest asgardeo-javascript -- --template vanilla

cd asgardeo-javascript

npm install

npm run dev

Install @asgardeo/browser​

The WSO2 Identity Platform Browser SDK (@asgardeo/browser) provides everything you need to integrate WSO2 Identity Platform into a single-page app. To get started, add it to the project. Make sure to stop the dev server started in the previous step.

npm install @asgardeo/browser

Initialize @asgardeo/browser​

Replace the existing code of the src/main.js file with the following code to initialize the AsgardeoSPAClient.

Important

Replace below placeholders with your registered organization name in WSO2 Identity Platform and the generated client-id from the app you registered in WSO2 Identity Platform.

  • <your-app-client-id>
  • https://api.asgardeo.io/t/<your-organization-name>
src/main.js
import { AsgardeoSPAClient } from "@asgardeo/browser";

const auth = AsgardeoSPAClient.getInstance();

await auth.initialize({
afterSignInUrl: "http://localhost:5173",
afterSignOutUrl: "http://localhost:5173",
clientId: "<your-app-client-id>",
baseUrl: "https://api.asgardeo.io/t/<your-organization-name>"
});

The AsgardeoSPAClient provides signIn and signOut methods, and isSignedIn to check the authentication state. Add the following code to the main.js file to conditionally render a login or logout button based on whether the user is authenticated.

src/main.js
import { AsgardeoSPAClient, hasAuthParamsInUrl } from "@asgardeo/browser";

const auth = AsgardeoSPAClient.getInstance();

await auth.initialize({
afterSignInUrl: "http://localhost:5173",
afterSignOutUrl: "http://localhost:5173",
clientId: "<your-app-client-id>",
baseUrl: "https://api.asgardeo.io/t/<your-organization-name>"
});

(async () => {
if (hasAuthParamsInUrl()) {
// Complete the sign-in when WSO2 Identity Platform redirects back with an authorization code.
await auth.signIn({ callOnlyOnRedirect: true });
} else if (!(await auth.isSignedIn())) {
// Sign in silently if the user already has a session with WSO2 Identity Platform.
await auth.signInSilently();
}

if (await auth.isSignedIn()) {
document.getElementById("authenticated-view").style.display = "block";
document.getElementById("unauthenticated-view").style.display = "none";
} else {
document.getElementById("authenticated-view").style.display = "none";
document.getElementById("unauthenticated-view").style.display = "block";
}
})();

document.querySelector('#app').innerHTML = `
<div>
<div id="authenticated-view" style="display: none">
<button id="logout">Log Out</button>
</div>
<div id="unauthenticated-view" style="display: none">
<button id="login">Log In</button>
</div>
</div>
`;

document.getElementById("login").addEventListener("click", async () => {
auth.signIn();
});

document.getElementById("logout").addEventListener("click", async () => {
auth.signOut();
});

Visit your app's homepage at http://localhost:5173.

Important

You need to create a test user in WSO2 Identity Platform by following this guide to tryout login and logout features.

Display logged in user details​

Modify the code as below to see logged in user details.

src/main.js
import { AsgardeoSPAClient, hasAuthParamsInUrl } from "@asgardeo/browser";

const auth = AsgardeoSPAClient.getInstance();

await auth.initialize({
afterSignInUrl: "http://localhost:5173",
afterSignOutUrl: "http://localhost:5173",
clientId: "<your-app-client-id>",
baseUrl: "https://api.asgardeo.io/t/<your-organization-name>"
});

(async () => {
if (hasAuthParamsInUrl()) {
// Complete the sign-in when WSO2 Identity Platform redirects back with an authorization code.
await auth.signIn({ callOnlyOnRedirect: true });
} else if (!(await auth.isSignedIn())) {
// Sign in silently if the user already has a session with WSO2 Identity Platform.
await auth.signInSilently();
}

if (await auth.isSignedIn()) {
document.getElementById("authenticated-view").style.display = "block";
document.getElementById("unauthenticated-view").style.display = "none";
const user = await auth.getUser();

document.getElementById("username").textContent = "Welcome " + user.username;
} else {
document.getElementById("authenticated-view").style.display = "none";
document.getElementById("unauthenticated-view").style.display = "block";
}
})();

document.querySelector('#app').innerHTML = `
<div>
<div id="authenticated-view" style="display: none">
<p id="username"></p>
<button id="logout">Log Out</button>
</div>
<div id="unauthenticated-view" style="display: none">
<button id="login">Log In</button>
</div>
</div>
`;

document.getElementById("login").addEventListener("click", async () => {
auth.signIn();
});

document.getElementById("logout").addEventListener("click", async () => {
auth.signOut();
});

Was this page helpful?