-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth.ts
67 lines (59 loc) · 1.99 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
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { createAppClient, viemConnector } from "@farcaster/auth-client";
import { sql } from 'kysely';
import { db } from "./kysely";
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
CredentialsProvider({
name: "Sign in with Farcaster",
credentials: {
message: { label: "Message", type: "text", placeholder: "0x0" },
signature: { label: "Signature", type: "text", placeholder: "0x0" },
name: { label: "Name", type: "text", placeholder: "0x0" },
pfp: { label: "Pfp", type: "text", placeholder: "0x0" },
csrfToken: { label: "CSRF Token", type: "text", placeholder: "0x0" },
},
async authorize(credentials) {
const appClient = createAppClient({
ethereum: viemConnector(),
});
const verifyResponse = await appClient.verifySignInMessage({
message: credentials?.message as string,
signature: credentials?.signature as `0x${string}`,
domain: "farhack.xyz",
nonce: credentials.csrfToken as string,
});
const { success, fid } = verifyResponse;
if (!success) {
return null;
}
let user = await db.selectFrom('users')
.selectAll()
.where('id', '=', fid)
.executeTakeFirst();
if (!user) {
await db.insertInto('users').values({
id: fid,
name: credentials?.name as string,
image: credentials?.pfp as string,
is_admin: false,
admin_hackathons: ''
}).execute();
user = await db.selectFrom('users')
.selectAll()
.where('id', '=', fid)
.executeTakeFirst();
}
if (!user) {
return null;
}
return {
id: user.id.toString(),
name: user.name,
image: user.image,
};
},
}),
],
});