chatbot-template/auth.ts

65 lines
1.8 KiB
TypeScript
Raw Normal View History

import NextAuth from 'next-auth'
2023-06-16 11:49:14 -04:00
import GitHub from 'next-auth/providers/github'
2023-06-22 11:02:20 -07:00
import CredentialsProvider from 'next-auth/providers/credentials'
2023-06-16 11:49:14 -04:00
2023-06-22 11:02:20 -07:00
// We default to using GitHub for authentication for local development and production.
// On Preview deployments, we use a dummy credentials provider. This allows folks to easily
// test the app without having to create a custom GitHub OAuth app or change the callback URL
// just to test the application on previews.
// We have a custom /sign-in page for non-preview environments. In preview environments, the user
// will be redirected to /api/auth/signin instead.
2023-06-16 11:49:14 -04:00
export const {
handlers: { GET, POST },
auth,
CSRF_experimental
// @ts-ignore
2023-06-16 11:49:14 -04:00
} = NextAuth({
2023-06-21 11:17:59 -07:00
// @ts-ignore
2023-06-22 11:02:20 -07:00
providers: [
process.env.VERCEL_ENV === 'preview'
? CredentialsProvider({
name: 'Credentials',
credentials: {
username: {
label: 'Username',
type: 'text',
placeholder: 'jsmith'
},
password: { label: 'Password', type: 'password' }
},
async authorize(credentials) {
return {
id: 1,
name: 'J Smith',
email: 'jsmith@example.com',
picture: 'https://i.pravatar.cc/150?u=jsmith@example.com'
} as any
}
})
: GitHub
],
2023-06-16 11:49:14 -04:00
callbacks: {
// @ts-ignore
2023-06-16 11:49:14 -04:00
jwt: async ({ token, profile }) => {
2023-06-21 14:19:13 -07:00
if (profile?.id) {
2023-06-16 11:49:14 -04:00
token.id = profile.id
token.image = profile.picture
}
return token
2023-06-22 10:37:35 -07:00
},
// @ts-ignore
authorized({ auth }) {
return !!auth?.user
2023-06-22 11:02:20 -07:00
},
trustHost: true
2023-06-16 11:49:14 -04:00
},
2023-06-22 11:02:20 -07:00
...(process.env.VERCEL_ENV === 'preview'
? {
pages: {
signIn: '/sign-in'
}
}
: {})
2023-06-16 11:49:14 -04:00
})