-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-context.ts
58 lines (52 loc) · 1.59 KB
/
load-context.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
import { PrismaD1 } from "@prisma/adapter-d1";
import { PrismaClient } from "@prisma/client";
import { createWorkersKVSessionStorage } from "@remix-run/cloudflare";
import { Authenticator } from "remix-auth";
import { DiscordStrategy } from "remix-auth-discord";
import { type PlatformProxy } from "wrangler";
type Cloudflare = Omit<PlatformProxy<Env>, "dispose">;
declare module "@remix-run/cloudflare" {
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface AppLoadContext extends Awaited<ReturnType<typeof getLoadContext>> {}
}
interface User {
id: number;
}
export const getLoadContext = ({
context: { cloudflare },
}: {
context: { cloudflare: Cloudflare };
}) => {
const prisma = new PrismaClient({ adapter: new PrismaD1(cloudflare.env.DB) });
const sessionStorage = createWorkersKVSessionStorage({
cookie: {
name: "session",
sameSite: "lax",
httpOnly: true,
secrets: [cloudflare.env.SESSION_COOKIE_SECRET],
secure: cloudflare.env.LOCAL !== "true",
},
kv: cloudflare.env.KV_SESSION,
});
const auth = new Authenticator<User>(sessionStorage);
const discordStrategy = new DiscordStrategy(
{
clientID: cloudflare.env.DISCORD_CLIENT_ID,
clientSecret: cloudflare.env.DISCORD_CLIENT_SECRET,
callbackURL: "/authentication/discord-callback",
scope: ["identify"],
},
async ({ profile }) => {
const user = await prisma.user.findUnique({
where: { discordId: profile.id },
select: { id: true },
});
if (!user) {
throw new Error("user not found");
}
return { id: user.id };
},
);
auth.use(discordStrategy);
return { auth };
};