diff --git a/bun.lockb b/bun.lockb index 0e129f7..e7a0ea7 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index d703f43..eadb1be 100755 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "@lordicon/element": "^1.9.0", "@prisma/client": "^5.15.1", "@sentry/sveltekit": "^7.113.0", + "@supabase/supabase-js": "^2.45.3", "bcryptjs": "^2.4.3", "bits-ui": "^0.21.13", "bytemd": "^1.21.0", diff --git a/src/lib/supabaseClient.js b/src/lib/supabaseClient.js new file mode 100644 index 0000000..16ef866 --- /dev/null +++ b/src/lib/supabaseClient.js @@ -0,0 +1,10 @@ +import { createClient } from "@supabase/supabase-js"; + +const supabaseUrl = process.env.SUPABASE_URL || ''; +const supabaseKey = process.env.SUPABASE_ANON_KEY || ''; + +if (!supabaseUrl || !supabaseKey) { + throw new Error('Supabase URL and key must be defined'); +} + +export const supabase = createClient(supabaseUrl, supabaseKey); \ No newline at end of file diff --git a/src/routes/(auth)/Profile/+page.server.ts b/src/routes/(auth)/Profile/+page.server.ts index a6f3ad8..62e22de 100755 --- a/src/routes/(auth)/Profile/+page.server.ts +++ b/src/routes/(auth)/Profile/+page.server.ts @@ -2,7 +2,10 @@ import { fail, redirect } from '@sveltejs/kit'; import type { Action, Actions, PageServerLoad } from './$types'; import { getDbInstance } from '$lib/database'; import type { Prisma } from '@prisma/client'; +import bcrypt from 'bcryptjs'; // Import bcrypt + const db = getDbInstance(); + export const load: PageServerLoad = async (event) => { const sessionID = event.cookies.get('session'); const session = await event.locals.auth(); @@ -48,5 +51,53 @@ export const actions: Actions = { }, hidePasswords: async ({}) => { return { displayPassword: [] }; + }, + updatePassword: async ({ request, locals }) => { + const formData = await request.formData(); + const currentPassword = formData.get('currentPassword') as string; + const userId = formData.get('id') as string; + const newPassword = formData.get('newPassword') as string; + + if (!userId) { + return fail(401, { + error: "Unauthorized! \t If you used OAuth You can't change your password." + }); + } + + try { + // Verify the current password + const user = await db.user.findUnique({ + where: { id: userId } + }); + + if (!user) { + return fail(404, { error: 'User not found' }); + } + + // Check if the user has a password hash + if (user.passwordHash) { + const correctPassword = await bcrypt.compare(currentPassword, user?.passwordHash); + + if (!correctPassword) { + return fail(401, { error: 'Incorrect password' }); + // Redirect to the profile page or return a success message + } else { + // Hash the new password using bcrypt + const hashedPassword = await bcrypt.hash(newPassword, 10); + + // Update the user's password in the database + await db.user.update({ + where: { id: userId }, + data: { + passwordHash: hashedPassword // Store the hashed password + } + }); + return { message: 'Password updated successfully!' }; + } + } + } catch (error) { + console.error('Error updating password:', error); + return fail(500, { error: 'Failed to update password' }); + } } }; diff --git a/src/routes/(auth)/Profile/+page.svelte b/src/routes/(auth)/Profile/+page.svelte index e84dc24..f16d50f 100755 --- a/src/routes/(auth)/Profile/+page.svelte +++ b/src/routes/(auth)/Profile/+page.svelte @@ -25,7 +25,7 @@ - + diff --git a/src/routes/(auth)/Profile/AccountSettingsList.svelte b/src/routes/(auth)/Profile/AccountSettingsList.svelte index c747ef6..9ecb914 100644 --- a/src/routes/(auth)/Profile/AccountSettingsList.svelte +++ b/src/routes/(auth)/Profile/AccountSettingsList.svelte @@ -1,4 +1,5 @@ @@ -31,16 +38,64 @@ As of now, the component displays a "Coming Soon" message, indicating that the a Account Settings -

Coming Soon

- + --> + +

More Coming Soon

diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index f2d8c44..a83f74d 100755 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,10 +1,12 @@ import { redirect } from '@sveltejs/kit'; import type { PageServerLoad, Actions, RequestEvent } from './$types'; - export const load: PageServerLoad = async (event) => { - if (event.url.href == 'https://svelte-mini-apps.netlify.app/') { - redirect(301, 'https://svelte-apps.me/'); - } +import { supabase } from '$lib/supabaseClient'; + +export const load: PageServerLoad = async (event) => { + if (event.url.href == 'https://svelte-mini-apps.netlify.app/') { + redirect(301, 'https://svelte-apps.me/'); + } }; // export const actions: Actions = {