chatbot-template/auth.ts

35 lines
833 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 },
auth,
2023-07-05 12:00:46 -04:00
CSRF_experimental // will be removed in future
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) {
2023-06-16 11:49:14 -04:00
token.id = profile.id
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
},
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-06-16 11:49:14 -04:00
})