-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
38 lines (32 loc) · 942 Bytes
/
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
import { MongoDBAdapter } from "@auth/mongodb-adapter";
import NextAuth from "next-auth";
import GithubProvider from "next-auth/providers/github";
import { Users } from "./models/user.server";
import { mongoClient } from "./mongodb";
const {
auth: authRaw,
handlers,
signIn,
signOut,
} = NextAuth({
adapter: MongoDBAdapter(mongoClient),
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID!,
clientSecret: process.env.GITHUB_SECRET!,
}),
],
secret: process.env.JWT_SECRET!,
callbacks: {
session: ({ session, user }) => ({ ...session, user: user ?? null }),
},
});
const auth = process.env.VERCEL
? authRaw
: async () => {
const userDocument = await Users.findOne();
if (!userDocument) throw new Error("Unauthorized");
const { _id, ...user } = userDocument;
return { user: { ...user, id: String(_id) } };
};
export { auth, handlers, signIn, signOut };