Merge pull request #28 from AmanTahiliani/main

update
This commit is contained in:
Sahej Panag
2024-04-22 14:26:35 -04:00
committed by GitHub
10 changed files with 48 additions and 28 deletions

View File

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

View File

@@ -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<SessionContextType>(getSessionCookie());

View File

@@ -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])

View File

@@ -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}),

View File

@@ -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);

View File

@@ -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(),
});

View File

@@ -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(),
});

View File

@@ -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}`);
}
@@ -105,8 +105,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) => {

View File

@@ -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',

View File

@@ -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'