Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"jwt-decode": "^3.1.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^5.4.0",
"react-router-dom": "^6.17.0",
"reactstrap": "^9.2.0",
"web-vitals": "^2.1.4"
Expand Down
58 changes: 42 additions & 16 deletions frontend/src/services/auth.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
import axios from "axios";

//Error Handeling function (centralised)
//While i was running the project, there was a 404 status form the backend due to which the the frontend was not running so i added a handleError functio which will handle the errors smoothly.
function handleError(error, functionName) {
if (error.response) {
// Server responded with a status other than 2xx
console.log(`[${functionName}] Error: ${error.response.status} - ${error.response.data}`);
} else if (error.request) {
// No response received from server
console.log(`[${functionName}] No response received:`, error.request);
} else {
// Error setting up the request
console.log(`[${functionName}] Request setup error:`, error.message);
}
}


export async function fetchCredentials() {
const response = await axios.get(
`${process.env.REACT_APP_BACKEND_URL}/auth/fetchAuth`,
{
withCredentials: true,
},
);
return response.data;
try{
const response = await axios.get(
`${process.env.REACT_APP_BACKEND_URL}/auth/fetchAuth`,
{
withCredentials: true,
},
);
return response.data;
}catch(err){
handleError(err, "fetchCredentials");
}

}

export async function registerUser(name, ID, email, password) {
Expand Down Expand Up @@ -38,7 +59,7 @@ export async function registerUser(name, ID, email, password) {
return null;
}
} catch (error) {
return null;
handleError(error, "registerUser");
}
}

Expand Down Expand Up @@ -68,7 +89,7 @@ export async function loginUser(email, password) {
return null;
}
} catch (error) {
return null;
handleError(error, "loginUser");
}
}

Expand Down Expand Up @@ -99,15 +120,20 @@ export async function registerStudentId(id, ID_No) {
}
} catch (error) {
console.error("Error registering student ID:", error);
return null;
handleError(error, "registerStudentID");
}
}

export async function logoutUser() {
await axios.post(
`${process.env.REACT_APP_BACKEND_URL}/auth/logout`,
{},
{ withCredentials: true },
);
return;
try{
await axios.post(
`${process.env.REACT_APP_BACKEND_URL}/auth/logout`,
{},
{ withCredentials: true },
);
return;
}catch(error){
handleError(error , "logoutUser")
}

}