fdhsajkfdsa

This commit is contained in:
afazio1
2024-04-22 14:07:15 -04:00
parent 70fb79ac14
commit 50ad513ead
10 changed files with 48 additions and 28 deletions

View File

@@ -13,7 +13,7 @@ export default function App() {
useEffect( useEffect(
() => { () => {
setSession(getSessionCookie()); setSession(getSessionCookie());
if (session === undefined) { if (session === null) {
navigate("/login"); navigate("/login");
} }
}, },

View File

@@ -1,16 +1,19 @@
import React from "react"; import React from "react";
type SessionContextType = string | undefined; type SessionContextType = string | null;
export const getSessionCookie = () => { export const getSessionCookie = () => {
const sessionCookie = document.cookie // const sessionCookie = document.cookie
.split('; ') // .split('; ')
.find(row => row.startsWith('token=')) // .find(row => row.startsWith('token='))
?.split('=')[1]; // ?.split('=')[1];
const sessionCookie = localStorage.getItem('authToken');
return sessionCookie; return sessionCookie;
} }
export const destroySessionCookie = () => { 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<SessionContextType>(getSessionCookie()); export const SessionContext = React.createContext<SessionContextType>(getSessionCookie());

View File

@@ -7,7 +7,7 @@ export const useSessionRedirect = () => {
const navigate = useNavigate(); const navigate = useNavigate();
// Redirect to home if session is defined // Redirect to home if session is defined
useEffect(() => { useEffect(() => {
if (session !== undefined) { if (session !== null) {
navigate('/search'); navigate('/search');
} }
}, [session, navigate]) }, [session, navigate])

View File

@@ -20,7 +20,7 @@ export const usePoll = () => {
.then(response => response.text()) .then(response => response.text())
.then((data) => { .then((data) => {
// send private ip to central server // 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', method: 'POST',
headers: getAuthHeaders(), headers: getAuthHeaders(),
body: JSON.stringify({ip: data}), body: JSON.stringify({ip: data}),

View File

@@ -13,7 +13,7 @@ export default function Login() {
const formData = new FormData(e.target as HTMLFormElement); const formData = new FormData(e.target as HTMLFormElement);
const username = formData.get('username'); const username = formData.get('username');
const password = formData.get('password'); const password = formData.get('password');
fetch('http://localhost:8000/api/login/', { fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/login/`, {
method: 'POST', method: 'POST',
credentials: 'include', credentials: 'include',
headers: { headers: {
@@ -25,7 +25,7 @@ export default function Login() {
.then(data => { .then(data => {
console.log("Login response data:", data); console.log("Login response data:", data);
if (data.token) { if (data.token) {
// localStorage.setItem('authToken', data.token); localStorage.setItem('authToken', data.token);
navigate('/search'); navigate('/search');
} else { } else {
console.error('No token received:', data); console.error('No token received:', data);

View File

@@ -39,13 +39,17 @@ const MainSearch: React.FC = () => {
const fetchProfessors = async () => { const fetchProfessors = async () => {
const token = getSessionCookie();
try { try {
const response = await fetch('http://localhost:8000/api/professors', { const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/professors/`, {
method: 'GET', method: 'GET',
headers: getAuthHeaders(), headers: {
'Authorization': `Token ${token}`,
},
}); });
const data = await response.json(); const data = await response.json();
console.log("HEREEEEEE", data);
console.log(response)
if (Array.isArray(data)) { if (Array.isArray(data)) {
setProfessors(data); setProfessors(data);
} else { } else {
@@ -58,7 +62,7 @@ const MainSearch: React.FC = () => {
const fetchCourses = async () => { const fetchCourses = async () => {
try { try {
const response = await fetch('http://localhost:8000/api/courses', { const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/courses`, {
method: 'GET', method: 'GET',
headers: getAuthHeaders(), headers: getAuthHeaders(),
}); });
@@ -75,7 +79,7 @@ const MainSearch: React.FC = () => {
const fetchTopics = async () => { const fetchTopics = async () => {
try { try {
const response = await fetch('http://localhost:8000/api/topics', { const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/topics`, {
method: 'GET', method: 'GET',
headers: getAuthHeaders(), headers: getAuthHeaders(),
}); });
@@ -91,7 +95,7 @@ const MainSearch: React.FC = () => {
}; };
const fetchSemesters = async () => { const fetchSemesters = async () => {
try { try {
const response = await fetch('http://localhost:8000/api/semesters', { const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/semesters`, {
method: 'GET', method: 'GET',
headers: getAuthHeaders(), headers: getAuthHeaders(),
}); });

View File

@@ -35,7 +35,7 @@ export default function RegisterFile() {
}, [serverFiles]) }, [serverFiles])
const fetchServerFiles = () => { const fetchServerFiles = () => {
fetch(`http://localhost:8000/api/get-peer-files/`, { fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/get-peer-files/`, {
method: 'GET', method: 'GET',
headers: getAuthHeaders() headers: getAuthHeaders()
}) })
@@ -55,7 +55,7 @@ export default function RegisterFile() {
const formData = new FormData(event.target as HTMLFormElement); const formData = new FormData(event.target as HTMLFormElement);
// register file with central server // register file with central server
fetch(`http://localhost:8000/api/register/`, { fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/register/`, {
method: 'POST', method: 'POST',
headers: getAuthHeaders(), headers: getAuthHeaders(),
body: JSON.stringify({ body: JSON.stringify({
@@ -116,7 +116,7 @@ export default function RegisterFile() {
const fetchProfessors = async () => { const fetchProfessors = async () => {
try { try {
const response = await fetch('http://localhost:8000/api/professors', { const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/professors`, {
method: 'GET', method: 'GET',
headers: getAuthHeaders(), headers: getAuthHeaders(),
}); });
@@ -134,7 +134,7 @@ export default function RegisterFile() {
const fetchCourses = async () => { const fetchCourses = async () => {
try { try {
const response = await fetch('http://localhost:8000/api/courses', { const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/courses`, {
method: 'GET', method: 'GET',
headers: getAuthHeaders(), headers: getAuthHeaders(),
}); });
@@ -151,7 +151,7 @@ export default function RegisterFile() {
const fetchTopics = async () => { const fetchTopics = async () => {
try { try {
const response = await fetch('http://localhost:8000/api/topics', { const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/topics`, {
method: 'GET', method: 'GET',
headers: getAuthHeaders(), headers: getAuthHeaders(),
}); });
@@ -167,7 +167,7 @@ export default function RegisterFile() {
}; };
const fetchSemesters = async () => { const fetchSemesters = async () => {
try { try {
const response = await fetch('http://localhost:8000/api/semesters', { const response = await fetch(`${import.meta.env.VITE_CENTRAL_SERVER}/api/semesters`, {
method: 'GET', method: 'GET',
headers: getAuthHeaders(), headers: getAuthHeaders(),
}); });

View File

@@ -18,7 +18,7 @@ const Results: React.FC = () => {
// Need to double check this // Need to double check this
const queryString = location.search; // Includes the '?' prefix const queryString = location.search; // Includes the '?' prefix
console.log(queryString) 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) { if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`); 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 }), body: JSON.stringify({ id: file.id, filename: file.filename, ip: file.original_author.ip_address }),
}) })
.then((response) => response.text()) .then((response) => response.text())
.then((data) => { .then(() => {
console.log("Success download response data:", data); 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); setSuccess(true);
}) })
.catch((error) => { .catch((error) => {

View File

@@ -12,7 +12,8 @@ export default function Signup() {
const email = formData.get('email'); const email = formData.get('email');
const username = formData.get('username'); const username = formData.get('username');
const password = formData.get('password'); 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', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',

View File

@@ -1,6 +1,7 @@
import { getSessionCookie } from "../contexts/session"; import { getSessionCookie } from "../contexts/session";
export const getAuthHeaders = () => { export const getAuthHeaders = () => {
const token = getSessionCookie(); const token = getSessionCookie();
console.log(token);
return { return {
'Authorization': `Token ${token}`, 'Authorization': `Token ${token}`,
'Content-Type': 'application/json' 'Content-Type': 'application/json'