Skip to content

Base required changes to integrate NextJS, NextAuth, and Azure B2C #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: original
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Required for NEXTAUTH - the root of your site
NEXTAUTH_URL="http://localhost:3000"
# Random string for jwt secret
JWT_SECRET="some-hedgehogs-are-not-so-cuddly-ow"
# The Client ID for the App Registration created within your Azure B2C Tenant
#### UPDATE WITH YOUR OWN VALUE ####
AUTH_CLIENT_ID="your-azureb2c-app-registration-client-id"
# The Client Secret for the App Registration created within your Azure B2C Tenant
#### UPDATE WITH YOUR OWN VALUE ####
AUTH_CLIENT_SECRET="your-azureb2c-app-registration-client-secret"
# Your Azure B2C Tenant name, like yourAzureB2CTenantName.onmicrosoft.com
#### UPDATE WITH YOUR OWN VALUE ####
AUTH_TENANT_NAME="yourAzureB2CTenantName"
# Your Azure B2C Sign in (and/or sign up) user flow name.
# This will start with B2C_1_
#### UPDATE WITH YOUR OWN VALUE ####
USER_FLOW="B2C_1_signupsignin1"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ yarn-debug.log*
yarn-error.log*

# local env files
.env
.env.local
.env.development.local
.env.test.local
Expand Down
12 changes: 12 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require('dotenv').config()

module.exports = {
env: {
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
AUTH_CLIENT_ID: process.env.AUTH_CLIENT_ID,
AUTH_CLIENT_SECRET: process.env.AUTH_CLIENT_SECRET,
AUTH_TENANT_NAME: process.env.AUTH_TENANT_NAME,
JWT_SECRET: process.env.JWT_SECRET,
USER_FLOW: process.env.USER_FLOW,
}
}
9 changes: 8 additions & 1 deletion pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import '../styles/globals.css'
import { Provider as AuthProvider } from 'next-auth/client'

function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
const { session } = pageProps

return (
<AuthProvider options={{ site: process.env.NEXTAUTH_URL }} session={session}>
<Component {...pageProps} />)
</AuthProvider>
)
}

export default MyApp
47 changes: 47 additions & 0 deletions pages/api/auth/[...nextauth].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import NextAuth from 'next-auth'

const tenantName = process.env.AUTH_TENANT_NAME
const userFlow = process.env.USER_FLOW

const options = {
session: {
jwt: true,
},
secret: process.env.JWT_SECRET,
pages: {
signOut: '/auth/signout',
},
providers: [
{
id: 'azureb2c',
name: 'Azure B2C',
type: 'oauth',
version: '2.0',
debug: true,
scope: 'offline_access openid',
params: {
grant_type: 'authorization_code',
},
accessTokenUrl: `https://${tenantName}.b2clogin.com/${tenantName}.onmicrosoft.com/${userFlow}/oauth2/v2.0/token`,
// requestTokenUrl: 'https://login.microsoftonline.com/2cc61216-bdb2-4dcd-9705-601b506491b2/oauth2/v2.0/token',
authorizationUrl: `https://${tenantName}.b2clogin.com/${tenantName}.onmicrosoft.com/${userFlow}/oauth2/v2.0/authorize?response_type=code+id_token&response_mode=form_post`,
profileUrl: 'https://graph.microsoft.com/oidc/userinfo',
profile: (profile) => {
console.log('THE PROFILE', profile)

return {
id: profile.oid,
fName: profile.given_name,
lName: profile.surname,
email: profile.emails.length ? profile.emails[0] : null,
}
},
clientId: process.env.AUTH_CLIENT_ID,
clientSecret: process.env.AUTH_CLIENT_SECRET,
idToken: true,
state: false,
},
],
}

export default (req, res) => NextAuth(req, res, options)
60 changes: 35 additions & 25 deletions pages/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Head from 'next/head'
import styles from '../styles/Home.module.css'
import { useSession } from 'next-auth/client'

export default function Home() {
const [session, loading] = useSession()

return (
<div className={styles.container}>
<Head>
Expand All @@ -19,35 +22,42 @@ export default function Home() {
<code className={styles.code}>pages/index.js</code>
</p>

<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h3>Documentation &rarr;</h3>
<p>Find in-depth information about Next.js features and API.</p>
</a>
{session ?
<>
<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h3>Documentation &rarr;</h3>
<p>Find in-depth information about Next.js features and API.</p>
</a>

<a href="https://nextjs.org/learn" className={styles.card}>
<h3>Learn &rarr;</h3>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>
<a href="https://nextjs.org/learn" className={styles.card}>
<h3>Learn &rarr;</h3>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>

<a
href="https://github.com/vercel/next.js/tree/master/examples"
className={styles.card}
>
<h3>Examples &rarr;</h3>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>
<a
href="https://github.com/vercel/next.js/tree/master/examples"
className={styles.card}
>
<h3>Examples &rarr;</h3>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>

<a
href="https://vercel.com/import?filter=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
>
<h3>Deploy &rarr;</h3>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
<a
href="https://vercel.com/import?filter=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
>
<h3>Deploy &rarr;</h3>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</a>
</div>
<div>You are signed in! You can also sign out if you like.</div>
</>
: <div>
You are not signed in! <a style={{color: 'blue'}} href="/api/auth/signin">You must sign in to access documentation!</a>
</div>}
</main>

<footer className={styles.footer}>
Expand Down