chatbot-template/auth.ts

40 lines
984 B
TypeScript
Raw Normal View History

2023-06-27 20:03:01 +02:00
import NextAuth, { type DefaultSession } from 'next-auth'
2023-06-16 11:49:14 -04:00
import GitHub from 'next-auth/providers/github'
2023-06-27 20:03:01 +02:00
declare module 'next-auth' {
interface Session {
user: {
/** The user's id. */
id: string
} & DefaultSession['user']
}
}
2023-06-22 11:02:20 -07:00
2023-06-16 11:49:14 -04:00
export const {
handlers: { GET, POST },
2023-11-26 18:30:48 -06:00
auth
2023-06-16 11:49:14 -04:00
} = NextAuth({
2023-06-27 20:03:01 +02:00
providers: [GitHub],
2023-06-16 11:49:14 -04:00
callbacks: {
2023-06-27 20:03:01 +02:00
jwt({ token, profile }) {
if (profile) {
token.id = String(profile.id); // Convert profile.id to a string
token.image = profile.avatar_url || profile.picture
2023-06-16 11:49:14 -04:00
}
return token
2023-06-22 10:37:35 -07:00
},
2023-11-26 18:30:48 -06:00
session: ({ session, token }) => {
if (session?.user && token?.id) {
session.user.id = String(token.id)
}
return session
},
2023-06-22 10:37:35 -07:00
authorized({ auth }) {
2023-07-05 12:00:46 -04:00
return !!auth?.user // this ensures there is a logged in user for -every- request
2023-06-27 20:03:01 +02:00
}
2023-06-16 11:49:14 -04:00
},
2023-06-27 20:03:01 +02:00
pages: {
2023-07-05 12:00:46 -04:00
signIn: '/sign-in' // overrides the next-auth default signin page https://authjs.dev/guides/basics/pages
2023-06-27 20:03:01 +02:00
}
2023-11-26 18:30:48 -06:00
})