mirror of
https://github.com/fosrl/pangolin.git
synced 2025-06-29 08:30:05 +02:00
65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
|
import { Request, Response, NextFunction } from "express";
|
||
|
import createHttpError from "http-errors";
|
||
|
import { z } from "zod";
|
||
|
import { fromError } from "zod-validation-error";
|
||
|
import HttpCode from "@server/types/HttpCode";
|
||
|
import { response } from "@server/utils";
|
||
|
import { validateResourceSessionToken } from "@server/auth/resource";
|
||
|
|
||
|
export const params = z.object({
|
||
|
token: z.string(),
|
||
|
resourceId: z.string().transform(Number).pipe(z.number().int().positive()),
|
||
|
});
|
||
|
|
||
|
export type CheckResourceSessionParams = z.infer<typeof params>;
|
||
|
|
||
|
export type CheckResourceSessionResponse = {
|
||
|
valid: boolean;
|
||
|
};
|
||
|
|
||
|
export async function checkResourceSession(
|
||
|
req: Request,
|
||
|
res: Response,
|
||
|
next: NextFunction,
|
||
|
): Promise<any> {
|
||
|
const parsedParams = params.safeParse(req.params);
|
||
|
|
||
|
if (!parsedParams.success) {
|
||
|
return next(
|
||
|
createHttpError(
|
||
|
HttpCode.BAD_REQUEST,
|
||
|
fromError(parsedParams.error).toString(),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
const { token, resourceId } = parsedParams.data;
|
||
|
|
||
|
try {
|
||
|
const { resourceSession } = await validateResourceSessionToken(
|
||
|
token,
|
||
|
resourceId,
|
||
|
);
|
||
|
|
||
|
let valid = false;
|
||
|
if (resourceSession) {
|
||
|
valid = true;
|
||
|
}
|
||
|
|
||
|
return response<CheckResourceSessionResponse>(res, {
|
||
|
data: { valid },
|
||
|
success: true,
|
||
|
error: false,
|
||
|
message: "Checked validity",
|
||
|
status: HttpCode.OK,
|
||
|
});
|
||
|
} catch (e) {
|
||
|
return next(
|
||
|
createHttpError(
|
||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||
|
"Failed to reset password",
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|