Next.JS Quickstart
Welcome to the Next.js quickstart guide! In this document, you will learn to build a Next.js app, add user login and display user profile information using WSO2 Identity Platform.
What You Will Learn
- Create new Next.js app
- Install @asgardeo/nextjs package
- Add user login and logout
- Display user profile information
Prerequisites
Before you start, ensure you have the following:
- About 15 minutes
- WSO2 Identity Platform account
- Make sure you have a JavaScript package manager like
npm,yarn, orpnpm. - A favorite text editor or IDE
Example Source Code
Configure an Application in WSO2 Identity Platform
- Sign into the WSO2 Identity Platform Console and navigate to Applications > New Application.
- Select Next.js and complete the wizard by providing a suitable name and an authorized redirect URL.
Name: asgardeo-nextjs
Authorized redirect URL: http://localhost:3000
Once you finish creating the application, note down the following values from its Guide tab. You will need them to configure WSO2 Identity Platform Next.js SDK.
- Client ID - The unique identifier for your application.
- Client Secret - The secret key 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>
The authorized redirect URL specifies where WSO2 Identity Platform should send users after they successfully log in. This is usually the web address where your application is running. For this guide, we'll use http://localhost:3000, as the sample app will be accessible at this URL.
Create a Next.js app
Create your new Next.js app.
- npm
- yarn
- pnpm
npm create next-app@latest asgardeo-nextjs -- --yes
cd asgardeo-nextjs
npm run dev
yarn create next-app@latest asgardeo-nextjs -- --yes
cd asgardeo-nextjs
yarn dev
pnpm create next-app@latest asgardeo-nextjs -- --yes
cd asgardeo-nextjs
pnpm dev
Install @asgardeo/nextjs
WSO2 Identity Platform Next.js SDK provides all the components and hooks you need to integrate WSO2 Identity Platform into your app. To get started, simply add the WSO2 Identity Platform Next.js SDK to the project. Make sure to stop the dev server started in the previous step.
- npm
- yarn
- pnpm
npm install @asgardeo/nextjs
yarn add @asgardeo/nextjs
pnpm add @asgardeo/nextjs
Set up environment variables
Create a .env or an appropriate environment configuration file in the root of your Next.js project. This file will store all the configuration values required for the WSO2 Identity Platform Next.js SDK to function.
NEXT_PUBLIC_ASGARDEO_BASE_URL="https://api.asgardeo.io/t/<your-organization-name>"
NEXT_PUBLIC_ASGARDEO_CLIENT_ID="<your-app-client-id>"
ASGARDEO_CLIENT_SECRET="<your-app-client-secret>"
Set up the middleware
The asgardeoMiddleware helper integrates WSO2 Identity Platform authentication into your Next.js application and supports both the App and Pages routers.
- For Next.js 16 and later
- For Next.js 15 and earlier
Create a file called proxy.ts in the root of your Next.js project.
import {asgardeoMiddleware} from '@asgardeo/nextjs/middleware';
export const proxy = asgardeoMiddleware();
export const config = {
matcher: [
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
'/(api|trpc)(.*)',
],
};
Create a file called middleware.ts in the root of your Next.js project.
import {asgardeoMiddleware} from '@asgardeo/nextjs/middleware';
export default asgardeoMiddleware();
export const config = {
matcher: [
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
'/(api|trpc)(.*)',
],
};
Add <AsgardeoProvider /> to your app
The <AsgardeoProvider /> serves as a context provider for the SDK. You can integrate this provider to your app by wrapping the root component.
Add the following changes to the app/layout.tsx file in your Next.js project.
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import {AsgardeoProvider} from '@asgardeo/nextjs/server';
import "./globals.css";
const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
});
const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
});
export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
<AsgardeoProvider>{children}</AsgardeoProvider>
</body>
</html>
);
}
Add sign-in and sign-out to your app
WSO2 Identity Platform SDK provides SignInButton, SignOutButton components to handle user sign-in and sign-out. You can use these components along side SignedIn and SignedOut components to conditionally render content based on the user's logged in state.
Replace the existing content of the app/page.tsx file with following content.
import {SignInButton, SignedIn, SignOutButton, SignedOut} from '@asgardeo/nextjs';
export default function Home() {
return (
<header>
<SignedOut>
<SignInButton />
</SignedOut>
<SignedIn>
<SignOutButton />
</SignedIn>
</header>
);
}
Display 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: TheUsercomponent provides a render prop pattern to access user profile information:UserProfile: TheUserProfilecomponent provides a declarative way to display and update user profile information.UserDropdown: TheUserDropdowncomponent provides a dropdown menu with built-in user information and sign-out functionality.
'use client'
import { SignedIn, SignedOut, SignInButton, SignOutButton, User, UserDropdown, UserProfile } from '@asgardeo/nextjs';
export default function Home() {
return (
<>
<header>
<SignedIn>
<UserDropdown />
<SignOutButton />
</SignedIn>
<SignedOut>
<SignInButton />
</SignedOut>
</header>
<main>
<SignedIn>
<User>
{(user) => (
<div>
<p>Welcome back, {user.userName || user.username || user.sub}</p>
</div>
)}
</User>
<UserProfile />
</SignedIn>
</main>
</>
);
}
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.
Read more about App-Native Authentication to understand how it works.
Set up the in-app sign-in form
First, create an app route for the sign-in page. Create a new file called app/sign-in/page.tsx and add the following code:
'use client'
import { SignIn } from '@asgardeo/nextjs';
export default function SignInPage() {
return <SignIn />;
}
Then, update the .env file with the route for the sign-in page. Add the following line to your .env file:
NEXT_PUBLIC_ASGARDEO_SIGN_IN_URL="/sign-in"
Run the app
To run the app, use the following command:
- npm
- yarn
- pnpm
npm run dev
yarn dev
pnpm dev
Visit your app's homepage at http://localhost:3000.
You need to create a test user in WSO2 Identity Platform by following this guide to tryout sign-in and sign-out features.
What's Next?
Was this page helpful?