Skip to content

Commit

Permalink
Apply proper headers across all fetch methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hburn7 committed Oct 29, 2023
1 parent 9c23b45 commit 391b4c1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
7 changes: 6 additions & 1 deletion src/components/LinkSubmissionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ function LinkSubmissionForm({
useState(false);

const apiLink = process.env.REACT_APP_API_URL;
const origin = process.env.REACT_APP_ORIGIN_URL;
const navigate = useNavigate();

useEffect(() => {
fetch(apiLink + "/me", {
method: "GET",
credentials: "include",
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": `${origin}`,
}
})
.then((response) => response.json())
.then((data) => {
Expand Down
10 changes: 8 additions & 2 deletions src/components/VerifiedTournaments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ function VerifiedTournaments({ hasVerifierRole }: IVerifiedTournamentProps) {
if(!hasVerifierRole) {
return;
}

fetch(process.env.REACT_APP_API_URL + "/tournaments/verified", {

const apiUrl = process.env.REACT_APP_API_URL;
const origin = process.env.REACT_APP_ORIGIN_URL;
fetch(apiUrl + "/tournaments/verified", {
method: "GET",
credentials: "include",
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": `${origin}`
}
})
.then((response) => response.json())
.then((data) => {
Expand Down
41 changes: 23 additions & 18 deletions src/components/pages/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ function Auth({ isAuthenticated, setIsAuthenticated, setAuthenticatedUser }: IAu
const code = params.get("code");
const navigate = useNavigate();

const apiLink = process.env.REACT_APP_API_URL;
const apiUrl = process.env.REACT_APP_API_URL;
const origin = process.env.REACT_APP_ORIGIN_URL;

useEffect(() => {
console.log("Logging in after osu! redirect");

// make api call to login with code
fetch(apiLink + '/login?code=' + code, {
fetch(apiUrl + '/login?code=' + code, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": `${origin}`,
},
credentials: "include",
})
.then((response) => {
if (response.status !== 200) {
throw new Error("Authorization failed!");
throw new Error("Authorization failed!");
}

setIsAuthenticated(true);
navigate("/", { replace: true });
})
Expand All @@ -40,19 +40,24 @@ function Auth({ isAuthenticated, setIsAuthenticated, setAuthenticatedUser }: IAu
);
});

fetch(apiLink + "/me", {
method: "GET",
credentials: "include",
})
.then((response) => response.json())
.then((data) => {
setAuthenticatedUser(data);
})
.catch((error) => {
console.error("Error fetching authenticated user:", error);
});

}, [apiLink, code, navigate, setIsAuthenticated, setAuthenticatedUser]);
const origin = process.env.REACT_APP_ORIGIN_URL;
fetch(apiUrl + "/me", {
method: "GET",
credentials: "include",
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": `${origin}`,
}
})
.then((response) => response.json())
.then((data) => {
setAuthenticatedUser(data);
})
.catch((error) => {
console.error("Error fetching authenticated user:", error);
});

}, [apiUrl, code, navigate, setIsAuthenticated, setAuthenticatedUser]);

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion src/components/pages/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ function Dashboard({ isAuthenticated, mode }: { isAuthenticated: boolean, mode:
const origin = process.env.REACT_APP_ORIGIN_URL;
fetch(apiLink + "/me/stats?dateMin=" + formattedDate + "&mode=" + mode, {
method: "GET",
credentials: "include",
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": `${origin}`,
},
credentials: "include",
})
.then((response) => response.json())
.then((data) => {
Expand Down

0 comments on commit 391b4c1

Please sign in to comment.