source: app/(app)/profile/actions.ts@ 509bd19

nextjs
Last change on this file since 509bd19 was 509bd19, checked in by Vasilaki Tocili <vasilakigorgi@…>, 4 months ago

refactor: UI cohesiveness/consistency

  • Implemented Profile page with forms for changing the account name,

email, password and a logout button that invalidates the session and
logs the user out

  • Removed the scrollbar for the larger screens and accounted for different

browser types, both for the outer page's and inside the "phone shell"
scrollbar

  • Made the bottom nav be fixed to the bottom across all the device screens

cohesively and consistently

  • Made the content inside the "phone shell" scrollable but the page is

not scrollable

  • Made the phone outer background same as the "phone shell"'s one, so the

larger phones such as my iPhone show single colour and feel like a phone
app, but still the larger screens such as laptop's and PC's remain black
to feel like a "phone shell"

  • Fixed all the non-profile pages to have the same determinate look

without flex competing with the UI and also to don't have those pages
return main but rather divs which are part of the parent main

  • Property mode set to 100644
File size: 1.4 KB
Line 
1'use server';
2
3import { auth } from '@/auth';
4import { redirect } from 'next/navigation';
5import postgres from 'postgres';
6import bcrypt from 'bcrypt';
7
8const sql = postgres(process.env.POSTGRES_URL!, { ssl: 'require' });
9
10export async function updateProfile(formData: FormData) {
11 const session = await auth();
12 if (!session?.user?.id) redirect('/login');
13
14 const name = formData.get('name') as string;
15 const email = formData.get('email') as string;
16
17 await sql`
18 UPDATE "user"
19 SET user_name = ${name},
20 email = ${email}
21 WHERE user_id = ${session.user.id}
22 `;
23
24 redirect('/profile');
25}
26
27export async function updatePassword(formData: FormData) {
28 const session = await auth();
29 if (!session?.user?.id) redirect('/login');
30
31 const currentPassword = formData.get('currentPassword') as string;
32 const newPassword = formData.get('newPassword') as string;
33
34 const users = await sql`
35 SELECT password
36 FROM "user"
37 WHERE user_id = ${session.user.id}
38 `;
39
40 const user = users[0];
41 if (!user) redirect('/login');
42
43 const match = await bcrypt.compare(currentPassword, user.password);
44 if (!match) {
45 throw new Error('Current password is incorrect');
46 }
47
48 const hashed = await bcrypt.hash(newPassword, 10);
49
50 await sql`
51 UPDATE "user"
52 SET password = ${hashed}
53 WHERE user_id = ${session.user.id}
54 `;
55
56 redirect('/profile');
57}
Note: See TracBrowser for help on using the repository browser.