Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dev user to sign-in using credentials #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions app/api/auth/[...nextauth]/options.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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<CallbacksOptions> = {
async signIn({ account, profile }) {
async signIn({ account, profile, user }) {
if (account?.provider === "credentials" && env.NODE_ENV === "development") {
return !!user
}

if (!account || !profile) {
return false
}
Expand All @@ -33,15 +56,9 @@ const callbacks: Partial<CallbacksOptions> = {
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)
},
}

Expand Down
4 changes: 4 additions & 0 deletions app/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ 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.
*
* 💡 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,
Expand Down
Loading