great user and redirect to login on /app
This commit is contained in:
parent
1e43b36fe6
commit
6d3a4b0e7a
5 changed files with 44 additions and 8 deletions
|
|
@ -1,11 +1,13 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
let { title }: { title: string } = $props();
|
let { title, username }: { title: string; username: string } = $props();
|
||||||
|
|
||||||
|
let greeting = $derived(`Welcome ${username}!`);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<header class="navbar justify-between bg-base-200 px-4">
|
<header class="navbar justify-between bg-base-200 px-4">
|
||||||
<h2 class="prose prose-xl">Hestia</h2>
|
<h2 class="prose prose-xl">Hestia</h2>
|
||||||
<h1 class="prose prose-2xl">{title}</h1>
|
<h1 class="prose prose-2xl">{title}</h1>
|
||||||
<p class="prose prose-lg">Welcome!</p>
|
<p class="prose prose-lg">{greeting}</p>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { prisma } from '$lib/server/prisma';
|
import { prisma } from '$lib/server/prisma';
|
||||||
import { redirect } from '@sveltejs/kit';
|
import { redirect } from '@sveltejs/kit';
|
||||||
|
|
||||||
export async function load(event) {
|
export async function load(event) {
|
||||||
const sessionId = event.cookies.get('auth_session');
|
const sessionId = event.cookies.get('auth_session');
|
||||||
if (!sessionId) {
|
if (!sessionId) {
|
||||||
|
|
@ -11,7 +12,7 @@ export async function load(event) {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!user) {
|
if (!user) {
|
||||||
redirect(401, '/login');
|
redirect(300, '/login');
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
24
src/routes/app/+layout.server.ts
Normal file
24
src/routes/app/+layout.server.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
import { prisma } from '$lib/server/prisma';
|
||||||
|
import { redirect } from '@sveltejs/kit';
|
||||||
|
import dayjs from 'dayjs';
|
||||||
|
|
||||||
|
export async function load(event) {
|
||||||
|
const sessionId = event.cookies.get('auth_session');
|
||||||
|
if (!sessionId) {
|
||||||
|
redirect(300, '/login');
|
||||||
|
}
|
||||||
|
const session = await prisma.session.findUnique({
|
||||||
|
where: { id: sessionId },
|
||||||
|
include: { user: true },
|
||||||
|
});
|
||||||
|
if (!session) {
|
||||||
|
redirect(300, '/login');
|
||||||
|
}
|
||||||
|
const expiry = session.expiresAt;
|
||||||
|
if (dayjs(expiry).isBefore(dayjs())) {
|
||||||
|
redirect(300, '/login');
|
||||||
|
}
|
||||||
|
|
||||||
|
const { password: _, ...rest } = session.user;
|
||||||
|
return rest;
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,15 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { Navbar } from '$lib/components/Navigation';
|
import { Navbar } from '$lib/components/Navigation';
|
||||||
|
import type { User } from '@prisma/client';
|
||||||
|
import type { Snippet } from 'svelte';
|
||||||
|
|
||||||
let { children } = $props();
|
type Props = {
|
||||||
|
children: Snippet;
|
||||||
|
data: Omit<User, 'password'>;
|
||||||
|
};
|
||||||
|
|
||||||
|
let { children, data }: Props = $props();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Navbar title="Svelte" />
|
<Navbar title="Svelte" username={data.name} />
|
||||||
{@render children()}
|
{@render children()}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
<script lang="ts">
|
||||||
|
</script>
|
||||||
Loading…
Add table
Reference in a new issue