-
-
Notifications
You must be signed in to change notification settings - Fork 385
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
Create Auth.js migration guide #268
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
29f712c
Fully isolate auth package
haydenbleasel 35b0b47
Create authentication folder
haydenbleasel c93d1b4
Update overview.mdx
haydenbleasel 397c14e
Draft Auth.js migration guide
haydenbleasel 3203fc2
Switch to generic naming for auth middleware
haydenbleasel bef4340
Update authjs.mdx
haydenbleasel fa9c3c9
Update authjs.mdx
haydenbleasel 9d9e4c2
Fix typo
haydenbleasel 2da16c1
Revert "Update authjs.mdx"
haydenbleasel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
--- | ||
title: Switch to Auth.js | ||
description: How to switch from Clerk to Auth.js. | ||
--- | ||
|
||
<Warning> | ||
next-forge support for Auth.js is currently blocked by [this issue](https://github.com/nextauthjs/next-auth/issues/11076). | ||
</Warning> | ||
|
||
Here's how to switch from Clerk to [Auth.js](https://authjs.dev/). | ||
|
||
## 1. Replace the dependencies | ||
|
||
Uninstall the existing Clerk dependencies from the `auth` package... | ||
|
||
```sh Terminal | ||
pnpm remove @clerk/nextjs @clerk/themes @clerk/types --filter auth | ||
``` | ||
|
||
... and install the Auth.js dependencies. | ||
|
||
```sh Terminal | ||
pnpm add next-auth@beta --filter auth | ||
``` | ||
|
||
## 2. Generate an Auth.js secret | ||
|
||
Auth.js requires a random value secret, used by the library to encrypt tokens and email verification hashes. In each of the relevant app directories, generate a secret with the following command: | ||
|
||
```sh Terminal | ||
cd apps/app && npx auth secret | ||
cd apps/web && npx auth secret | ||
cd apps/api && npx auth secret | ||
``` | ||
|
||
This will automatically add an `AUTH_SECRET` environment variable to the `.env.local` file in each directory. | ||
|
||
## 3. Replace the relevant files | ||
|
||
Delete the existing `client.ts` and `server.ts` files in the `auth` package. Then, create the following file: | ||
|
||
```tsx packages/auth/index.ts | ||
import NextAuth from "next-auth"; | ||
|
||
export const { handlers, signIn, signOut, auth } = NextAuth({ | ||
providers: [], | ||
}); | ||
``` | ||
|
||
## 4. Update the middleware | ||
|
||
Update the `middleware.ts` file in the `auth` package with the following content: | ||
|
||
```tsx packages/auth/middleware.ts | ||
import 'server-only'; | ||
|
||
export { auth as authMiddleware } from './'; | ||
``` | ||
|
||
## 5. Update the auth components | ||
|
||
Auth.js has no concept of "sign up", so we'll use the `signIn` function to sign up users. Update both the `sign-in.tsx` and `sign-up.tsx` components in the `auth` package with the same content: | ||
|
||
<CodeGroup> | ||
|
||
```tsx packages/auth/components/sign-in.tsx | ||
import { signIn } from '../'; | ||
|
||
export const SignIn = () => ( | ||
<form | ||
action={async () => { | ||
"use server"; | ||
await signIn(); | ||
}} | ||
> | ||
<button type="submit">Sign in</button> | ||
</form> | ||
); | ||
``` | ||
|
||
```tsx packages/auth/components/sign-up.tsx | ||
import { signIn } from '../'; | ||
|
||
export const SignUp = () => ( | ||
<form | ||
action={async () => { | ||
"use server"; | ||
await signIn(); | ||
}} | ||
> | ||
<button type="submit">Sign up</button> | ||
</form> | ||
); | ||
``` | ||
|
||
</CodeGroup> | ||
|
||
## 6. Update the Provider file | ||
|
||
Auth.js has no concept of a Provider as a higher-order component, so you can either remove it entirely or just replace it with a stub, like so: | ||
|
||
```tsx packages/auth/provider.tsx | ||
import type { ReactNode } from 'react'; | ||
|
||
type AuthProviderProps = { | ||
children: ReactNode; | ||
}; | ||
|
||
export const AuthProvider = ({ children }: AuthProviderProps) => children; | ||
``` | ||
|
||
## 7. Create an auth route handler | ||
|
||
In your `app` application, create an auth route handler file with the following content: | ||
|
||
```tsx apps/app/api/auth/[...nextauth]/route.ts | ||
import { handlers } from "@repo/auth" | ||
|
||
export const { GET, POST } = handlers; | ||
``` | ||
|
||
## 8. Update your apps | ||
|
||
From here, you'll need to replace any remaining Clerk implementations in your apps with Auth.js references. This means swapping out references like: | ||
|
||
```tsx page.tsx | ||
const { orgId } = await auth(); | ||
const { redirectToSignIn } = await auth(); | ||
const user = await currentUser(); | ||
``` | ||
|
||
Etcetera. Keep in mind that you'll need to build your own "organization" logic as Auth.js doesn't have a concept of organizations. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '@clerk/nextjs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { clerkMiddleware as authMiddleware } from '@clerk/nextjs/server'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import 'server-only'; | ||
|
||
export * from '@clerk/nextjs/server'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A small suggestion: #281