Fix verification

This commit is contained in:
Ondřej 2024-02-26 04:16:26 +01:00
parent d8011f1326
commit b50a88ee43
3 changed files with 29 additions and 38 deletions

View file

@ -1,37 +0,0 @@
import type { APIContext } from "astro";
import { db } from "../../../../db";
import type { Lang } from "../../../../lang";
export const prerender = false;
interface Params extends Record<string, string> {
lang: Lang;
letterId: string;
token: string;
}
export async function GET({ params, redirect }: APIContext<never, Params>) {
const letterId = parseInt(params.letterId);
if (Number.isNaN(letterId)) {
return new Response(null, { status: 400 });
}
const letter = await db
.selectFrom("letters")
.where("id", "=", letterId)
.select(["confirmationToken", "confirmed"])
.executeTakeFirst();
if (!letter) {
return new Response(null, { status: 404 });
}
if (letter.confirmationToken !== params.token) {
return new Response(null, { status: 400 });
}
if (!letter.confirmed) {
db.updateTable("letters").set({ confirmed: 1 }).where("id", "=", letterId).execute();
}
return redirect(`/${params.lang}/email-confirmed`, 301);
}

View file

@ -0,0 +1,28 @@
import type { APIContext } from "astro";
import { db } from "../../../db";
import type { Lang } from "../../../lang";
export const prerender = false;
interface Params extends Record<string, string> {
lang: Lang;
token: string;
}
export async function GET({ params: { lang, token }, redirect }: APIContext<never, Params>) {
const letter = await db
.selectFrom("letters")
.where("confirmationToken", "=", token)
.select(["id", "confirmationToken", "confirmed"])
.executeTakeFirst();
if (!letter) {
return new Response(null, { status: 404 });
}
if (!letter.confirmed) {
db.updateTable("letters").set({ confirmed: 1 }).where("id", "=", letter.id).execute();
}
return redirect(`/${lang}/email-confirmed`, 301);
}

View file

@ -72,7 +72,7 @@ async function sendConfirmationEmail(
) { ) {
const { t } = makeT(lang); const { t } = makeT(lang);
const confirmationUrl = new URL( const confirmationUrl = new URL(
`${lang}/${letter.id}/${letter.confirmationToken}`, `${lang}/confirm/${letter.confirmationToken}`,
import.meta.env.PUBLIC_URL import.meta.env.PUBLIC_URL
); );
const from = `${t("website_name")} <info@re-set.cz>`; const from = `${t("website_name")} <info@re-set.cz>`;