mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-28 21:58:15 +02:00
fix reset password sql error
This commit is contained in:
parent
7797c6c770
commit
8dd30c88ab
2 changed files with 43 additions and 11 deletions
|
@ -11,7 +11,7 @@ import {
|
||||||
users
|
users
|
||||||
} from "@server/db/schema";
|
} from "@server/db/schema";
|
||||||
import db from "@server/db";
|
import db from "@server/db";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq, inArray } from "drizzle-orm";
|
||||||
import config from "@server/lib/config";
|
import config from "@server/lib/config";
|
||||||
import type { RandomReader } from "@oslojs/crypto/random";
|
import type { RandomReader } from "@oslojs/crypto/random";
|
||||||
import { generateRandomString } from "@oslojs/crypto/random";
|
import { generateRandomString } from "@oslojs/crypto/random";
|
||||||
|
@ -95,12 +95,36 @@ export async function validateSessionToken(
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function invalidateSession(sessionId: string): Promise<void> {
|
export async function invalidateSession(sessionId: string): Promise<void> {
|
||||||
await db.delete(resourceSessions).where(eq(resourceSessions.userSessionId, sessionId));
|
try {
|
||||||
await db.delete(sessions).where(eq(sessions.sessionId, sessionId));
|
await db.transaction(async (trx) => {
|
||||||
|
await trx
|
||||||
|
.delete(resourceSessions)
|
||||||
|
.where(eq(resourceSessions.userSessionId, sessionId));
|
||||||
|
await trx.delete(sessions).where(eq(sessions.sessionId, sessionId));
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
logger.error("Failed to invalidate session", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function invalidateAllSessions(userId: string): Promise<void> {
|
export async function invalidateAllSessions(userId: string): Promise<void> {
|
||||||
await db.delete(sessions).where(eq(sessions.userId, userId));
|
try {
|
||||||
|
await db.transaction(async (trx) => {
|
||||||
|
const userSessions = await trx
|
||||||
|
.select()
|
||||||
|
.from(sessions)
|
||||||
|
.where(eq(sessions.userId, userId));
|
||||||
|
await trx.delete(resourceSessions).where(
|
||||||
|
inArray(
|
||||||
|
resourceSessions.userSessionId,
|
||||||
|
userSessions.map((s) => s.sessionId)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
await trx.delete(sessions).where(eq(sessions.userId, userId));
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
logger.error("Failed to all invalidate user sessions", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function serializeSessionCookie(
|
export function serializeSessionCookie(
|
||||||
|
|
|
@ -149,8 +149,6 @@ export async function resetPassword(
|
||||||
|
|
||||||
const passwordHash = await hashPassword(newPassword);
|
const passwordHash = await hashPassword(newPassword);
|
||||||
|
|
||||||
await invalidateAllSessions(resetRequest[0].userId);
|
|
||||||
|
|
||||||
await db.transaction(async (trx) => {
|
await db.transaction(async (trx) => {
|
||||||
await trx
|
await trx
|
||||||
.update(users)
|
.update(users)
|
||||||
|
@ -162,11 +160,21 @@ export async function resetPassword(
|
||||||
.where(eq(passwordResetTokens.email, email));
|
.where(eq(passwordResetTokens.email, email));
|
||||||
});
|
});
|
||||||
|
|
||||||
await sendEmail(ConfirmPasswordReset({ email }), {
|
try {
|
||||||
from: config.getNoReplyEmail(),
|
await invalidateAllSessions(resetRequest[0].userId);
|
||||||
to: email,
|
} catch (e) {
|
||||||
subject: "Password Reset Confirmation"
|
logger.error("Failed to invalidate user sessions", e);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await sendEmail(ConfirmPasswordReset({ email }), {
|
||||||
|
from: config.getNoReplyEmail(),
|
||||||
|
to: email,
|
||||||
|
subject: "Password Reset Confirmation"
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
logger.error("Failed to send password reset confirmation email", e);
|
||||||
|
}
|
||||||
|
|
||||||
return response<ResetPasswordResponse>(res, {
|
return response<ResetPasswordResponse>(res, {
|
||||||
data: null,
|
data: null,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue