import Toast from '@components/Toast'; import { Env, isAuthorised, loginWithToken } from 'cloudflare-auth'; import { html, view } from '@lib/html'; export const onRequestPost: PagesFunction = async ({ request, env }) => { const url = new URL(request.url); const data = await request.formData(); const email = data.get('email') as string; if (!email) { return view(Toast('Email not specified')); } const magicLink = await loginWithToken(email, env, url.origin, true); try { return view( Toast( 'Click to login: ' + magicLink + '', 'alert-success' ) ); } catch { return view(Toast('Magic link failed to send!', 'alert-failure')); } }; export const onRequestGet: PagesFunction = async ({ env, request }) => { const loggedIn = await isAuthorised(request, env); if (loggedIn) { console.log('redirect to dashboard'); const url = new URL(request.url); return Response.redirect(url.origin + '/dashboard', 303); } return view(html`
Login
`); };