---
import PasswortZuruecksetzenModule from "../../modules/Auth/PasswortZuruecksetzenModule.svelte";
import { validateAccessTokenServer } from "#server/lib/validateAccessToken";
import MinimalLayout from "#layouts/MinimalLayout.astro";
import { decodeToken } from "#lib/auth/token";
import { TokenType } from "#lib/auth/types";

const valid = await validateAccessTokenServer(Astro)

if (valid) {
	return Astro.redirect("/dashboard")
}

const token = Astro.url.searchParams.get("t")

if (!token) {
	return Astro.redirect("/")
}

const decoded = decodeToken(token)

console.log(decoded.exp, Date.now());


if (!decoded.exp || decoded.exp < Date.now() || decoded.typ !== TokenType.Reset) {
	return Astro.redirect("/")
}
---

<MinimalLayout title="Passwort Vergessen">
	<PasswortZuruecksetzenModule token={token} client:load></PasswortZuruecksetzenModule>
</MinimalLayout>
