From 4d258093f5f895085d0e076e1ea4bfeb7dd4ce12 Mon Sep 17 00:00:00 2001 From: Shannon Kao Date: Tue, 7 Feb 2023 12:40:21 -0800 Subject: [PATCH] Ensure single trailing slash in client-side URL construction --- client/src/components/AuthClient.ts | 18 ++++++++++-------- client/src/util/browser.ts | 4 ++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/client/src/components/AuthClient.ts b/client/src/components/AuthClient.ts index dc17754ef..19dac1e53 100644 --- a/client/src/components/AuthClient.ts +++ b/client/src/components/AuthClient.ts @@ -1,7 +1,9 @@ import { NetworkError, HTTPError } from "error-reporting"; +import { addTrailingSlash } from "../util/browser"; -const BASE_URL = process.env.REACT_APP_API_BASE_URL; -const AUTH_SERVER_BASE_URL = process.env.REACT_APP_AUTH_SERVER_BASE_URL; + +const BASE_URL = addTrailingSlash(process.env.REACT_APP_API_BASE_URL); +const AUTH_SERVER_BASE_URL = addTrailingSlash(process.env.REACT_APP_AUTH_SERVER_BASE_URL); let userEmail: string | undefined; const getUserEmail = () => userEmail; @@ -21,7 +23,7 @@ const setUserEmail = (email: string) => (userEmail = email); * Creates an account for this user if one does not already exist. */ const authenticate = async (username: string, password: string) => { - const json = await postAuthRequest(`${BASE_URL}/auth/authenticate`, { username, password }); + const json = await postAuthRequest(`${BASE_URL}auth/authenticate`, { username, password }); setUserEmail(username); return json; }; @@ -31,7 +33,7 @@ const authenticate = async (username: string, password: string) => { * and expiry time. */ const login = async (username: string, password: string) => { - const json = await postAuthRequest(`${BASE_URL}/auth/login`, { username, password }); + const json = await postAuthRequest(`${BASE_URL}auth/login`, { username, password }); return json; }; @@ -39,7 +41,7 @@ const login = async (username: string, password: string) => { * Revokes the current access token, if one is present */ const logout = async () => { - const json = await postAuthRequest(`${BASE_URL}/auth/logout`); + const json = await postAuthRequest(`${BASE_URL}auth/logout`); userEmail = undefined; return json; }; @@ -49,7 +51,7 @@ const logout = async () => { */ const userExists = async (username: string) => { try { - const response = await fetch(`${AUTH_SERVER_BASE_URL}/user/?email=${username}`); + const response = await fetch(`${AUTH_SERVER_BASE_URL}user/?email=${username}`); return !!response.ok; } catch (e) { console.log(e); @@ -60,14 +62,14 @@ const userExists = async (username: string) => { * Checks to see if a user is currently authenticated (via httponly cookie) */ const userAuthenticated = async () => { - return await postAuthRequest(`${BASE_URL}/auth/auth_check`); + return await postAuthRequest(`${BASE_URL}auth/auth_check`); }; /** * Sends an authenticated request to update the user email */ const updateEmail = async (newEmail: string) => { - return await postAuthRequest(`${BASE_URL}/user/update`, { new_email: newEmail }); + return await postAuthRequest(`${BASE_URL}user/update`, { new_email: newEmail }); }; /** diff --git a/client/src/util/browser.ts b/client/src/util/browser.ts index 0de6f8c77..8d087a4a1 100644 --- a/client/src/util/browser.ts +++ b/client/src/util/browser.ts @@ -67,6 +67,10 @@ export default { return decodeURIComponent(results[2].replace(/\+/g, " ")); }, + addTrailingSlash(url: string) { + return url.endsWith("/") ? url : url + "/"; + }, + WOAU_COOKIE_NAME: "woau", setCookie(name: string, value: string, days: number = 30) {