Skip to content

Commit

Permalink
Update auth for preview environments
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpalmer committed Jun 22, 2023
1 parent 7436269 commit f957405
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions auth.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
import NextAuth from 'next-auth'
import GitHub from 'next-auth/providers/github'
import CredentialsProvider from 'next-auth/providers/credentials'

// 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.
export const {
handlers: { GET, POST },
auth,
CSRF_experimental
// @ts-ignore
} = NextAuth({
// @ts-ignore
providers: [GitHub],
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: '[email protected]',
picture: 'https://i.pravatar.cc/[email protected]'
} as any
}
})
: GitHub
],
debugger: true,
callbacks: {
// @ts-ignore
jwt: async ({ token, profile }) => {
Expand All @@ -21,9 +52,14 @@ export const {
// @ts-ignore
authorized({ auth }) {
return !!auth?.user
}
},
trustHost: true
},
pages: {
signIn: '/sign-in'
}
...(process.env.VERCEL_ENV === 'preview'
? {
pages: {
signIn: '/sign-in'
}
}
: {})
})

0 comments on commit f957405

Please sign in to comment.