same issue reoccus when tryinfg to change admin email and password, i ge

This commit is contained in:
Leon Serfaty G
2025-07-17 11:30:50 +00:00
parent 89cfc08a17
commit 431023f6ed
3 changed files with 28 additions and 14 deletions
+6 -4
View File
@@ -32,18 +32,20 @@ export async function updateUser(
): Promise<{ success: boolean; error?: string }> {
const session = await getSession();
if (!session?.userId) {
return { success: false, error: 'Not authenticated' };
return { success: false, error: 'Not authenticated. Please log in again.' };
}
const validated = UserUpdateSchema.safeParse(data);
if (!validated.success) {
return { success: false, error: 'Invalid data' };
const errors = validated.error.flatten().fieldErrors;
const firstError = Object.values(errors)[0]?.[0] ?? 'Invalid data provided.';
return { success: false, error: firstError };
}
const { name, email, password } = validated.data;
try {
if (password && password.length > 0) {
if (password && password.trim().length > 0) {
// In a real application, hash the password
const stmt = db.prepare(
'UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?'
@@ -59,6 +61,6 @@ export async function updateUser(
if (error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
return { success: false, error: 'Email already in use.' };
}
return { success: false, error: 'Failed to update user profile.' };
return { success: false, error: 'Failed to update user profile due to a server error.' };
}
}
+12 -6
View File
@@ -31,6 +31,7 @@ export async function signIn(formData: FormData) {
name: user.name,
};
// Set the session cookie
cookies().set('session', JSON.stringify(sessionData), {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
@@ -55,18 +56,23 @@ export async function signOut() {
}
export async function getSession() {
const cookieStore = cookies();
const sessionCookie = cookieStore.get('session');
const sessionCookie = cookies().get('session');
if (!sessionCookie) {
if (!sessionCookie?.value) {
return null;
}
try {
const session = JSON.parse(sessionCookie.value);
return session;
// Basic validation to ensure the session object has expected properties
if (session && typeof session === 'object' && session.userId) {
return session as User & { isLoggedIn: boolean; userId: number };
}
return null;
} catch (error) {
console.error('Failed to parse session cookie:', error);
// If parsing fails, the cookie is invalid. Clear it.
cookies().delete('session');
return null;
}
}
}