Skip to main content

next-auth

@ensono-stacks/next:next-auth

Add Next Authentication to your next application

The next-auth generator will create a new library and configure NextAuth.js into an existing Next application. It will add the initial configuration, add the session provider, setup an API endpoint and add local environmental variables. It will also configure provider specific setup.

Prerequisites

An existing Next application

Usage

nx g @ensono-stacks/next:next-auth

Command line arguments

The following command line arguments are available:

OptionDescriptionTypeAccepted ValuesDefault
--nameThe name of the librarystringstringN/A
--directoryThe location of where the library should be installedstringstringN/A
--projectThe name of the projectnameOfApplicationstringN/A
--providerThe provider to be installedstringnone, microsoft-entra-id, auth0none
--sessionStorageThe session storage to be usedstringcookie, rediscookie
--guestSessionEnable guest sessioinsbooleantrue/falsetrue
--importPathThe library name used to import itstringstringN/A
--skipPackageJsonDo not add dependencies to package.jsonbooleantrue/falsefalse

Generator Output

  • Creates a new Library with the given name. Contains the configuration for Auth.js, including provider and adapter setup, config, and token rotation.
Generated files
├── auth
│ ├── src
│ │ ├── actions
│ │ │ ├── index.ts
│ │ │ ├── oauth.ts
│ │ │ ├── profile.ts
│ │ ├── adapter
│ │ │ ├── index.ts
│ │ │ ├── redis-adapter.ts
│ │ │ ├── redis-repository.ts
│ │ ├── callbacks
│ │ │ ├── index.ts
│ │ │ ├── jwt.ts
│ │ │ ├── session.ts
│ │ ├── providers
│ │ │ ├── auth0.ts
│ │ │ ├── guest.ts
│ │ │ ├── index.ts
│ │ ├── types
│ │ │ ├── index.d.ts
│ │ ├── utils
│ │ │ ├── guards.ts
│ │ │ ├── index.ts
│ │ ├── config.ts
│ │ ├── index.ts
│ ├── .eslintrc.json
│ ├── jest.config.ts
│ ├── project.json
│ ├── README.md
│ ├── tsconfig.json
│ ├── tsconfig.lib.json
└── └── tsconfig.spec.json
  • Creates a new Next API endpoint with the file name [...nextauth].ts. This contains the dynamic route handler for NextAuth.js which will also contain all of your global NextAuth.js configurations. If you have specified a provider when running the generator this will be added to the providers array
/apps/appName/src/app/api/auth/[...nextauth]/route.ts
import { handlers } from "@my-stacks-workspace/auth";

export const { GET, POST } = handlers;
  • Install the next-auth package and add to package.json, unless the --skipPackageJson option was used
/package.json
"dependencies": {
...otherDependencies
"next-auth": "5.0.0-beta.19",
"ioredis": "5.4.1", // if using --sessionStorage=redis
},
  • Create or append an .env.local file. Adding required next auth environmental variables. These will vary depending on the provider chosen.
/.env.local
NEXTAUTH_URL=http://localhost:4200
NEXTAUTH_SECRET=secretValue
AZURE_AD_CLIENT_ID=
AZURE_AD_CLIENT_SECRET=
AZURE_AD_TENANT_ID=
note

Be sure to update the environmental variables with the values provided by your provider

/apps/appName/src/components/client-component.tsx
"use client";

import { useSession } from "next-auth/react";

export default function ClientComponent() {
const { data } = useSession();

if (!data || !data.user || data.user.role === "guest") {
return null;
}

return <div>{data.user.name}</div>;
}
/apps/appName/src/app/page.tsx
import { SessionProvider } from "next-auth/react";

import { auth } from "@my-stacks-workspace/auth";

import ClientComponent from "../components/client-component";
import GuestSessionProvider from "../components/guest-session";

export default async function Index() {
const session = await auth();
return (
<>
<SessionProvider session={session}>
<GuestSessionProvider />
<ClientComponent />
</SessionProvider>
</>
);
}

For further information please see the Getting Started Guide to Next Auth