Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ry 86 switch over auth #96

Merged
merged 8 commits into from
Feb 25, 2025
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
5 changes: 5 additions & 0 deletions apps/backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ export class AuthService {
const res = await axios
.post(tokenExchangeEndpoint, urlEncodedBody)
.catch((err) => {
console.error(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good change!

'Cognito Token Fetch Error:',
err.response?.data || err.message,
);
console.error('Full Error Details:', err.toJSON ? err.toJSON() : err);
throw new Error(`Error while fetching tokens from cognito: ${err}`);
});
const tokens = res.data as TokenExchangeResponseDTO;
Expand Down
6 changes: 4 additions & 2 deletions apps/backend/src/file-upload/entities/file-upload.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';
import { Application } from '../../applications/application.entity';
import { Application } from '../../applications/application.entity';

@Entity()
export class FileUpload {
Expand All @@ -18,6 +18,8 @@ export class FileUpload {
@Column({ type: 'bytea' }) // For PostgreSQL binary data
file_data: Buffer;

@ManyToOne(() => Application, (application) => application.attachments, { onDelete: 'CASCADE' })
@ManyToOne(() => Application, (application) => application.attachments, {
onDelete: 'CASCADE',
})
application: Application;
}
31 changes: 25 additions & 6 deletions apps/frontend/src/components/LoginPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,41 @@ import apiClient from '@api/apiClient';
import useLoginContext from './useLoginContext';
import { useNavigate } from 'react-router-dom';
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,
});

/**
* Login Page component first checks if the user has been redirected from the
* Cognito login page with an authorization code. If the code is present, it
* fetches the user's access token and stores it in the context.
*/
export default function LoginPage() {
const { setToken } = useLoginContext();
const navigate = useNavigate();

useEffect(() => {
const urlParams = new URLSearchParams(window.location.search);
const authCode = urlParams.get('code');

async function getToken() {
if (authCode) {
const sessionToken = sessionStorage.getItem('token');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the token expires, will this cause a 404 loop? since the token in sessionToken doesnt get reverified?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'll do some research. How would I test this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace the token in local storage with something invalid and reload the page

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I mess with the token in storage it gives a 401 error. Should I be regenerating tokens? I was also wondering if you knew how token expiration was handled previously or if it mattered at all before.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to explicitly regenerate tokens because the component itself will just boot you to the login page, you just need to make sure you don't navigate to the '/' endpoint if the token is invalid. The way it was handled previously is if it failed it would display the login button where its hard coded to redirect to the google oauth page. Line 42 in this file.


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

sessionStorage.setItem('token', JSON.stringify(token));
setToken(token);
navigate('/');
} catch (error) {
Expand All @@ -29,6 +47,7 @@ export default function LoginPage() {
}
getToken();
}, [navigate, setToken]);

return (
<Stack
width="100vw"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/mongodb": "^4.0.7",
"@types/multer": "^1.4.12",
"amazon-cognito-identity-js": "^6.3.5",
"aws-jwt-verify": "^5.0.0",
"axios": "^1.5.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
Expand Down
Loading
Loading