Add Democratic fundraising platform.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
31
src/auth.ts
31
src/auth.ts
@@ -2,29 +2,48 @@ import NextAuth from "next-auth";
|
||||
import Credentials from "next-auth/providers/credentials";
|
||||
import bcrypt from "bcryptjs";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
isEmailShape,
|
||||
normalizeEmail,
|
||||
normalizeUsername,
|
||||
USERNAME_RE,
|
||||
} from "@/lib/account-identifiers";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { authConfig } from "./auth.config";
|
||||
|
||||
const credentialsSchema = z.object({
|
||||
email: z.string().email(),
|
||||
email: z.string().trim().min(1),
|
||||
password: z.string().min(1),
|
||||
});
|
||||
|
||||
export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
trustHost: true,
|
||||
session: { strategy: "jwt", maxAge: 30 * 24 * 60 * 60 },
|
||||
...authConfig,
|
||||
providers: [
|
||||
Credentials({
|
||||
name: "Credentials",
|
||||
credentials: {
|
||||
email: { label: "Email", type: "email" },
|
||||
email: { label: "Email or username", type: "text" },
|
||||
password: { label: "Password", type: "password" },
|
||||
},
|
||||
async authorize(raw) {
|
||||
const parsed = credentialsSchema.safeParse(raw);
|
||||
if (!parsed.success) return null;
|
||||
|
||||
const { email, password } = parsed.data;
|
||||
const user = await prisma.user.findUnique({ where: { email } });
|
||||
const identifier = parsed.data.email;
|
||||
const password = parsed.data.password;
|
||||
|
||||
let user = null;
|
||||
if (isEmailShape(identifier)) {
|
||||
const emailLookup = normalizeEmail(identifier);
|
||||
user = await prisma.user.findFirst({
|
||||
where: { email: { equals: emailLookup, mode: "insensitive" } },
|
||||
});
|
||||
} else {
|
||||
const loginName = normalizeUsername(identifier);
|
||||
if (!USERNAME_RE.test(loginName)) return null;
|
||||
user = await prisma.user.findUnique({ where: { username: loginName } });
|
||||
}
|
||||
|
||||
if (!user?.passwordHash) return null;
|
||||
|
||||
const ok = await bcrypt.compare(password, user.passwordHash);
|
||||
|
||||
Reference in New Issue
Block a user