Repository navigation
Expand file tree
/
Copy pathauth.ts
More file actions
70 lines (66 loc) · 1.94 KB
/
Copy pathauth.ts
File metadata and controls
70 lines (66 loc) · 1.94 KB
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
import type { GetServerSidePropsContext } from "next";
import {
getServerSession,
type NextAuthOptions,
type DefaultSession,
} from "next-auth";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { prisma } from "./db";
/**
* Module augmentation for `next-auth` types
* Allows us to add custom properties to the `session` object
* and keep type safety
* @see https://next-auth.js.org/getting-started/typescript#module-augmentation
**/
declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
// ...other properties
// role: UserRole;
} & DefaultSession["user"];
}
// interface User {
// // ...other properties
// // role: UserRole;
// }
}
/**
* Options for NextAuth.js used to configure
* adapters, providers, callbacks, etc.
* @see https://next-auth.js.org/configuration/options
**/
export const authOptions: NextAuthOptions = {
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
// session.user.role = user.role; <-- put other properties on the session here
}
return session;
},
},
adapter: PrismaAdapter(prisma),
providers: [
/**
* ...add more providers here
*
* Most other providers require a bit more work than the Discord provider.
* For example, the GitHub provider requires you to add the
* `refresh_token_expires_in` field to the Account model. Refer to the
* NextAuth.js docs for the provider you want to use. Example:
* @see https://next-auth.js.org/providers/github
**/
],
};
/**
* Wrapper for getServerSession so that you don't need
* to import the authOptions in every file.
* @see https://next-auth.js.org/configuration/nextjs
**/
export const getServerAuthSession = (ctx: {
req: GetServerSidePropsContext["req"];
res: GetServerSidePropsContext["res"];
}) => {
return getServerSession(ctx.req, ctx.res, authOptions);
};