-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
83 lines (77 loc) · 2.28 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import type { DefaultSession, NextAuthOptions, Session } from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
import GoogleProvider from 'next-auth/providers/google'
import { serverEnv } from '@/env/server'
import { prisma } from '@/lib/prisma'
import { PrismaAdapter } from '@next-auth/prisma-adapter'
import { Role } from '@prisma/client'
// Helpful example: https://github.com/mikemajara/nextjs-prisma-next-auth-credentials/blob/main/pages/api/auth/%5B...nextauth%5D.ts
export const authOptions: NextAuthOptions = {
debug: serverEnv.DEBUG,
adapter: PrismaAdapter(prisma),
secret: serverEnv.NEXTAUTH_SECRET,
theme: {
colorScheme: 'light',
logo: '/images/logo-light.svg',
},
providers: [
GoogleProvider({
clientId: serverEnv.GOOGLE_CLIENT_ID,
clientSecret: serverEnv.GOOGLE_CLIENT_SECRET,
}),
],
callbacks: {
// TODO: Use signIn to handle blocking of users
session: async ({ session, token, user }) => {
return {
...session,
user: {
...session.user,
id: (user || token).id,
role: (user || token).role,
image: (user || token).image,
email: (user || token).email,
},
} as Session
},
},
}
if (serverEnv.MOCK_LOGIN) {
// TODO: I couldn’t get the DB-based mock login to work, so I’m using this
authOptions.session = { strategy: 'jwt' }
authOptions.providers.push(
CredentialsProvider({
name: 'Mock Login',
credentials: {},
authorize: async () => ({}),
})
)
authOptions.callbacks!.jwt = async ({ token }) => {
const email = 'mock.user@example.com'
const name = 'Mock User'
const image = serverEnv.NEXT_APP_URL + '/images/logo-min-light.png'
const role = Role.USER
const user = await prisma.user.upsert({
select: { id: true, name: true, role: true, image: true, email: true },
where: { email },
create: { email, name, image, role },
update: { email, name, image, role },
})
return { ...token, ...user }
}
}
declare module 'next-auth' {
interface Session extends DefaultSession {
user: {
id: string
name: string
email: string
image?: string | null
role: Role
}
expires: string
}
interface User {
role: Role
}
}