User.email and implementation

This commit is contained in:
2026-01-28 21:34:26 +00:00
parent d4cc50f289
commit c0e06ac8ba
12 changed files with 1220 additions and 19 deletions

View File

@@ -5,10 +5,14 @@ import { db } from "../client";
export async function createUser(
name: string,
username: string,
email: string,
passwordHash: string,
avatarURL?: string | null,
) {
const [user] = await db.insert(User).values({ name, username, passwordHash, avatarURL }).returning();
const [user] = await db
.insert(User)
.values({ name, username, email, passwordHash, avatarURL })
.returning();
return user;
}
@@ -22,6 +26,11 @@ export async function getUserByUsername(username: string) {
return user;
}
export async function getUserByEmail(email: string) {
const [user] = await db.select().from(User).where(eq(User.email, email));
return user;
}
export async function updateById(
id: number,
updates: {

View File

@@ -2,6 +2,7 @@ import { RegisterRequestSchema } from "@sprint/shared";
import type { BunRequest } from "bun";
import { buildAuthCookie, generateToken, hashPassword } from "../../auth/utils";
import { createSession, createUser, getUserByUsername } from "../../db/queries";
import { getUserByEmail } from "../../db/queries/users";
import { errorResponse, parseJsonBody } from "../../validation";
export default async function register(req: BunRequest) {
@@ -12,15 +13,20 @@ export default async function register(req: BunRequest) {
const parsed = await parseJsonBody(req, RegisterRequestSchema);
if ("error" in parsed) return parsed.error;
const { name, username, password, avatarURL } = parsed.data;
const { name, username, email, password, avatarURL } = parsed.data;
const existing = await getUserByUsername(username);
if (existing) {
const existingUsername = await getUserByUsername(username);
if (existingUsername) {
return errorResponse("username already taken", "USERNAME_TAKEN", 400);
}
const existingEmail = await getUserByEmail(email);
if (existingEmail) {
return errorResponse("email already registered", "EMAIL_TAKEN", 400);
}
const passwordHash = await hashPassword(password);
const user = await createUser(name, username, passwordHash, avatarURL);
const user = await createUser(name, username, email, passwordHash, avatarURL);
if (!user) {
return errorResponse("failed to create user", "USER_CREATE_ERROR", 500);
}

View File

@@ -39,10 +39,8 @@ async function handler(req: AuthedRequest) {
const quantity = Math.max(1, totalMembers - 5);
const priceId = billingPeriod === "annual" ? STRIPE_PRICE_ANNUAL : STRIPE_PRICE_MONTHLY;
// build customer data - use username as email if no email field exists
const customerEmail = user.username.includes("@")
? user.username
: `${user.username}@localhost.local`;
// use the user's email from the database
const customerEmail = user.email;
const session = await stripe.checkout.sessions.create({
customer_email: customerEmail,