refactor: small tweaks to password reset flow
This commit is contained in:
@@ -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 };
|
||||
}),
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user