Authentication for Next.js
Open Source. Full Stack. Own Your Data.
Patch file: next-auth/core/lib/oauth/callback.js
install pnmp
curl -fsSL https://get.pnpm.io/install.sh | sh -
install Node version ">=16.16.0"
nvm install 16.16.0
nvm use 16.16.0
in next-auth-patched/packages/next-auth:
pnpm install
pnpm run build
testing the library locally: in next-auth-patched/packages/next-auth
npm link
in root project:
npm link next-auth
in next-auth library package.json is required: "name": "@unbox-infinity/next-auth", "repository": "https://github.com/Unbox-infinity/next-auth-patched.git", (for multiple packages under the same repo) "publishConfig": { "registry": "https://npm.pkg.github.com" }
generate personal access token in github with write and read repo permissions
cd next-auth-patched/packages/next-auth
npm login --registry=https://npm.pkg.github.com
login with github username(lowercase) and the token generated
npm publish
NextAuth.js is a complete open source authentication solution for Next.js applications.
It is designed from the ground up to support Next.js and Serverless.
This is a monorepo containing the following packages / projects:
- The primary
next-auth
package - A development test application
- All
@next-auth/*-adapter
packages - The documentation site
npm install next-auth
The easiest way to continue getting started, is to follow the getting started section in our docs.
We also have a section of tutorials for those looking for more specific examples.
See next-auth.js.org for more information and documentation.
- Designed to work with any OAuth service, it supports OAuth 1.0, 1.0A and 2.0
- Built-in support for many popular sign-in services
- Supports email / passwordless authentication
- Supports stateless authentication with any backend (Active Directory, LDAP, etc)
- Supports both JSON Web Tokens and database sessions
- Designed for Serverless but runs anywhere (AWS Lambda, Docker, Heroku, etc…)
NextAuth.js can be used with or without a database.
- An open source solution that allows you to keep control of your data
- Supports Bring Your Own Database (BYOD) and can be used with any database
- Built-in support for MySQL, MariaDB, Postgres, Microsoft SQL Server, MongoDB and SQLite
- Works great with databases from popular hosting providers
- Can also be used without a database (e.g. OAuth + JWT)
- Promotes the use of passwordless sign-in mechanisms
- Designed to be secure by default and encourage best practices for safeguarding user data
- Uses Cross-Site Request Forgery (CSRF) Tokens on POST routes (sign in, sign out)
- Default cookie policy aims for the most restrictive policy appropriate for each cookie
- When JSON Web Tokens are enabled, they are encrypted by default (JWE) with A256GCM
- Auto-generates symmetric signing and encryption keys for developer convenience
- Features tab/window syncing and session polling to support short lived sessions
- Attempts to implement the latest guidance published by Open Web Application Security Project
Advanced options allow you to define your own routines to handle controlling what accounts are allowed to sign in, for encoding and decoding JSON Web Tokens and to set custom cookie security policies and session properties, so you can control who is able to sign in and how often sessions have to be re-validated.
NextAuth.js comes with built-in types. For more information and usage, check out the TypeScript section in the documentation.
// pages/api/auth/[...nextauth].js
import NextAuth from "next-auth"
import AppleProvider from "next-auth/providers/apple"
import GoogleProvider from "next-auth/providers/google"
import EmailProvider from "next-auth/providers/email"
export default NextAuth({
secret: process.env.SECRET,
providers: [
// OAuth authentication providers
AppleProvider({
clientId: process.env.APPLE_ID,
clientSecret: process.env.APPLE_SECRET,
}),
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
// Sign in with passwordless email link
EmailProvider({
server: process.env.MAIL_SERVER,
from: "<[email protected]>",
}),
],
})
The useSession()
React Hook in the NextAuth.js client is the easiest way to check if someone is signed in.
import { useSession, signIn, signOut } from "next-auth/react"
export default function Component() {
const { data: session } = useSession()
if (session) {
return (
<>
Signed in as {session.user.email} <br />
<button onClick={() => signOut()}>Sign out</button>
</>
)
}
return (
<>
Not signed in <br />
<button onClick={() => signIn()}>Sign in</button>
</>
)
}
Use the <SessionProvider>
to allow instances of useSession()
to share the session object across components. It also takes care of keeping the session updated and synced between tabs/windows.
import { SessionProvider } from "next-auth/react"
export default function App({
Component,
pageProps: { session, ...pageProps },
}) {
return (
<SessionProvider session={session}>
<Component {...pageProps} />
</SessionProvider>
)
}
If you think you have found a vulnerability (or not sure) in NextAuth.js or any of the related packages (i.e. Adapters), we ask you to have a read of our Security Policy to reach out responsibly. Please do not open Pull Requests/Issues/Discussions before consulting with us.
NextAuth.js is made possible thanks to all of its contributors.
We're happy to announce we've recently created an OpenCollective for individuals and companies looking to contribute financially to the project!
We're open to all community contributions! If you'd like to contribute in any way, please first read our Contributing Guide.
ISC