Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion amplify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ applications:
- '**/*'
cache:
paths:
- node_modules/**/*
- node_modules/**/*
redirects: # ← add this whole block
- source: "/<*>" # match any client‑side route
target: "/index.html" # serve SPA shell
status: "200" # rewrite, not redirect
condition: null
1 change: 1 addition & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// API INDEX

const BASE = (import.meta.env.VITE_API_URL || '').replace(/\/$/, '');

export async function api(
path: string,
init?: RequestInit
init: RequestInit = {}
): Promise<Response> {
// Ensure path starts with a single slash
const cleanPath = path.startsWith('/') ? path : `/${path}`;
const url = `${BASE}${cleanPath}`;
return fetch(url, init);

return fetch(url, {
credentials: 'include', // ← send & receive the jwt cookie
...init,
});
}
29 changes: 19 additions & 10 deletions frontend/src/context/auth/authContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, createContext, ReactNode } from 'react';
import { useContext, createContext, ReactNode, useEffect } from 'react';
import { getAppStore } from '../../external/bcanSatchel/store';
import { setAuthState, logoutUser } from '../../external/bcanSatchel/actions'
import { observer } from 'mobx-react-lite';
Expand Down Expand Up @@ -42,13 +42,12 @@ export const AuthProvider = observer(({ children }: { children: ReactNode }) =>

const data = await response.json();

// TODO: Need to either completely remove access_token
// or verify it in each action
if (data.access_token) {
setAuthState(true, data.user, data.access_token);
} else {
if (data.user) {
// cookie was set by /auth/login
setAuthState(true, data.user, null);
} else {
alert('Login failed. Please check your credentials.');
}
}
};

/**
Expand All @@ -74,9 +73,19 @@ export const AuthProvider = observer(({ children }: { children: ReactNode }) =>
/**
* Log out the user
*/
const logout = () => {
logoutUser(); // Satchel action that clears state
};
const logout = () => {
api('/auth/logout', { method: 'POST' });
logoutUser();
};

// Session Level 1.1
// Restore on page-load / hard-refresh
useEffect(() => {
api('/auth/session')
.then(r => (r.ok ? r.json() : Promise.reject()))
.then(({ user }) => setAuthState(true, user, null))
.catch(() => logoutUser());
}, []);

return (
<AuthContext.Provider
Expand Down