70
loading...
This website collects cookies to deliver better user experience
Under Manage in the side menu, find and select App Registration
Create a New Registration and fill in the form by entering the name of the app (can be changed later) and selecting the support account types (I used the single-tenant option, the first option in the screenshot below). DO NOT enter a Redirect URI for now.
Select the newly created application and then select Authentication > Add Platform under the Manage menu
Select the Single-Page Application tile under the Web applications heading
Enter a redirect URI. I'll be using http://localhost:3000/
. DO NOT check the boxes under Implicit grant and hybrid flows heading
Click on the Configure button to finish.
Note You can add more than one redirect URI, for instance, for different environments, development, staging, production, etc.
npx create-next-app msal-next-auth --use-npm
cd msal-next-auth
npm install @azure/msal-react @azure/msal-browser
services/msal.js
in the root of the project and add the following codeimport * as msal from "@azure/msal-browser";
const msalConfig = {
auth: {
clientId: process.env.NEXT_PUBLIC_AZURE_AD_CLIENT_ID,
authority: `https://login.microsoftonline.com/${process.env.NEXT_PUBLIC_AZURE_AD_TENANT_ID}`,
redirectUri: '/'
}
};
const msalInstance = new msal.PublicClientApplication(msalConfig);
export { msalInstance }
.env.local
, in the root of the project folder..env.local
NEXT_PUBLIC_AZURE_AD_CLIENT_ID='your-client-id'
NEXT_PUBLIC_AZURE_AD_TENANT_ID='your-tenant-id'
pages/_app.js
import { MsalProvider } from '@azure/msal-react';
import { msalInstance } from '../services/msal';
import '../styles/globals.css';
function MyApp({ Component, pageProps }) {
return (
<MsalProvider instance={msalInstance}>
<Component {...pageProps} />
</MsalProvider>
);
}
export default MyApp;
MsalAuthenticationTemplate
component. Paired with UnauthenticatedTemplate
, this can be useful when rendering specific content to authenticated or unauthenticated users respectively. pages/index.js
, with the following codeimport {
AuthenticatedTemplate,
UnauthenticatedTemplate,
useMsal,
} from '@azure/msal-react';
import Head from 'next/head';
import styles from '../styles/Home.module.css';
function SignInButton() {
// useMsal hook will return the PublicClientApplication instance you provided to MsalProvider
const { instance } = useMsal();
return <button onClick={() => instance.loginRedirect()}>Sign In</button>;
}
function WelcomeUser() {
const { accounts } = useMsal();
const username = accounts[0].username;
return <p>Welcome, {username}</p>;
}
export default function Home() {
return (
<div className={styles.container}>
<Head>
<title>Azure AD Authentication using MSAL and Next.js</title>
</Head>
<AuthenticatedTemplate>
<p>This will only render if a user is signed-in.</p>
<WelcomeUser />
</AuthenticatedTemplate>
<UnauthenticatedTemplate>
<p>This will only render if a user is not signed-in.</p>
<SignInButton />
</UnauthenticatedTemplate>
</div>
);
}
npm run dev
), you should see the following in the browser.UnauthenticatedTemplate
and AuthenticatedTemplate
wrappers.useIsAuthenticated
hook. Learn more herepages/index.js
...
// Define sign out button
function SignOutButton() {
const { instance } = useMsal();
return <button onClick={() => instance.logoutRedirect()}>Sign Out</button>;
}
...
export default function Home() {
return (
<div className={styles.container}>
<Head>
<title>Azure AD Authentication using MSAL and Next.js</title>
</Head>
<AuthenticatedTemplate>
<p>This will only render if a user is signed-in.</p>
<WelcomeUser />
<SignOutButton /> // <-- Add to authenticated content
</AuthenticatedTemplate>
...
npm run dev
.