fix: unable to change password when using magic link (#484)

* fix: fixed password not resetting & added password prompt with save guards

* fix: migrated to using setPassword
This commit is contained in:
Morfixx
2026-05-11 05:40:59 -07:00
committed by GitHub
parent 607672718c
commit 71205b67dc
6 changed files with 218 additions and 62 deletions

View File

@@ -1,8 +1,8 @@
import { count, desc, eq } from "drizzle-orm";
import { and, count, desc, eq, isNotNull } from "drizzle-orm";
import { v4 as uuidv4 } from "uuid";
import type { dbClient } from "@kan/db/client";
import { apikey, users } from "@kan/db/schema";
import { account, apikey, users } from "@kan/db/schema";
export const getCount = async (db: dbClient) => {
const result = await db.select({ count: count() }).from(users);
@@ -11,27 +11,58 @@ export const getCount = async (db: dbClient) => {
};
export const getById = async (db: dbClient, userId: string) => {
return await db.query.users.findFirst({
columns: {
id: true,
name: true,
email: true,
image: true,
stripeCustomerId: true,
},
with: {
apiKeys: {
columns: {
id: true,
prefix: true,
key: true,
},
orderBy: desc(apikey.createdAt),
limit: 1,
const [user, credentialAccount, magicLinkAccount] = await Promise.all([
db.query.users.findFirst({
columns: {
id: true,
name: true,
email: true,
image: true,
stripeCustomerId: true,
},
},
where: eq(users.id, userId),
});
with: {
apiKeys: {
columns: {
id: true,
prefix: true,
key: true,
},
orderBy: desc(apikey.createdAt),
limit: 1,
},
},
where: eq(users.id, userId),
}),
db
.select({ id: account.id })
.from(account)
.where(
and(
eq(account.userId, userId),
eq(account.providerId, "credential"),
isNotNull(account.password),
),
)
.limit(1),
db
.select({ id: account.id })
.from(account)
.where(
and(
eq(account.userId, userId),
eq(account.providerId, "magic-link"),
),
)
.limit(1),
]);
if (!user) return undefined;
return {
...user,
hasPassword: credentialAccount.length > 0,
hasMagicLinkAccount: magicLinkAccount.length > 0,
};
};
export const getByStripeCustomerId = async (