diff --git a/backend/src/controllers/userControllers.ts b/backend/src/controllers/userControllers.ts
index 4208765..a050050 100644
--- a/backend/src/controllers/userControllers.ts
+++ b/backend/src/controllers/userControllers.ts
@@ -55,7 +55,8 @@ const googleSignInOrSignUp = asyncHandler(
sameSite: "lax",
});
const username = user.username;
- res.status(200).json({ isCollegeEmail, username });
+ const userId = user.user_id;
+ res.status(200).json({ isCollegeEmail, username, userId });
}
);
@@ -318,6 +319,7 @@ const getCurrentUserDetails = asyncHandler(
user_id: true,
email: true,
username: true,
+ pic: true,
userCourses: {
select: {
Course: {
diff --git a/frontend/src/components/AddUsername.jsx b/frontend/src/components/AddUsername.jsx
index b5f427f..414c8d4 100644
--- a/frontend/src/components/AddUsername.jsx
+++ b/frontend/src/components/AddUsername.jsx
@@ -71,7 +71,7 @@ const AddUsername = () => {
setLoading(false);
toast({
title: "Error",
- description: "Error adding username.",
+ description: err.response?.data.message || "An error occurred",
status: "error",
duration: 3000,
isClosable: true,
diff --git a/frontend/src/components/EditDetails.jsx b/frontend/src/components/EditDetails.jsx
index 76bba6c..c44be20 100644
--- a/frontend/src/components/EditDetails.jsx
+++ b/frontend/src/components/EditDetails.jsx
@@ -1,4 +1,4 @@
-import React, { useState } from "react";
+import React, { useEffect, useState } from "react";
import {
Box,
FormControl,
@@ -8,6 +8,8 @@ import {
VStack,
useToast,
Flex,
+ Avatar,
+ Text,
} from "@chakra-ui/react";
import { z } from "zod";
import axios from "axios";
@@ -41,24 +43,31 @@ const EditDetails = () => {
setFormData({ ...formData, pic: e.target.files[0] });
};
- if (loadingUser) {
- return (
-
-
-
- );
- }
- if (!userDetails) {
- toast({
- title: "Error",
- description: "You need to be logged in to access this page.",
- status: "error",
- duration: 3000,
- isClosable: true,
- });
- navigate("/login");
- return;
- }
+ useEffect(() => {
+ if (loadingUser) return;
+
+ if (!userDetails) {
+ toast({
+ title: "Error",
+ description: "You need to be logged in to access this page.",
+ status: "error",
+ duration: 3000,
+ isClosable: true,
+ });
+ navigate("/login");
+ } else if (userDetails.username === null) {
+ navigate(`/addusername/${userDetails.user_id}`);
+ }
+ }, [userDetails, loadingUser, navigate, toast]);
+
+ useEffect(() => {
+ if (userDetails) {
+ setFormData({
+ username: userDetails.username || "",
+ pic: "",
+ });
+ }
+ }, [userDetails]);
const handleSubmit = async (e) => {
e.preventDefault();
@@ -92,13 +101,6 @@ const EditDetails = () => {
pic: picUrl,
};
- const user = await axios.get("/api/user/me", {
- withCredentials: true,
- });
- if (!updatedFormData.username) {
- updatedFormData.username = user.data.username;
- }
-
EditDetailsSchema.parse(updatedFormData);
setError({});
@@ -130,8 +132,20 @@ const EditDetails = () => {
}
};
+ if (loadingUser) {
+ return (
+
+
+
+ );
+ }
+
return (
+
+
+ {userDetails?.username || "Your Username"}
+