-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
55 lines (50 loc) · 1.87 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
import {Account, getServerSession, Profile, Session, User} from "next-auth"
import Google from "next-auth/providers/google"
import {JWT} from "next-auth/jwt";
import {AdapterUser} from "next-auth/adapters";
import {NextApiRequest, NextApiResponse} from "next";
import {NextRequest} from "next/server";
// Read more at: https://next-auth.js.org/getting-started/typescript#module-augmentation
declare module "next-auth/jwt" {
interface JWT {
/** The user's role. */
userRole?: "admin"
}
}
export const config = {
providers: [
Google({ clientId: process.env.AUTH_GOOGLE_ID, clientSecret: process.env.AUTH_GOOGLE_SECRET })
],
callbacks: {
async jwt({ token }: {
token: JWT;
user: User | AdapterUser;
account: Account | null;
profile?: Profile | undefined;
trigger?: "signIn" | "signUp" | "update" | undefined;
isNewUser?: boolean | undefined; session?: any; }) {
token.userRole = "admin"
return token
},
},
};
// Helper function to get session without passing config every time
// https://next-auth.js.org/configuration/nextjs#getserversession
export function auth(request: NextRequest, response: Response|null): Promise<Session|null> {
const res = response ? response : new Response(null, {});
return getServerSession(request as unknown as NextApiRequest, {
...res,
getHeader: (name: string) => res.headers?.get(name),
setHeader: (name: string, value: string) => res.headers?.set(name, value),
} as unknown as NextApiResponse, config as any)
}
// We recommend doing your own environment variable validation
declare global {
namespace NodeJS {
export interface ProcessEnv {
NEXTAUTH_SECRET: string
AUTH_GOOGLE_ID: string
AUTH_GOOGLE_SECRET: string
}
}
}