Skip to content
Closed
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
43 changes: 36 additions & 7 deletions apps/frontend/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,55 @@
import ApplicationDetailPage from '@features/applications/pages/ApplicationDetailPage';
import LoginContext from '@features/auth/components/LoginPage/LoginContext';
import ProtectedRoutes from '@features/auth/components/ProtectedRoutes';
import LoginPage from '@features/auth/components/LoginPage';
import AdminRoutes from '@features/auth/components/AdminRoutes';
import HomePage from '@shared/pages/HomePage';

export const App: React.FC = () => {
const [token, setToken] = useState<string>('');
const [token, setToken] = useState<string>(() => {
const storedToken = localStorage.getItem('token');
return storedToken ? JSON.parse(storedToken) : '';
});

return (
<LoginContext.Provider value={{ setToken, token }}>
<BrowserRouter>
<Routes>
<Route path="/login" element={<LoginPage />} />
<Route path="/login" element={<HomePage />} />
<Route path="/home" element={<HomePage />} />

<Route element={<ProtectedRoutes token={token} />}>
<Route element={<ProtectedRoutes />}>
<Route element={<AdminRoutes />}>
<Route path="/" element={<ApplicationsPage />} />
<Route path="/applications" element={<ApplicationsPage />} />
<Route
path="/"
element={
<Navigation>

Check failure on line 31 in apps/frontend/src/app.tsx

View workflow job for this annotation

GitHub Actions / pre-deploy

'Navigation' is not defined
<DashboardPage />

Check failure on line 32 in apps/frontend/src/app.tsx

View workflow job for this annotation

GitHub Actions / pre-deploy

'DashboardPage' is not defined
</Navigation>
}
/>
<Route
path="/applications"
element={
<Navigation>

Check failure on line 39 in apps/frontend/src/app.tsx

View workflow job for this annotation

GitHub Actions / pre-deploy

'Navigation' is not defined
<ApplicationsPage />
</Navigation>
}
/>
<Route
path="/applications/:userId"
element={<ApplicationDetailPage />}
element={
<Navigation>

Check failure on line 47 in apps/frontend/src/app.tsx

View workflow job for this annotation

GitHub Actions / pre-deploy

'Navigation' is not defined
<ApplicationDetailPage />
</Navigation>
}
/>
<Route
path="/settings"
element={
<Navigation>

Check failure on line 55 in apps/frontend/src/app.tsx

View workflow job for this annotation

GitHub Actions / pre-deploy

'Navigation' is not defined
<SettingsPage />

Check failure on line 56 in apps/frontend/src/app.tsx

View workflow job for this annotation

GitHub Actions / pre-deploy

'SettingsPage' is not defined
</Navigation>
}
/>
</Route>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import { useFullName } from '@shared/hooks/useUserData';
import FileUploadBox from '../FileUploadBox';
import {
Application,

Check warning on line 19 in apps/frontend/src/features/applicant/components/ApplicantView/user.tsx

View workflow job for this annotation

GitHub Actions / pre-deploy

'Application' is defined but never used
ApplicationStage,

Check warning on line 20 in apps/frontend/src/features/applicant/components/ApplicantView/user.tsx

View workflow job for this annotation

GitHub Actions / pre-deploy

'ApplicationStage' is defined but never used
} from '@sharedTypes/types/application.types';
import { FilePurpose } from '@sharedTypes/types/file-upload.types';

Expand Down Expand Up @@ -125,8 +125,7 @@
</Box>
{!isLoading &&
selectedApplication &&
String(selectedApplication.stage) ===
ApplicationStage.PM_CHALLENGE && (
String(selectedApplication.stage) === 'PM_CHALLENGE' && (
<FileUploadBox
accessToken={accessToken}
applicationId={applicationId}
Expand Down
81 changes: 38 additions & 43 deletions apps/frontend/src/features/auth/components/LoginPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ import { useEffect } from 'react';
import apiClient from '@api/apiClient';
import useLoginContext from './useLoginContext';
import { useNavigate } from 'react-router-dom';
import { Box, CircularProgress, Typography } from '@mui/material';
import { Button, Stack } from '@mui/material';
import { CognitoJwtVerifier } from 'aws-jwt-verify';

const verifier = CognitoJwtVerifier.create({
userPoolId: import.meta.env.VITE_COGNITO_USER_POOL_ID as string,
tokenUse: 'access',
clientId: import.meta.env.VITE_COGNITO_CLIENT_ID as string,
});

/**
* LoginPage - Handles Cognito OAuth callback only
*
* This page is responsible for:
* 1. Receiving the auth code from Cognito redirect
* 2. Exchanging it for access/refresh tokens
* 3. Storing tokens and redirecting to dashboard
*
* This is NOT a landing page - users should not visit this directly.
* They arrive here only after authenticating with Cognito.
*/
export default function LoginPage() {
const { setToken } = useLoginContext();
const navigate = useNavigate();
Expand All @@ -23,12 +19,23 @@ export default function LoginPage() {
const urlParams = new URLSearchParams(window.location.search);
const authCode = urlParams.get('code');

async function handleCognitoCallback() {
if (authCode) {
async function getToken() {
const localToken = localStorage.getItem('token');

if (localToken) {
try {
const token = JSON.parse(localToken);
await verifier.verify(token);
setToken(token);
navigate('/');
} catch (error) {
console.log('Error verifying token:', error);
localStorage.removeItem('token');
}
} else if (authCode) {
try {
const tokenResponse = await apiClient.getToken(authCode);

// Store both tokens in localStorage for persistence
localStorage.setItem(
'auth_tokens',
JSON.stringify({
Expand All @@ -37,47 +44,35 @@ export default function LoginPage() {
}),
);

// Keep backward compatibility - store access token for existing code
sessionStorage.setItem(
localStorage.setItem(
'token',
JSON.stringify(tokenResponse.access_token),
);

setToken(tokenResponse.access_token);

// Redirect to dashboard after successful login
navigate('/');
window.location.href = '/';
} catch (error) {
console.error('Error fetching token:', error);
// Redirect to home page on error
navigate('/home');
}
} else {
// No auth code - redirect to home page
navigate('/home');
}
}

handleCognitoCallback();
getToken();
}, [navigate, setToken]);

return (
<Box
sx={{
width: '100vw',
height: '100vh',
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
gap: 3,
backgroundColor: '#181818',
}}
<Stack
width="100vw"
height="100vh"
justifyContent="center"
alignItems="center"
>
<CircularProgress size={60} sx={{ color: '#4a5fa8' }} />
<Typography variant="h6" sx={{ color: '#ffffff' }}>
Logging you in...
</Typography>
</Box>
<Button
variant="contained"
color="primary"
href="https://scaffolding.auth.us-east-2.amazoncognito.com/login?client_id=4c5b8m6tno9fvljmseqgmk82fv&response_type=code&scope=email+openid&redirect_uri=http%3A%2F%2Flocalhost%3A4200%2Flogin"
>
Login
</Button>
</Stack>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { Navigate, Outlet } from 'react-router-dom';
* if the user is authenticated (i.e if an access token exists).
* If the user is not authenticated, it redirects to the login page.
*/
function ProtectedRoutes({ token }: { token: string }) {
function ProtectedRoutes() {
const storedToken = localStorage.getItem('token');
const token = storedToken ? JSON.parse(storedToken) : '';

return token ? <Outlet /> : <Navigate to="/login" />;
}

Expand Down
Loading
Loading