Skip to main content

Nuxt quickstart

Cloud has the newest features. Some may not be in a release yet; the newest release is Identity & Access Management 7.3.0.

Welcome to the Nuxt quickstart guide! In this document, you will learn to build a Nuxt app, add user login, and display user profile information using WSO2 Identity Platform.

What You Will Learn​

  • Create a new Nuxt app
  • Install @asgardeo/nuxt module
  • Add user sign-in and sign-out
  • Display user profile information
  • Protect routes with route middleware

Prerequisites​

Before you start, ensure you have the following:

Example Source Code​

Nuxt App Sample

Configure an application in WSO2 Identity Platform​

  • Sign into the WSO2 Identity Platform Console and navigate to Applications > New Application.
  • Select Traditional Web Application and complete the wizard by providing a suitable name, the OpenID Connect protocol, and an authorized redirect URL.
Example

Name: asgardeo-nuxt

Authorized redirect URL: http://localhost:3000/api/auth/callback

Once you finish creating the application, note down the following values from its Info tab. You will need them to configure the WSO2 Identity Platform Nuxt SDK.

  • Client ID - The unique identifier for your application.
  • Client Secret - The confidential secret generated for your application.
  • Base URL - The base URL of your WSO2 Identity Platform organization. This typically follows the format https://api.asgardeo.io/t/<your-organization-name>.
info

The authorized redirect URL is the address WSO2 Identity Platform sends the user back to after a successful sign-in. The SDK exposes this callback at /api/auth/callback, so for local development you must register http://localhost:3000/api/auth/callback.

Create a Nuxt app​

Create your new Nuxt app.

npm create nuxt@latest asgardeo-nuxt
cd asgardeo-nuxt
npm run dev

Install @asgardeo/nuxt​

The WSO2 Identity Platform Nuxt SDK is shipped as a Nuxt module. It registers a Nitro server plugin that runs the OAuth flow, a set of API routes under /api/auth/*, and auto-imported components and composables you can use anywhere in your app. Stop the dev server before installing the module.

npm install @asgardeo/nuxt

Set up environment variables​

Create a .env file in the root of your Nuxt project. The Nuxt module reads NUXT_PUBLIC_* variables on the client and the unprefixed secrets on the server only.

.env
NUXT_PUBLIC_ASGARDEO_BASE_URL="https://api.asgardeo.io/t/<your-organization-name>"
NUXT_PUBLIC_ASGARDEO_CLIENT_ID="<your-app-client-id>"
NUXT_PUBLIC_ASGARDEO_AFTER_SIGN_OUT_URL="<after-sign-out-redirect-url>"
ASGARDEO_CLIENT_SECRET="<your-app-client-secret>"

Register the WSO2 Identity Platform Nuxt module​

Open nuxt.config.ts and add @asgardeo/nuxt to the modules array. The SDK reads the credentials you set in .env automatically — you only need to declare the module.

nuxt.config.ts
export default defineNuxtConfig({
modules: ['@asgardeo/nuxt'],
});

The SDK reads your credentials from the environment variables automatically. You can optionally configure post sign-in / sign-out destinations in the asgardeo config block if needed.

Wrap your app with <AsgardeoProvider />​

<AsgardeoProvider /> mounts the SDK's full provider tree (auth, user, organization, branding, theme) and exposes the SSR-hydrated state to every composable below it. The component is auto-registered by the module — you do not need to import it.

Replace the contents of app.vue with the following.

app.vue
<template>
<AsgardeoProvider>
<NuxtPage />
</AsgardeoProvider>
</template>
info

All SDK components are auto-registered (for example, SignedIn, SignInButton, UserDropdown). You can use them directly in any <template> without an explicit import.

Add sign-in and sign-out to your app​

The WSO2 Identity Platform Nuxt SDK provides <SignInButton /> and <SignOutButton /> to start and end a session. Combine them with <SignedIn /> and <SignedOut /> to render content based on the user's authentication state.

Create a pages/index.vue file (or replace its contents if it already exists) with the following.

pages/index.vue
<template>
<header>
<SignedOut>
<SignInButton />
</SignedOut>
<SignedIn>
<SignOutButton />
</SignedIn>
</header>
</template>

When the user clicks Sign In, the SDK calls /api/auth/signin, which redirects the browser to WSO2 Identity Platform's sign-in page. After authentication, WSO2 Identity Platform sends the user back to /api/auth/callback where the SDK exchanges the authorization code for tokens and issues the session cookie.

Display the signed-in user's profile information​

The SDK provides several ways to access the signed-in user's profile information. You can use the User, UserProfile, or UserDropdown components to access and display user profile information in a declarative way.

  • <User />: Component provides a scoped slot (v-slot) pattern to access user profile information:
  • <UserProfile /> — Component provides a declarative way to display and update user profile information.
  • <UserDropdown /> — Component provides a dropdown menu with built-in user information and sign-out functionality.

Update pages/index.vue to render the dropdown and a personalized greeting.

pages/index.vue
<template>
<header>
<SignedOut>
<SignInButton />
</SignedOut>
<SignedIn>
<UserDropdown />
<SignOutButton />
</SignedIn>
</header>

<main>
<SignedIn>
<User v-slot="{ user }">
<div>
<p>Welcome back, {{ user.userName || user.username || user.sub }}</p>
</div>
</User>
<UserProfile />
</SignedIn>
</main>
</template>

Protect a page with middleware​

The WSO2 Identity Platform Nuxt SDK provides a named middleware called asgardeoMiddleware that you can use to gate any route behind authentication. Unauthenticated visitors are redirected to the sign-in flow automatically.

Use definePageMeta to apply the asgardeoMiddleware middleware to any page. For example, create a pages/dashboard.vue:

pages/dashboard.vue
<script setup lang="ts">
definePageMeta({ middleware: ['asgardeoMiddleware'] });
</script>

Choose how users will sign in​

Before running the app, you need to decide how you want users to sign into your application:

When users click Sign In, your app redirects them to WSO2 Identity Platform's sign-in page. After they sign in, it redirects them back to your app. This default behavior works without extra configurations.

The sign-in form appears directly inside your application using the SignIn component. Users never leave your app during the sign-in process.

Enable App-Native Authentication​

The embedded sign-in functionality depends on the App-Native Authentication feature. This feature allows your app to authenticate users without redirecting them to the WSO2 Identity Platform sign-in page.

To enable this feature, follow these steps:

  • Navigate to WSO2 Identity Platform Console.
  • Go to Applications > Your App > Advanced.
  • Enable App-Native Authentication by checking the checkbox.
info

Read more about App-Native Authentication to understand how it works.

Set up the in-app sign-in form​

Create a dedicated sign-in page that renders the embedded form. Create pages/sign-in.vue:

pages/sign-in.vue
<template>
<SignIn />
</template>

Then, update the .env file with the route for the sign-in page. Add the following line to your .env file:

.env
NUXT_PUBLIC_ASGARDEO_SIGN_IN_URL="/sign-in"

Run the app​

Start the dev server.

npm run dev

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

Important

To try out sign-in and sign-out, create a test user in WSO2 Identity Platform by following this guide.

Was this page helpful?