Fix verification
This commit is contained in:
parent
d8011f1326
commit
b50a88ee43
3 changed files with 29 additions and 38 deletions
|
@ -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);
|
||||
}
|
28
src/pages/[lang]/confirm/[token].ts
Normal file
28
src/pages/[lang]/confirm/[token].ts
Normal 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);
|
||||
}
|
|
@ -72,7 +72,7 @@ async function sendConfirmationEmail(
|
|||
) {
|
||||
const { t } = makeT(lang);
|
||||
const confirmationUrl = new URL(
|
||||
`${lang}/${letter.id}/${letter.confirmationToken}`,
|
||||
`${lang}/confirm/${letter.confirmationToken}`,
|
||||
import.meta.env.PUBLIC_URL
|
||||
);
|
||||
const from = `${t("website_name")} <info@re-set.cz>`;
|
||||
|
|
Reference in a new issue