forked from vercel/ai-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
84 lines (81 loc) · 2 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import NextAuth, { type DefaultSession } from 'next-auth'
import { headers } from 'next/headers';
import CredentialsProvider from "next-auth/providers/credentials"
// import { SiweMessage } from "siwe"
import { SigninMessage } from './utils/signMessage';
declare module 'next-auth' {
interface Session {
user: {
/** The user's id. */
id: string
} & DefaultSession['user']
}
}
const providers = [
CredentialsProvider({
name: "Solana",
credentials: {
message: {
label: "Message",
type: "text",
placeholder: "0x0",
},
signature: {
label: "Signature",
type: "text",
placeholder: "0x0",
},
nonce: {
label: "Nonce",
type: "text",
}
},
async authorize(credentials) {
try {
const msg = credentials?.message;
const siws = new SigninMessage(JSON.parse(msg ? (msg as string) : '{}'))
const signature = credentials?.signature;
const result = await siws.validate(signature ? (signature as string) : '',)
if (result) {
return {
id: siws.publicKey,
}
}
return null
} catch (e) {
return null
}
},
}),
]
export const {
handlers: { GET, POST },
auth
} = NextAuth({
providers,
callbacks: {
jwt({ token, profile }) {
if (profile) {
token.id = profile.id
token.image = profile.avatar_url || profile.picture
}
return token
},
session: ({ session, token }) => {
if (session?.user && token?.sub) {
session.user.id = String(token.sub)
}
return session
},
authorized({ auth }) {
return !!auth?.user // this ensures there is a logged in user for -every- request
},
redirect({ url, baseUrl }) {
if (url.startsWith("/")) return `${baseUrl}${url}`
return baseUrl
}
},
pages: {
signIn: '/sign-in' // overrides the next-auth default signin page https://authjs.dev/guides/basics/pages
}
})