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

Updates #26

Merged
merged 4 commits into from
Nov 12, 2024
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
3 changes: 2 additions & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Module } from '@nestjs/common';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { GrantModule } from './grant/grant.module';
import { NotificationsModule } from './notifications/notifications.module';

@Module({
imports: [AuthModule, UserModule, GrantModule],
imports: [AuthModule, UserModule, GrantModule, NotificationsModule],
})
export class AppModule {}
38 changes: 29 additions & 9 deletions backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { Injectable, Logger } from '@nestjs/common';
import { Injectable, InternalServerErrorException, Logger, UnauthorizedException } from '@nestjs/common';
import AWS from 'aws-sdk';
import { table } from 'console';
import * as crypto from 'crypto';

AWS.config.update({
region: process.env.AWS_REGION,
});

@Injectable()
export class AuthService {
private readonly logger = new Logger(AuthService.name);
Expand Down Expand Up @@ -96,6 +92,7 @@ export class AuthService {
challenge?: string;
requiredAttributes?: string[];
username?: string;
message?: string;
}> {
const clientId = process.env.COGNITO_CLIENT_ID;
const clientSecret = process.env.COGNITO_CLIENT_SECRET;
Expand Down Expand Up @@ -206,13 +203,36 @@ export class AuthService {
user = newUser;
}

return { access_token: idToken, user };
return { access_token: idToken, user, message: "Login Successful!" };
} catch (error: unknown) {
if (error instanceof Error) {
/* Login Failures */
const cognitoError = error as AwsCognitoError;

if (cognitoError.code) {
switch (cognitoError.code) {
case 'NotAuthorizedException':
this.logger.warn(`Login failed: ${cognitoError.message}`);
throw new UnauthorizedException('Incorrect username or password.');
default:
this.logger.error(
`Login failed: ${cognitoError.message}`,
cognitoError.stack,
);
throw new InternalServerErrorException(
'An error occurred during login.',
);
}
} else if (error instanceof Error) {
// Handle non-AWS errors
this.logger.error('Login failed', error.stack);
throw new Error(error.message || 'Login failed');
throw new InternalServerErrorException(
error.message || 'Login failed.',
);
}
throw new Error('An unknown error occurred during login');
// Handle unknown errors
throw new InternalServerErrorException(
'An unknown error occurred during login.',
);
}
}

Expand Down
8 changes: 5 additions & 3 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import AWS from 'aws-sdk';

/* ! */
async function bootstrap() {
AWS.config.update({
region: process.env.AWS_REGION
});
AWS.config.update({
region: process.env.AWS_REGION,
accessKeyId: process.env.OPEN_HATCH,
secretAccessKey: process.env.CLOSED_HATCH
});
const app = await NestFactory.create(AppModule);
app.enableCors();
await app.listen(3001);
Expand Down
14 changes: 14 additions & 0 deletions backend/src/notifications/notifcations.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// src/notifications/notifications.service.ts
import { Injectable } from '@nestjs/common';
import * as AWS from 'aws-sdk';

// idk about name but maybe?
const TABLE_NAME = 'BCANNotifications';

@Injectable()
export class NotificationsService {

// function to make a notification

// function to find notifications by user id
}
17 changes: 17 additions & 0 deletions backend/src/notifications/notifications.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// src/notifications/notifications.controller.ts
import { Controller, Post, Body, Get, Query } from '@nestjs/common';

@Controller('notifications')
export class NotificationsController {
// constructor(private readonly notificationsService: NotificationsService) {}

@Post()
async create(@Body() notification: Partial<Notification>) {
// return this.notificationsService.create(notification);
}

@Get()
async findByUser(@Query('userId') userId: string) {
// return this.notificationsService.findByUser(userId);
}
}
11 changes: 11 additions & 0 deletions backend/src/notifications/notifications.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// src/notifications/notifications.module.ts
import { Module } from '@nestjs/common';
import { NotificationsController } from './notifications.controller';
import { NotificationsService } from './notifcations.service';

@Module({
providers: [NotificationsService],
controllers: [NotificationsController],
exports: [NotificationsService],
})
export class NotificationsModule {}
4 changes: 4 additions & 0 deletions backend/src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface AwsCognitoError extends Error {
code?: string;
[key: string]: any; // Allows for additional props
}
1 change: 1 addition & 0 deletions frontend/src/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const Login = observer(() => {
if (data.access_token) {
setAuthentication(true, data.user, data.access_token);
navigate('/dashboard');
alert(data.message); // Alert wit message from backend indicating success
} else {
alert('Login failed. Please check your credentials.');
}
Expand Down
Loading