Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Bit-Barron committed Oct 28, 2024
1 parent 4f0811a commit 397d939
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 26 deletions.
23 changes: 23 additions & 0 deletions prisma/migrations/20241028234556_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- CreateTable
CREATE TABLE "User" (
"id" TEXT NOT NULL,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Session" (
"id" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"expiresAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "Session_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- AddForeignKey
ALTER TABLE "Session" ADD CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
8 changes: 1 addition & 7 deletions src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
// See https://svelte.dev/docs/kit/types#app.d.ts
// for information about these interfaces
declare global {
namespace App {
interface Locals {
auth: import('lucia').AuthRequest;
user: import('lucia').User | null;
session: import('lucia').Session | null;
}

// interface Error {}
// interface PageData {}
// interface PageState {}
// interface Platform {}
}
}

Expand Down
48 changes: 31 additions & 17 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,42 @@ import type { Handle } from '@sveltejs/kit';

export const handle: Handle = async ({ event, resolve }) => {
const sessionId = event.cookies.get(lucia.sessionCookieName);
console.log('Session ID from cookie:', sessionId);

if (!sessionId) {
console.log('No session ID found');
event.locals.user = null;
event.locals.session = null;
return resolve(event);
}

const { session, user } = await lucia.validateSession(sessionId);
if (session && session.fresh) {
const sessionCookie = lucia.createSessionCookie(session.id);
event.cookies.set(sessionCookie.name, sessionCookie.value, {
path: '.',
...sessionCookie.attributes
});
}
if (!session) {
const sessionCookie = lucia.createBlankSessionCookie();
event.cookies.set(sessionCookie.name, sessionCookie.value, {
path: '.',
...sessionCookie.attributes
});
try {
const { session, user } = await lucia.validateSession(sessionId);
console.log('Session validation result:', { session, user }); // Debug log

if (session && session.fresh) {
const sessionCookie = lucia.createSessionCookie(session.id);
event.cookies.set(sessionCookie.name, sessionCookie.value, {
path: '/',
...sessionCookie.attributes
});
console.log('Set fresh session cookie'); // Debug log
}
if (!session) {
const sessionCookie = lucia.createBlankSessionCookie();
event.cookies.set(sessionCookie.name, sessionCookie.value, {
path: '/',
...sessionCookie.attributes
});
console.log('Set blank session cookie'); // Debug log
}
event.locals.user = user;
event.locals.session = session;
return resolve(event);
} catch (error) {
console.error('Error validating session:', error); // Debug log
event.locals.user = null;
event.locals.session = null;
return resolve(event);
}
event.locals.user = user;
event.locals.session = session;
return resolve(event);
};
11 changes: 11 additions & 0 deletions src/routes/(auth)/login/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import { lucia } from '$lib/server/auth.js';
import { fail, redirect, type Actions } from '@sveltejs/kit';
import { Argon2id } from 'oslo/password';
import prisma from '../../../utils/prisma';
import type { PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ locals }) => {
if (locals.user) {
throw redirect(302, '/');
}

return {
user: locals.user
};
};

export const actions: Actions = {
default: async ({ request, cookies }) => {
Expand Down
11 changes: 11 additions & 0 deletions src/routes/(auth)/register/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { Argon2id } from 'oslo/password';
import { lucia } from '$lib/server/auth.js';
import { redirect, type Actions } from '@sveltejs/kit';
import prisma from '../../../utils/prisma';
import type { PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ locals }) => {
if (locals.user) {
throw redirect(302, '/');
}

return {
user: locals.user
};
};

export const actions = {
default: async ({ request, cookies }) => {
Expand Down
15 changes: 15 additions & 0 deletions src/routes/(main)/dashboard/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from '../../(auth)/login/$types';

export const load: PageServerLoad = async ({ locals }) => {
if (!locals.user) {
throw redirect(302, '/login');
}

return {
user: {
email: locals.user.email,
id: locals.user.id
}
};
};
5 changes: 3 additions & 2 deletions src/routes/(main)/dashboard/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
export let data;
import type { PageData } from './$types';
export let data: PageData;
console.log(data);
console.log('barron', data);
</script>

0 comments on commit 397d939

Please sign in to comment.