Skip to content

Commit 012ea40

Browse files
chore: simplify next-auth (vercel#80)
1 parent 5912336 commit 012ea40

File tree

8 files changed

+55
-116
lines changed

8 files changed

+55
-116
lines changed

.env.example

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
## Then get your OpenAI API Key here: https://platform.openai.com/account/api-keys
33
OPENAI_API_KEY=XXXXXXXX
44

5-
## Generate a random secret: https://generate-secret.vercel.app/32
6-
NEXTAUTH_SECRET=XXXXXXXX
7-
8-
## Only required for localhost
9-
NEXTAUTH_URL=http://localhost:3000
10-
5+
## Generate a random secret: https://generate-secret.vercel.app/32 or `openssl rand -base64 32`
6+
AUTH_SECRET=XXXXXXXX
117
## Create a GitHub OAuth app here: https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/creating-an-oauth-app
128
AUTH_GITHUB_ID=XXXXXXXX
139
AUTH_GITHUB_SECRET=XXXXXXXX
10+
## Support OAuth login on preview deployments, see: https://authjs.dev/guides/basics/deployment#securing-a-preview-deployment
11+
AUTH_REDIRECT_PROXY_URL=https://auth.example.com/api/auth
1412

15-
# instructions to create kv database here: https://vercel.com/docs/storage/vercel-kv/quickstart and
13+
# Instructions to create kv database here: https://vercel.com/docs/storage/vercel-kv/quickstart and
1614
KV_URL=XXXXXXXX
1715
KV_REST_API_URL=XXXXXXXX
1816
KV_REST_API_TOKEN=XXXXXXXX

app/api/auth/[...nextauth]/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { GET, POST } from '@/auth'
2+
export const runtime = 'edge'

app/api/chat/route.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ const openai = new OpenAIApi(configuration)
1616
export async function POST(req: Request) {
1717
const json = await req.json()
1818
const { messages, previewToken } = json
19-
const session = await auth()
19+
const userId = (await auth())?.user.id
2020

21-
if (session == null) {
21+
if (!userId) {
2222
return new Response('Unauthorized', {
2323
status: 401
2424
})
@@ -38,31 +38,28 @@ export async function POST(req: Request) {
3838
const stream = OpenAIStream(res, {
3939
async onCompletion(completion) {
4040
const title = json.messages[0].content.substring(0, 100)
41-
const userId = session?.user?.id
42-
if (userId) {
43-
const id = json.id ?? nanoid()
44-
const createdAt = Date.now()
45-
const path = `/chat/${id}`
46-
const payload = {
47-
id,
48-
title,
49-
userId,
50-
createdAt,
51-
path,
52-
messages: [
53-
...messages,
54-
{
55-
content: completion,
56-
role: 'assistant'
57-
}
58-
]
59-
}
60-
await kv.hmset(`chat:${id}`, payload)
61-
await kv.zadd(`user:chat:${userId}`, {
62-
score: createdAt,
63-
member: `chat:${id}`
64-
})
41+
const id = json.id ?? nanoid()
42+
const createdAt = Date.now()
43+
const path = `/chat/${id}`
44+
const payload = {
45+
id,
46+
title,
47+
userId,
48+
createdAt,
49+
path,
50+
messages: [
51+
...messages,
52+
{
53+
content: completion,
54+
role: 'assistant'
55+
}
56+
]
6557
}
58+
await kv.hmset(`chat:${id}`, payload)
59+
await kv.zadd(`user:chat:${userId}`, {
60+
score: createdAt,
61+
member: `chat:${id}`
62+
})
6663
}
6764
})
6865

auth.ts

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,35 @@
1-
import NextAuth from 'next-auth'
1+
import NextAuth, { type DefaultSession } from 'next-auth'
22
import GitHub from 'next-auth/providers/github'
3-
import CredentialsProvider from 'next-auth/providers/credentials'
3+
import { NextResponse } from 'next/server'
44

5-
// We default to using GitHub for authentication for local development and production.
6-
// On Preview deployments, we use a dummy credentials provider. This allows folks to easily
7-
// test the app without having to create a custom GitHub OAuth app or change the callback URL
8-
// just to test the application on previews.
5+
declare module 'next-auth' {
6+
interface Session {
7+
user: {
8+
/** The user's id. */
9+
id: string
10+
} & DefaultSession['user']
11+
}
12+
}
913

10-
// We have a custom /sign-in page for non-preview environments. In preview environments, the user
11-
// will be redirected to /api/auth/signin instead.
1214
export const {
1315
handlers: { GET, POST },
1416
auth,
1517
CSRF_experimental
16-
// @ts-ignore
1718
} = NextAuth({
18-
// @ts-ignore
19-
providers: [
20-
process.env.VERCEL_ENV === 'preview'
21-
? CredentialsProvider({
22-
name: 'Credentials',
23-
credentials: {
24-
username: {
25-
label: 'Username',
26-
type: 'text',
27-
placeholder: 'jsmith'
28-
},
29-
password: { label: 'Password', type: 'password' }
30-
},
31-
async authorize(credentials) {
32-
return {
33-
id: 1,
34-
name: 'J Smith',
35-
36-
picture: 'https://i.pravatar.cc/[email protected]'
37-
} as any
38-
}
39-
})
40-
: GitHub
41-
],
19+
providers: [GitHub],
4220
callbacks: {
43-
// @ts-ignore
44-
jwt: async ({ token, profile }) => {
45-
if (profile?.id) {
21+
jwt({ token, profile }) {
22+
if (profile) {
4623
token.id = profile.id
4724
token.image = profile.picture
4825
}
4926
return token
5027
},
51-
// @ts-ignore
5228
authorized({ auth }) {
5329
return !!auth?.user
54-
},
55-
trustHost: true
30+
}
5631
},
57-
...(process.env.VERCEL_ENV === 'preview'
58-
? {}
59-
: {
60-
pages: {
61-
signIn: '/sign-in'
62-
}
63-
})
32+
pages: {
33+
signIn: '/sign-in'
34+
}
6435
})

middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { auth as middleware } from './auth'
22

33
export const config = {
4-
matcher: ['/', '/api/chat']
4+
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)']
55
}

next-auth.d.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"focus-trap-react": "^10.1.1",
3333
"nanoid": "^4.0.2",
3434
"next": "13.4.7-canary.1",
35-
"next-auth": "0.0.0-manual.e65faa1c",
35+
"next-auth": "0.0.0-manual.83c4ebd1",
3636
"next-themes": "^0.2.1",
3737
"openai-edge": "^0.5.1",
3838
"react": "^18.2.0",

pnpm-lock.yaml

Lines changed: 7 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)