From 8152b112a500ba01dac221f2082095843cbef741 Mon Sep 17 00:00:00 2001 From: Ben Hindman Date: Tue, 23 Jul 2024 16:21:41 -0400 Subject: [PATCH] Allow dev user to sign-in using credentials --- app/api/auth/[...nextauth]/options.ts | 37 +++++++++++++++++++-------- app/env.ts | 4 +++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/api/auth/[...nextauth]/options.ts b/app/api/auth/[...nextauth]/options.ts index 3abcbe8..912dc23 100644 --- a/app/api/auth/[...nextauth]/options.ts +++ b/app/api/auth/[...nextauth]/options.ts @@ -1,5 +1,6 @@ import GoogleProvider from "next-auth/providers/google" import GitHubProvider from "next-auth/providers/github" +import CredentialsProvider from "next-auth/providers/credentials" import type { Provider } from "next-auth/providers" import { env } from "../../../env" import { CallbacksOptions } from "next-auth" @@ -22,8 +23,30 @@ const providers: Provider[] = [ }), ] +if (env.NODE_ENV === "development") { + providers.push( + CredentialsProvider({ + name: "Credentials", + credentials: { + username: { label: "Username", type: "text" }, + password: { label: "Password", type: "password" }, + }, + authorize: async (credentials) => { + if (credentials?.username === "admin" && credentials?.password === "admin") { + return { id: "1", name: "admin", email: "satoshi@flashapp.me" } + } + return null + }, + }), + ) +} + const callbacks: Partial = { - async signIn({ account, profile }) { + async signIn({ account, profile, user }) { + if (account?.provider === "credentials" && env.NODE_ENV === "development") { + return !!user + } + if (!account || !profile) { return false } @@ -33,15 +56,9 @@ const callbacks: Partial = { return false } - if (account.provider === "google") { - const verified = new Boolean("email_verified" in profile && profile.email_verified) - return verified && env.AUTHORIZED_EMAILS.includes(email) - } - - if (account.provider === "github") { - return env.AUTHORIZED_EMAILS.includes(email) - } - return false + // eslint-disable-next-line no-new-wrappers + const verified = new Boolean("email_verified" in profile && profile.email_verified) + return verified && env.AUTHORIZED_EMAILS.includes(email) }, } diff --git a/app/env.ts b/app/env.ts index d287258..ac3fbf7 100644 --- a/app/env.ts +++ b/app/env.ts @@ -24,6 +24,9 @@ export const env = createEnv({ * 💡 You'll get type errors if these are not prefixed with NEXT_PUBLIC_. */ client: {}, + shared: { + NODE_ENV: z.string(), + }, /* * Due to how Next.js bundles environment variables on Edge and Client, * we need to manually destructure them to make sure all are included in bundle. @@ -31,6 +34,7 @@ export const env = createEnv({ * 💡 You'll get type errors if not all variables from `server` & `client` are included here. */ runtimeEnv: { + NODE_ENV: process.env.NODE_ENV, GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET, GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID,