diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 446f514..6151024 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -13,7 +13,7 @@ export default function App() { useEffect( () => { setSession(getSessionCookie()); - if (session === undefined) { + if (session === null) { navigate("/login"); } }, diff --git a/frontend/src/contexts/session.ts b/frontend/src/contexts/session.ts index ce77016..55a72c1 100644 --- a/frontend/src/contexts/session.ts +++ b/frontend/src/contexts/session.ts @@ -1,16 +1,19 @@ import React from "react"; -type SessionContextType = string | undefined; +type SessionContextType = string | null; export const getSessionCookie = () => { - const sessionCookie = document.cookie - .split('; ') - .find(row => row.startsWith('token=')) - ?.split('=')[1]; + // const sessionCookie = document.cookie + // .split('; ') + // .find(row => row.startsWith('token=')) + // ?.split('=')[1]; + const sessionCookie = localStorage.getItem('authToken'); return sessionCookie; } export const destroySessionCookie = () => { - document.cookie = "token=; Max-Age=0;" + console.log("destroying session cookie"); + localStorage.removeItem('authToken'); + // document.cookie = "token=; Max-Age=0;" } export const SessionContext = React.createContext(getSessionCookie()); diff --git a/frontend/src/hooks/sessionRedirect.ts b/frontend/src/hooks/sessionRedirect.ts index 80ddd7c..fe761c4 100644 --- a/frontend/src/hooks/sessionRedirect.ts +++ b/frontend/src/hooks/sessionRedirect.ts @@ -7,7 +7,7 @@ export const useSessionRedirect = () => { const navigate = useNavigate(); // Redirect to home if session is defined useEffect(() => { - if (session !== undefined) { + if (session !== null) { navigate('/search'); } }, [session, navigate]) diff --git a/frontend/src/hooks/usePoll.ts b/frontend/src/hooks/usePoll.ts index ff3bc1d..10284fc 100644 --- a/frontend/src/hooks/usePoll.ts +++ b/frontend/src/hooks/usePoll.ts @@ -20,7 +20,7 @@ export const usePoll = () => { .then(response => response.text()) .then((data) => { // send private ip to central server - return fetch('http://localhost:8000/api/poll/', { + return fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/poll/`, { method: 'POST', headers: getAuthHeaders(), body: JSON.stringify({ip: data}), diff --git a/frontend/src/screens/Login.tsx b/frontend/src/screens/Login.tsx index 54e5da6..32a45e4 100644 --- a/frontend/src/screens/Login.tsx +++ b/frontend/src/screens/Login.tsx @@ -13,7 +13,7 @@ export default function Login() { const formData = new FormData(e.target as HTMLFormElement); const username = formData.get('username'); const password = formData.get('password'); - fetch('http://localhost:8000/api/login/', { + fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/login/`, { method: 'POST', credentials: 'include', headers: { @@ -25,7 +25,7 @@ export default function Login() { .then(data => { console.log("Login response data:", data); if (data.token) { - // localStorage.setItem('authToken', data.token); + localStorage.setItem('authToken', data.token); navigate('/search'); } else { console.error('No token received:', data); diff --git a/frontend/src/screens/MainSearch.tsx b/frontend/src/screens/MainSearch.tsx index 52dab6c..52386c1 100644 --- a/frontend/src/screens/MainSearch.tsx +++ b/frontend/src/screens/MainSearch.tsx @@ -39,13 +39,17 @@ const MainSearch: React.FC = () => { const fetchProfessors = async () => { + const token = getSessionCookie(); try { - const response = await fetch('http://localhost:8000/api/professors', { + const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/professors/`, { method: 'GET', - headers: getAuthHeaders(), + headers: { + 'Authorization': `Token ${token}`, + }, }); const data = await response.json(); - + console.log("HEREEEEEE", data); + console.log(response) if (Array.isArray(data)) { setProfessors(data); } else { @@ -58,7 +62,7 @@ const MainSearch: React.FC = () => { const fetchCourses = async () => { try { - const response = await fetch('http://localhost:8000/api/courses', { + const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/courses`, { method: 'GET', headers: getAuthHeaders(), }); @@ -75,7 +79,7 @@ const MainSearch: React.FC = () => { const fetchTopics = async () => { try { - const response = await fetch('http://localhost:8000/api/topics', { + const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/topics`, { method: 'GET', headers: getAuthHeaders(), }); @@ -91,7 +95,7 @@ const MainSearch: React.FC = () => { }; const fetchSemesters = async () => { try { - const response = await fetch('http://localhost:8000/api/semesters', { + const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/semesters`, { method: 'GET', headers: getAuthHeaders(), }); diff --git a/frontend/src/screens/RegisterFile.tsx b/frontend/src/screens/RegisterFile.tsx index 5f94b23..fb8657d 100644 --- a/frontend/src/screens/RegisterFile.tsx +++ b/frontend/src/screens/RegisterFile.tsx @@ -35,7 +35,7 @@ export default function RegisterFile() { }, [serverFiles]) const fetchServerFiles = () => { - fetch(`http://localhost:8000/api/get-peer-files/`, { + fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/get-peer-files/`, { method: 'GET', headers: getAuthHeaders() }) @@ -55,7 +55,7 @@ export default function RegisterFile() { const formData = new FormData(event.target as HTMLFormElement); // register file with central server - fetch(`http://localhost:8000/api/register/`, { + fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/register/`, { method: 'POST', headers: getAuthHeaders(), body: JSON.stringify({ @@ -116,7 +116,7 @@ export default function RegisterFile() { const fetchProfessors = async () => { try { - const response = await fetch('http://localhost:8000/api/professors', { + const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/professors`, { method: 'GET', headers: getAuthHeaders(), }); @@ -134,7 +134,7 @@ export default function RegisterFile() { const fetchCourses = async () => { try { - const response = await fetch('http://localhost:8000/api/courses', { + const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/courses`, { method: 'GET', headers: getAuthHeaders(), }); @@ -151,7 +151,7 @@ export default function RegisterFile() { const fetchTopics = async () => { try { - const response = await fetch('http://localhost:8000/api/topics', { + const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/topics`, { method: 'GET', headers: getAuthHeaders(), }); @@ -167,7 +167,7 @@ export default function RegisterFile() { }; const fetchSemesters = async () => { try { - const response = await fetch('http://localhost:8000/api/semesters', { + const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/semesters`, { method: 'GET', headers: getAuthHeaders(), }); diff --git a/frontend/src/screens/Results.tsx b/frontend/src/screens/Results.tsx index 2bc6a72..5c630c9 100644 --- a/frontend/src/screens/Results.tsx +++ b/frontend/src/screens/Results.tsx @@ -18,7 +18,7 @@ const Results: React.FC = () => { // Need to double check this const queryString = location.search; // Includes the '?' prefix console.log(queryString) - const response = await fetch(`http://localhost:8000/api/files/filter${queryString}`, {headers: getAuthHeaders()}); + const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/files/filter${queryString}`, {headers: getAuthHeaders()}); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } @@ -101,8 +101,19 @@ function DownloadButton({ file }: { file: File }) { body: JSON.stringify({ id: file.id, filename: file.filename, ip: file.original_author.ip_address }), }) .then((response) => response.text()) - .then((data) => { - console.log("Success download response data:", data); + .then(() => { + return fetch( + `${import.meta.env.VITE_CENTRAL_SERVER}/api/files/${file.id}/add-peer/`, + { + method: "POST", + headers: getAuthHeaders(), + } + ) + }) + .then((response) => { + if (!response.ok) { + throw new Error("Failed to add peer"); + } setSuccess(true); }) .catch((error) => { diff --git a/frontend/src/screens/Signup.tsx b/frontend/src/screens/Signup.tsx index 9022619..a1f308c 100644 --- a/frontend/src/screens/Signup.tsx +++ b/frontend/src/screens/Signup.tsx @@ -12,7 +12,8 @@ export default function Signup() { const email = formData.get('email'); const username = formData.get('username'); const password = formData.get('password'); - fetch('http://localhost:8000/api/signup/', { + console.log(import.meta.env.VITE_CENTRAL_SERVER) + fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/signup/`, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/frontend/src/utils/getAuthHeaders.ts b/frontend/src/utils/getAuthHeaders.ts index 2c7870d..d9a138c 100644 --- a/frontend/src/utils/getAuthHeaders.ts +++ b/frontend/src/utils/getAuthHeaders.ts @@ -1,6 +1,7 @@ import { getSessionCookie } from "../contexts/session"; export const getAuthHeaders = () => { - const token = getSessionCookie(); + const token = getSessionCookie(); + console.log(token); return { 'Authorization': `Token ${token}`, 'Content-Type': 'application/json'