Página(s) privada(s)
Una vez que el usuario está autenticado, puedes construir rutas privadas como un dashboard de usuario, una cuenta, etc.
Aquí tienes un ejemplo de un dashboard de usuario sencillo que muestra datos privados del usuario en la página:
---import { getSession } from '@/lib/auth'
const session = getSession(Astro.request)
if (session) { // If need to do something on the server side say fetch data for the user // This is the right block to do it} else { // In case the user is not logged in // Redirect them for example return Astro.redirect('/')}---
<html> <head> </head> <body> The user that's logged name is {session.user.name} </body></html>Aquí tienes un ejemplo sencillo de cómo proteger una ruta en SvelteKit.
Crea un +page.server.ts en (o junto a) el directorio que quieras proteger:
import { error } from '@sveltejs/kit'import type { RequestEvent } from './$types'import { getSession } from '@/lib/utils/auth'
export async function load(event: RequestEvent) { const session = getSession(event.request) if (!session) { // In case the user is not logged in // Send 403 throw error(403, { message: 'Unauthorized' }) } // If need to do something on the server side say fetch data for the user // This is the right block to do it return session}