-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
53 lines (50 loc) · 1.39 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
import NextAuth, { type DefaultSession } from 'next-auth'
import GitHub from 'next-auth/providers/github'
declare module 'next-auth' {
interface Session {
user: {
/** The user's id. */
id: number
} & DefaultSession['user']
}
}
export const {
handlers: { GET, POST },
auth
} = NextAuth({
providers: [GitHub],
callbacks: {
jwt({ token, profile }) {
if (profile) {
token.id = profile.id
token.image = profile.avatar_url || profile.picture
}
return token
},
// @ts-ignore
session: ({ session, token }) => {
if (session?.user && token?.id) {
// @ts-ignore
session.user.id = String(token.id)
}
return session
},
authorized({ auth, request: { nextUrl } }) {
const isLoggedIn = !!auth?.user
if (nextUrl.pathname.startsWith('/upload')) {
// only allow Gianfranco to upload images
if (!isLoggedIn || !['899175'].includes(auth?.user?.id)) {
const redirectUrl = new URL('sign-in', nextUrl.origin)
redirectUrl.searchParams.append('callbackUrl', nextUrl.href)
return Response.redirect(redirectUrl)
}
}
// this ensures there is a logged in user for -every- request
return isLoggedIn
}
},
pages: {
// overrides the next-auth default signin page https://authjs.dev/guides/basics/pages
signIn: '/sign-in'
}
})