Skip to content

Commit

Permalink
Ensure single trailing slash in client-side URL construction
Browse files Browse the repository at this point in the history
  • Loading branch information
shakao committed Feb 7, 2023
1 parent c9c1008 commit a319776
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 9 additions & 8 deletions client/src/components/AuthClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NetworkError, HTTPError } from "error-reporting";
import browser 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 = browser.addTrailingSlash(process.env.REACT_APP_API_BASE_URL);
const AUTH_SERVER_BASE_URL = browser.addTrailingSlash(process.env.REACT_APP_AUTH_SERVER_BASE_URL);

let userEmail: string | undefined;
const getUserEmail = () => userEmail;
Expand All @@ -21,7 +22,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;
};
Expand All @@ -31,15 +32,15 @@ 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;
};

/**
* 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;
};
Expand All @@ -49,7 +50,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);
Expand All @@ -60,14 +61,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 });
};

/**
Expand Down
4 changes: 4 additions & 0 deletions client/src/util/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a319776

Please sign in to comment.