Skip to content

Instantly share code, notes, and snippets.

@tomfa
Last active June 15, 2024 10:39
Show Gist options
  • Save tomfa/4d08c387192d620d8a822f328fd3bec7 to your computer and use it in GitHub Desktop.
Save tomfa/4d08c387192d620d8a822f328fd3bec7 to your computer and use it in GitHub Desktop.
NextAuth: link in console for development
import { type NextAuthOptions } from "next-auth";
import EmailProvider from "next-auth/providers/email";
import { ConsoleEmailLink } from "./dev.auth.ts";
/**
* ConsoleEmailLink will print a login link to the console.
* Enables you to login while developing without internet,
* and makes local setup easier.
*/
export const authOptions: NextAuthOptions = {
// [...your existing options]
providers: [
env.NODE_ENV === "development"
? ConsoleEmailLink()
: EmailProvider({
server: env.EMAIL_URL,
from: env.EMAIL_FROM,
}),
],
};
import type { EmailConfig } from "next-auth/providers/email";
export function ConsoleEmailLink(): EmailConfig {
return {
id: "email",
type: "email",
name: "Email",
server: { host: "localhost", port: 25, auth: { user: "", pass: "" } },
from: "NextAuth <no-reply@example.com>",
maxAge: 24 * 60 * 60,
async sendVerificationRequest({ url }) {
console.log(`----------- <ConsoleEmail> -----------`);
console.log(JSON.stringify({ subject: "Sign in", url }, undefined, 2));
console.log(`----------- </ConsoleEmail> -----------`);
},
options: { server: undefined, from: "console-email@local" },
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment