Skip to content

Instantly share code, notes, and snippets.

@mortenson
Created August 2, 2026 20:51
Show Gist options
  • Select an option

  • Save mortenson/c999eb7071a16e073cd3b52fe9cecf17 to your computer and use it in GitHub Desktop.

Select an option

Save mortenson/c999eb7071a16e073cd3b52fe9cecf17 to your computer and use it in GitHub Desktop.
A better auth plugin that allows you to quickly create and login as different users
import { BetterAuthPlugin, User } from "better-auth";
import { createAuthEndpoint } from "better-auth/api";
import { setSessionCookie } from "better-auth/cookies";
import z from "zod";
// A debug plugin that allows you to quickly create and login as different users.
// Usage: Visit "/debug-login?name=myuser" in your browser to get a session for myuser@example.com
// Note: only add to your plugin list if not on production
export const debugLoginPlugin = () => {
return {
id: "debug-login-plugin",
endpoints: {
postDebugLogin: createAuthEndpoint(
"/debug-login",
{
method: "GET",
query: z.object({
name: z.string().regex(/^[a-zA-Z0-9-]+$/),
}),
},
async (ctx) => {
// Hardcoding @example.com is a safeguard in case this is added to production in error
const email = `${ctx.query.name}@example.com`;
let user: User | null = await ctx.context.adapter.findOne({
model: "user",
where: [{ field: "email", value: email }],
});
if (!user) {
user = await ctx.context.adapter.create({
model: "user",
data: {
email: email,
name: ctx.query.name,
emailVerified: true,
},
});
}
if (!user) {
throw new Error("Oopsie doodle");
}
const session = await ctx.context.internalAdapter.createSession(user.id);
ctx.context.setNewSession({
session,
user,
});
await setSessionCookie(ctx, {
session,
user,
});
throw ctx.redirect("/");
},
),
},
} satisfies BetterAuthPlugin;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment