Preload a CSRF token on the registration page
All checks were successful
Playwright Tests / test (pull_request) Has been skipped
Check usage of free licenses / build-static-assets (pull_request) Successful in 28s
Add copyright notice / copyright_notice (pull_request) Successful in 42s

This commit is contained in:
Manuel Bustillo 2024-12-08 09:31:31 +01:00
parent f68587419d
commit b4cfd91ff4
2 changed files with 27 additions and 6 deletions

View File

@ -5,10 +5,22 @@
import LoginForm from '@/app/ui/components/login-form';
import RegistrationForm from '@/app/ui/components/registration-form';
import { useParams } from 'next/navigation'
import { useEffect } from 'react';
import { retrieveCSRFToken } from '../api/authentication';
import { getCsrfToken } from '../lib/utils';
export default async function Page() {
const params = useParams<{ slug: string }>()
useEffect(() => {
if (getCsrfToken() == 'unknown') {
retrieveCSRFToken();
}
}, []);
if (typeof window !== 'undefined') {
localStorage.setItem('slug', await params.slug);
}
return (
<main className="flex min-h-screen flex-col p-6">

View File

@ -43,6 +43,15 @@ function flattenErrors(errors: StructuredErrors): string[] {
});
}
// At this moment we're making an initial request to get a valid CSRF token
export function retrieveCSRFToken() {
return fetch(`/api/token`, {
headers: {
'Accept': 'application/json',
}
}).then((response) => { return null });
}
export function register({ slug, email, password, passwordConfirmation, captcha, onRegister, onError }: {
slug: string,
email: string,