Skip to main content

JavaScript Quickstart

You're reading Identity & Access Management 7.2.0. The newest release is 7.3.0, and Cloud has the newest features. Go to the newest release

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 Server.

What You Will Learn​

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

Prerequisites​

Before you start, ensure you have the following:

  • About 15 minutes
  • Set-up WSO2 Identity Server
  • Install Node.js on your system.
  • Make sure you have a JavaScript package manager like npm, yarn, or pnpm.
  • A favorite text editor or IDE

Example Source Code​

JavaScript App Sample

Configure an Application in WSO2 Identity Server​

  • Sign into WSO2 Identity Server 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: is-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 WSO2 Identity Platform JavaScript SDK.

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

The authorized redirect URL determines where WSO2 Identity Server 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 usehttp://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 is-javascript -- --template vanilla

cd is-javascript

npm install

npm run dev

Install @asgardeo/auth-spa​

WSO2 Identity Platform JavaScript SDK provides all the components and hooks you need to integrate WSO2 Identity Server into your app. To get started, simply add the WSO2 Identity Platform JavaScript SDK to the project. Make sure to stop the dev server started in the previous step.

npm install @asgardeo/auth-spa

Initialize @asgardeo/auth-spa​

Replace the existing code of the main.jsx file with the following code to initialize AuthSPAClient.

Important

Replace below placeholders with your registered organization name in WSO2 Identity Server and the generatedclient-id from the app you registered in WSO2 Identity Server.

  • <your-app-client-id>
  • https://localhost:9443
src/main.js

import { AsgardeoSPAClient } from "@asgardeo/auth-spa";

const auth = AsgardeoSPAClient.getInstance();

await auth.initialize({
signInRedirectURL: "http://localhost:5173",
signOutRedirectURL: "http://localhost:5173",
clientID: "<your-app-client-id>",
baseUrl: "https://localhost:9443",
scope: ["openid", "profile"]
});

The AsgardeoSPAClient provides signIn and signOut methods and access to 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, SPAUtils } from "@asgardeo/auth-spa";

const auth = AsgardeoSPAClient.getInstance();

await auth.initialize({
signInRedirectURL: "http://localhost:5173",
signOutRedirectURL: "http://localhost:5173",
clientID: "<your-app-client-id>",
baseUrl: "https://localhost:9443",
scope: ["openid", "profile"]
});

(async () => {
let user = undefined;

if (SPAUtils.hasAuthSearchParamsInURL()) {
user = await auth.signIn({ callOnlyOnRedirect: true });
} else {
user = await auth.trySignInSilently();
}

if (user) {
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 Server 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, SPAUtils } from "@asgardeo/auth-spa";

const auth = AsgardeoSPAClient.getInstance();

await auth.initialize({
signInRedirectURL: "http://localhost:5173",
signOutRedirectURL: "http://localhost:5173",
clientID: "<your-app-client-id>",
baseUrl: "https://localhost:9443",
scope: ["openid", "profile"]
});

(async () => {
let user = undefined;

if (SPAUtils.hasAuthSearchParamsInURL()) {
user = await auth.signIn({ callOnlyOnRedirect: true });
} else {
user = await auth.trySignInSilently();
}

if (user) {
document.getElementById("authenticated-view").style.display = "block";
document.getElementById("unauthenticated-view").style.display = "none";
document.getElementById("username").innerHTML = "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?