-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2122679
commit 5441a27
Showing
6 changed files
with
101 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters