same issue reoccus when tryinfg to change admin email and password, i ge
This commit is contained in:
@@ -40,16 +40,22 @@ export default function UserProfilePage() {
|
|||||||
register,
|
register,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
reset,
|
reset,
|
||||||
formState: { errors },
|
formState: { errors, isDirty },
|
||||||
} = useForm<UserProfileFormValues>({
|
} = useForm<UserProfileFormValues>({
|
||||||
resolver: zodResolver(userProfileSchema),
|
resolver: zodResolver(userProfileSchema),
|
||||||
|
defaultValues: {
|
||||||
|
name: "",
|
||||||
|
email: "",
|
||||||
|
password: "",
|
||||||
|
confirmPassword: ""
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchUser() {
|
async function fetchUser() {
|
||||||
const user = await getUser();
|
const user = await getUser();
|
||||||
if (user) {
|
if (user) {
|
||||||
reset({ name: user.name, email: user.email });
|
reset({ name: user.name, email: user.email, password: "", confirmPassword: "" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fetchUser();
|
fetchUser();
|
||||||
@@ -68,7 +74,7 @@ export default function UserProfilePage() {
|
|||||||
title: "Profile Updated",
|
title: "Profile Updated",
|
||||||
description: "Your profile has been updated successfully.",
|
description: "Your profile has been updated successfully.",
|
||||||
});
|
});
|
||||||
// Clear password fields after successful submission
|
// Clear password fields and reset dirty state after successful submission
|
||||||
reset({ ...data, password: '', confirmPassword: '' });
|
reset({ ...data, password: '', confirmPassword: '' });
|
||||||
} else {
|
} else {
|
||||||
toast({
|
toast({
|
||||||
@@ -112,7 +118,7 @@ export default function UserProfilePage() {
|
|||||||
</div>
|
</div>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
<CardFooter className="flex justify-end">
|
<CardFooter className="flex justify-end">
|
||||||
<Button type="submit" disabled={isSaving}>
|
<Button type="submit" disabled={isSaving || !isDirty}>
|
||||||
{isSaving ? 'Saving...' : 'Save Changes'}
|
{isSaving ? 'Saving...' : 'Save Changes'}
|
||||||
</Button>
|
</Button>
|
||||||
</CardFooter>
|
</CardFooter>
|
||||||
|
|||||||
@@ -32,18 +32,20 @@ export async function updateUser(
|
|||||||
): Promise<{ success: boolean; error?: string }> {
|
): Promise<{ success: boolean; error?: string }> {
|
||||||
const session = await getSession();
|
const session = await getSession();
|
||||||
if (!session?.userId) {
|
if (!session?.userId) {
|
||||||
return { success: false, error: 'Not authenticated' };
|
return { success: false, error: 'Not authenticated. Please log in again.' };
|
||||||
}
|
}
|
||||||
|
|
||||||
const validated = UserUpdateSchema.safeParse(data);
|
const validated = UserUpdateSchema.safeParse(data);
|
||||||
if (!validated.success) {
|
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;
|
const { name, email, password } = validated.data;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (password && password.length > 0) {
|
if (password && password.trim().length > 0) {
|
||||||
// In a real application, hash the password
|
// In a real application, hash the password
|
||||||
const stmt = db.prepare(
|
const stmt = db.prepare(
|
||||||
'UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?'
|
'UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?'
|
||||||
@@ -59,6 +61,6 @@ export async function updateUser(
|
|||||||
if (error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
|
if (error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
|
||||||
return { success: false, error: 'Email already in use.' };
|
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
@@ -31,6 +31,7 @@ export async function signIn(formData: FormData) {
|
|||||||
name: user.name,
|
name: user.name,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Set the session cookie
|
||||||
cookies().set('session', JSON.stringify(sessionData), {
|
cookies().set('session', JSON.stringify(sessionData), {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: process.env.NODE_ENV === 'production',
|
secure: process.env.NODE_ENV === 'production',
|
||||||
@@ -55,18 +56,23 @@ export async function signOut() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getSession() {
|
export async function getSession() {
|
||||||
const cookieStore = cookies();
|
const sessionCookie = cookies().get('session');
|
||||||
const sessionCookie = cookieStore.get('session');
|
|
||||||
|
|
||||||
if (!sessionCookie) {
|
if (!sessionCookie?.value) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const session = JSON.parse(sessionCookie.value);
|
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) {
|
} catch (error) {
|
||||||
console.error('Failed to parse session cookie:', error);
|
console.error('Failed to parse session cookie:', error);
|
||||||
|
// If parsing fails, the cookie is invalid. Clear it.
|
||||||
|
cookies().delete('session');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user