-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
auth.ts
127 lines (122 loc) · 3.91 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { createAppClient, viemConnector } from '@farcaster/auth-client'
import NextAuth from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
export const {
handlers: { GET, POST },
auth,
} = NextAuth({
theme: {
logo: 'https://next-auth.js.org/img/logo/logo-sm.png',
},
providers: [
CredentialsProvider({
name: 'Sign in with Farcaster',
credentials: {
message: {
label: 'Message',
type: 'text',
placeholder: '0x0',
},
signature: {
label: 'Signature',
type: 'text',
placeholder: '0x0',
},
// In a production app with a server, these should be fetched from
// your Farcaster data indexer rather than have them accepted as part
// of credentials.
// ...jfc i love dEcEnTrAlIzaTiOn
name: {
label: 'Name',
type: 'text',
placeholder: '0x0',
},
pfp: {
label: 'Pfp',
type: 'text',
placeholder: '0x0',
},
},
authorize: async (credentials, req) => {
const { csrfToken } = (await req.json()) as any
const appClient = createAppClient({
ethereum: viemConnector(),
})
const { success, fid } = await appClient.verifySignInMessage({
message: credentials?.message as string,
signature: credentials?.signature as `0x${string}`,
domain: process.env.NEXT_PUBLIC_DOMAIN!,
nonce: csrfToken,
})
if (!success) {
return null
}
return {
id: fid.toString(),
name: credentials.name as string,
image: credentials?.pfp as string,
}
},
}),
],
callbacks: {
jwt: async ({ token, user, account, profile, trigger }) => {
if (user) token.user = user
if (user) {
token.uid = user.id
}
return token
},
session: async ({ session, token, user }) => {
if (token.user) session.user = { ...session.user, id: (token.user as any).id }
// session.user.uid = user.uid;
return session
},
},
events: {
async signIn(message) {
/* on successful sign in */
},
async signOut(message) {
/* on signout */
},
async createUser(message) {
/* user created */
},
async updateUser(message) {
/* user updated - e.g. their email was verified */
},
async linkAccount(message) {
/* account (e.g. Twitter) linked to a user */
},
async session(message) {
/* session is active */
},
},
pages: {
signIn: '/login',
signOut: '/',
error: '/', // Error code passed in query string as ?error=
verifyRequest: '/', // (used for check email message)
// newUser: null // If set, new users will be directed here on first sign in
},
debug: false,
cookies: {
sessionToken: {
name: process.env.AUTH_SESSION_COOKIE_NAME,
options: {
httpOnly: true,
sameSite: 'none',
secure: true,
},
},
csrfToken: {
name: process.env.AUTH_CSRF_COOKIE_NAME,
options: {
httpOnly: true,
sameSite: 'none',
secure: true,
},
},
},
})