refactor: small tweaks to password reset flow

This commit is contained in:
Henry
2026-05-11 13:43:35 +01:00
parent 54dcd08d9a
commit 3d9e859b31
4 changed files with 50 additions and 20 deletions

View File

@@ -2,9 +2,9 @@ import { TRPCError } from "@trpc/server";
import { z } from "zod";
import * as userRepo from "@kan/db/repository/user.repo";
import { generateAvatarUrl } from "@kan/shared/utils";
import { createTRPCRouter, protectedProcedure } from "../trpc";
import { generateAvatarUrl } from "@kan/shared/utils";
export const userRouter = createTRPCRouter({
getUser: protectedProcedure
@@ -119,6 +119,17 @@ export const userRouter = createTRPCRouter({
};
}),
setPassword: protectedProcedure
.meta({
openapi: {
method: "POST",
path: "/users/me/password",
summary: "Set password",
description:
"Sets a password for a user who signed up via magic link and has no password yet",
tags: ["Users"],
protect: true,
},
})
.input(
z.object({
newPassword: z
@@ -152,7 +163,14 @@ export const userRouter = createTRPCRouter({
});
}
await ctx.auth.api.setPassword({ newPassword: input.newPassword });
try {
await ctx.auth.api.setPassword({ newPassword: input.newPassword });
} catch {
throw new TRPCError({
message: "Failed to set password",
code: "INTERNAL_SERVER_ERROR",
});
}
return { success: true };
}),

View File

@@ -4,6 +4,9 @@ import { v4 as uuidv4 } from "uuid";
import type { dbClient } from "@kan/db/client";
import { account, apikey, users } from "@kan/db/schema";
const PROVIDER_CREDENTIAL = "credential";
const PROVIDER_MAGIC_LINK = "magic-link";
export const getCount = async (db: dbClient) => {
const result = await db.select({ count: count() }).from(users);
@@ -39,7 +42,7 @@ export const getById = async (db: dbClient, userId: string) => {
.where(
and(
eq(account.userId, userId),
eq(account.providerId, "credential"),
eq(account.providerId, PROVIDER_CREDENTIAL),
isNotNull(account.password),
),
)
@@ -50,7 +53,7 @@ export const getById = async (db: dbClient, userId: string) => {
.where(
and(
eq(account.userId, userId),
eq(account.providerId, "magic-link"),
eq(account.providerId, PROVIDER_MAGIC_LINK),
),
)
.limit(1),