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 2122679 commit 5441a27
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 22 deletions.
23 changes: 23 additions & 0 deletions prisma/migrations/20241028225832_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"
53 changes: 53 additions & 0 deletions src/routes/(auth)/login/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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';

export const actions: Actions = {
default: async ({ request, cookies }) => {
try {
const formData = await request.formData();
const { email, password } = Object.fromEntries(formData) as Record<string, string>;

if (!email || !password) {
return fail(400, {
message: 'Email and password are required'
});
}

const user = await prisma.user.findUnique({
where: { email }
});

console.log(user);

const validPassword = user ? await new Argon2id().verify(user.password, password) : false;

if (!user || !validPassword) {
return fail(400, {
message: 'Incorrect username or password'
});
}

const session = await lucia.createSession(user.id, []);
const sessionCookie = lucia.createSessionCookie(session.id);

cookies.set(sessionCookie.name, sessionCookie.value, {
path: '/',
...sessionCookie.attributes
});

throw redirect(302, '/');
} catch (error) {
console.error('Login error:', error);

if (error instanceof Response) {
throw error;
}

return fail(500, {
message: 'An error occurred during login'
});
}
}
};
9 changes: 5 additions & 4 deletions src/routes/(auth)/login/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { enhance } from '$app/forms';
import { Button } from '$lib/components/ui/button/index.js';
import * as Card from '$lib/components/ui/card/index.js';
import { Input } from '$lib/components/ui/input/index.js';
Expand All @@ -11,10 +12,10 @@
<Card.Description>Enter your email below to login to your account</Card.Description>
</Card.Header>
<Card.Content>
<div class="grid gap-4">
<form use:enhance method="POST" class="grid gap-4">
<div class="grid gap-2">
<Label for="email">Email</Label>
<Input id="email" type="email" placeholder="m@example.com" required />
<Input id="email" name="email" type="email" placeholder="m@example.com" required />
</div>
<div class="grid gap-2">
<div class="flex items-center">
Expand All @@ -23,11 +24,11 @@
Forgot your password?
</a>
</div>
<Input id="password" type="password" required />
<Input id="password" name="password" type="password" required />
</div>
<Button type="submit" class="w-full">Login</Button>
<Button variant="outline" class="w-full">Login with Google</Button>
</div>
</form>
<div class="mt-4 text-center text-sm">
Don&apos;t have an account?
<a href="/register" class="underline">Sign up</a>
Expand Down
7 changes: 3 additions & 4 deletions src/routes/(auth)/register/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { generateId } from 'lucia';
import { Argon2id } from 'oslo/password';
import { lucia } from '$lib/server/auth.js';
import { redirect } from '@sveltejs/kit';
import { redirect, type Actions } from '@sveltejs/kit';
import prisma from '../../../utils/prisma';

export const actions = {
default: async ({ request, cookies }: any) => {
default: async ({ request, cookies }) => {
const data = await request.formData();

console.log(data);
Expand All @@ -28,4 +27,4 @@ export const actions = {
});
redirect(302, '/');
}
};
} satisfies Actions;
28 changes: 14 additions & 14 deletions src/routes/(auth)/register/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
<Card.Description>Enter your information to create an account</Card.Description>
</Card.Header>
<Card.Content>
<form method="post" use:enhance>
<div class="grid gap-4">
<div class="grid gap-2">
<Label for="email">Email</Label>
<Input id="email" type="email" placeholder="m@example.com" required />
</div>
<div class="grid gap-2">
<Label for="password">Password</Label>
<Input id="password" type="password" />
</div>
<form method="POST" use:enhance class="grid gap-4">
<div class="grid gap-2">
<Label for="email">Email</Label>
<Input id="email" name="email" type="email" placeholder="m@example.com" required />
</div>
<div class="grid gap-2">
<Label for="password">Password</Label>
<Input id="password" name="password" type="password" required />
</div>
<div class="grid gap-2">
<Button type="submit" class="w-full">Create an account</Button>
<Button variant="outline" class="w-full">Sign up with GitHub</Button>
<Button variant="outline" class="w-full" type="button">Sign up with GitHub</Button>
</div>
<div class="mt-4 text-center text-sm">
Already have an account?
<a href="/login" class="underline"> Sign in </a>
<a href="/login" class="underline">Sign in</a>
</div>
</form></Card.Content
>
</form>
</Card.Content>
</Card.Root>

0 comments on commit 5441a27

Please sign in to comment.