From 599182ab91e11e9e9a3c7ccc1cb6a09c29e5091f Mon Sep 17 00:00:00 2001 From: JBB0807 <104856796+JBB0807@users.noreply.github.com> Date: Thu, 1 May 2025 13:32:12 -0700 Subject: [PATCH 1/4] Logout calls back-end --- src/components/Navbar.jsx | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/components/Navbar.jsx b/src/components/Navbar.jsx index f507995..0a011b4 100644 --- a/src/components/Navbar.jsx +++ b/src/components/Navbar.jsx @@ -1,7 +1,7 @@ import React, { useState, useEffect, useRef } from "react"; import "../scss/styles.scss"; import "../scss/components/_navbar.scss"; -import { Link, useNavigate } from "react-router-dom"; +import { Link } from "react-router-dom"; const Navbar = () => { const [glitchEffect, setGlitchEffect] = useState(false); @@ -9,23 +9,9 @@ const Navbar = () => { const [user, setUser] = useState(null); const [menuOpen, setMenuOpen] = useState(false); const menuRef = useRef(null); - const navigate = useNavigate(); - const handleLogout = () => { - // Implement client-side logout without calling the backend - // This clears the user state in the frontend - setUser(null); - - // Clear any authentication cookies if they exist - document.cookie.split(";").forEach((cookie) => { - const [name] = cookie.trim().split("="); - document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`; - }); - - // Redirect to home page - navigate("/"); - - console.log("Logged out successfully"); + async function handleLogout() { + window.open("http://localhost:8080/auth/logout", "_self"); }; useEffect(() => { From 28dd929927d2c7091a4a6fb3b1478a35505b0546 Mon Sep 17 00:00:00 2001 From: JBB0807 <104856796+JBB0807@users.noreply.github.com> Date: Fri, 2 May 2025 15:07:05 -0700 Subject: [PATCH 2/4] working login for students and logout --- src/pages/SignIn.jsx | 42 ++++++++++++++++++++++++++++++------------ src/pages/SignUp.jsx | 4 ++-- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/pages/SignIn.jsx b/src/pages/SignIn.jsx index 9976eb5..a404843 100644 --- a/src/pages/SignIn.jsx +++ b/src/pages/SignIn.jsx @@ -3,7 +3,7 @@ import "@fortawesome/fontawesome-free/css/all.min.css"; function SignInForm() { const [state, setState] = React.useState({ - email: "", + assignmentID: "", password: "", }); const handleChange = (evt) => { @@ -17,15 +17,33 @@ function SignInForm() { const handleOnSubmit = (evt) => { evt.preventDefault(); - const { email, password } = state; - alert(`You are login with email: ${email} and password: ${password}`); + const { assignmentId, password } = state; + console.log(`You are loggind in with email: ${assignmentId} and password: ${password}`); - for (const key in state) { - setState({ - ...state, - [key]: "", + console.log("Submitting login request with state:", state); + fetch("http://localhost:8080/auth/student/login", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(state), + credentials: "include", + }) + .then((response) => { + console.log("Received response:", response); + if (!response.ok) { + throw new Error("Network response was not ok"); + } + return response.json(); + }) + .then((data) => { + console.log("Success:", data); + window.location.href = "/"; + }) + .catch((error) => { + console.error("Error occurred during login:", error); + alert("Login failed!"); }); - } }; return ( @@ -39,10 +57,10 @@ function SignInForm() { */} { evt.preventDefault(); - const { name, email, password } = state; + const { assignmentID, password } = state; alert( - `You are signed in with name: ${name} email: ${email} and password: ${password}` + `You are signed in with assignmentID: ${assignmentID} and password: ${password}` ); for (const key in state) { From 00c14f4eebf47e484e8ff693b044bf80714b2622 Mon Sep 17 00:00:00 2001 From: JBB0807 <104856796+JBB0807@users.noreply.github.com> Date: Fri, 2 May 2025 17:20:38 -0700 Subject: [PATCH 3/4] convert endpoint urls to env variables --- .env | 4 ++-- src/components/Navbar.jsx | 4 +++- src/pages/SignIn.jsx | 4 +++- src/pages/SignUp.jsx | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.env b/.env index 0d0092a..7dc9c2c 100644 --- a/.env +++ b/.env @@ -1,2 +1,2 @@ -VITE_AUTH_URL="http://localhost:8080/auth/google" -#VITE_AUTH_URL="https://byte-camp-auth-service.fly.dev/auth/google" \ No newline at end of file +VITE_AUTH_URL="http://localhost:8080" +#VITE_AUTH_URL="https://byte-camp-auth-service.fly.dev" \ No newline at end of file diff --git a/src/components/Navbar.jsx b/src/components/Navbar.jsx index 0a011b4..02b1e5e 100644 --- a/src/components/Navbar.jsx +++ b/src/components/Navbar.jsx @@ -3,6 +3,8 @@ import "../scss/styles.scss"; import "../scss/components/_navbar.scss"; import { Link } from "react-router-dom"; +const authUrl = import.meta.env.VITE_AUTH_URL; + const Navbar = () => { const [glitchEffect, setGlitchEffect] = useState(false); const [activeLink, setActiveLink] = useState("/"); @@ -37,7 +39,7 @@ const Navbar = () => { document.addEventListener("mousedown", handleClickOutside); async function fetchUser() { - const res = await fetch("http://localhost:8080/auth/current_user", { + const res = await fetch(`${authUrl}/auth/current_user`, { credentials: "include", // very important }); if (res.ok) { diff --git a/src/pages/SignIn.jsx b/src/pages/SignIn.jsx index a404843..ff2372e 100644 --- a/src/pages/SignIn.jsx +++ b/src/pages/SignIn.jsx @@ -1,6 +1,8 @@ import React from "react"; import "@fortawesome/fontawesome-free/css/all.min.css"; +const authUrl = import.meta.env.VITE_AUTH_URL; + function SignInForm() { const [state, setState] = React.useState({ assignmentID: "", @@ -21,7 +23,7 @@ function SignInForm() { console.log(`You are loggind in with email: ${assignmentId} and password: ${password}`); console.log("Submitting login request with state:", state); - fetch("http://localhost:8080/auth/student/login", { + fetch(`${authUrl}/auth/student/login`, { method: "POST", headers: { "Content-Type": "application/json", diff --git a/src/pages/SignUp.jsx b/src/pages/SignUp.jsx index cb9d445..9f6804b 100644 --- a/src/pages/SignUp.jsx +++ b/src/pages/SignUp.jsx @@ -34,7 +34,7 @@ function SignUpForm() { const googleAuth = () => { - window.open(authUrl, "_self"); + window.open(`${authUrl}/auth/google`, "_self"); }; return ( From 12f81acbf25b4338897c540ec6b6d98c5b5db6c8 Mon Sep 17 00:00:00 2001 From: JBB0807 <104856796+JBB0807@users.noreply.github.com> Date: Fri, 2 May 2025 18:34:20 -0700 Subject: [PATCH 4/4] bug fixes for fly server --- .env | 4 ++-- .env.development | 2 ++ src/components/Navbar.jsx | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 .env.development diff --git a/.env b/.env index 7dc9c2c..ddf3a43 100644 --- a/.env +++ b/.env @@ -1,2 +1,2 @@ -VITE_AUTH_URL="http://localhost:8080" -#VITE_AUTH_URL="https://byte-camp-auth-service.fly.dev" \ No newline at end of file +#VITE_AUTH_URL="http://localhost:8080" +VITE_AUTH_URL="https://byte-camp-auth-service.fly.dev" \ No newline at end of file diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..7dc9c2c --- /dev/null +++ b/.env.development @@ -0,0 +1,2 @@ +VITE_AUTH_URL="http://localhost:8080" +#VITE_AUTH_URL="https://byte-camp-auth-service.fly.dev" \ No newline at end of file diff --git a/src/components/Navbar.jsx b/src/components/Navbar.jsx index 02b1e5e..537cb27 100644 --- a/src/components/Navbar.jsx +++ b/src/components/Navbar.jsx @@ -13,7 +13,7 @@ const Navbar = () => { const menuRef = useRef(null); async function handleLogout() { - window.open("http://localhost:8080/auth/logout", "_self"); + window.open(`${authUrl}/auth/logout`, "_self"); }; useEffect(() => {