Auth.js is the next major version of NextAuth.js, a complete rewrite of the original library. It supports the same authentication flows as NextAuth.js, and it supports over 80 OAuth providers out of the box.
Why Auth.js?
Auth.js is super simple to use, and to get started with. If you are looking for a simple way to add authentication to your web app, Auth.js is one of the best options available. Auth.js supports many adapters like Prisma and Drizzle, and it is also free and open source.
Getting started
You can access all methods and properties of Auth.js by importing them from
@/server/auth
. For example, to get the current user session, you can call the
auth
function:
import { auth } from "@/server/auth"
export default async function Page() {
const session = await auth()
const user = session?.user
return (
<div>
<h1>Welcome {user?.name}</h1>
<p>Your email is {user?.email}</p>
</div>
)
}
For a working example, scaffold a new app using Auth.js
and browse through the
files in the src
directory, specifically the app/page.tsx
file and
server/auth
directory, which contain some basic authentication logic.
Authentication
By default, we have included Discord OAuth, but you can add any of the
80+ providers that
Auth.js supports. To configure additional providers, you can add them to the
providers
array in the server/auth/config.ts
file.
import { NextAuthConfig } from "next-auth"
import GitHub from "next-auth/providers/github"
import { env } from "@/env"
export const authConfig: NextAuthConfig = {
providers: [
//...other providers
GitHub({
clientId: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET,
}),
],
}
Do not forget to set the environment variables for your providers. You can add
them to your .env
and src/env.js
files.
Concepts
For a more in-depth look at the concepts behind Auth.js, check out the Auth.js concepts. Here are some of the most important concepts:
- Session Strategies: Auth.js supports both JWT and database sessions. You can choose which one to use based on your needs.
- Authentication: Auth.js supports many authentication flows, including OAuth, Magic Links, Credentials, and WebAuthn.
- Database models: Auth.js can be used with any database, and it supports many popular ORMs like Prisma and Drizzle.
Useful Resources
Resource | Description |
---|---|
Auth.js Documentation | Official documentation for Auth.js |
Auth.js GitHub Repository | Auth.js source code and issues |