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

feat: [#7] - Add. static authentication for SignIn api #8

Merged
merged 2 commits into from
Dec 11, 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
12 changes: 7 additions & 5 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
fileignoreconfig:
- filename: frontend/src/app/auth/auth.tsx
checksum: 4ffb9648d31740f2a6fff7fa3368d13d2bfaea047f2eb0f2bc342984ca1d633f
- filename: frontend/src/app/signup/page.tsx
checksum: 98adba90b3c2e6e6b2c9d43996a844467b8afb4881e606f9b22dbd5f239d69ea
version: ""
- filename: apis.rest
checksum: 2e52807fb85686dd07d53d8af0296040d2c4b0cd0e3389fee66bd9c86ad501c0
- filename: backend/src/auth/auth.service.ts
checksum: b1e774f02679869d2647ea66be0b6f56163b3dff854af6f66b9512db235a2529
- filename: backend/src/users/users.service.ts
checksum: d6456d97232e89cec4e3960ebb6d1b29d9d9cf571a23ec72035ea7de8e63c60f
version: ""
9 changes: 9 additions & 0 deletions apis.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
POST http://localhost:3001/login
Content-Type: application/json

{
"userId": 2,
"username": "maria",
"password": "guess"
}

14 changes: 3 additions & 11 deletions backend/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';

@ApiTags('App')
@ApiTags('App')
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
@ApiOperation({ summary: 'Get a welcome message' })
@ApiResponse({ status: 200, description: 'Return Hello World message' })
@ApiOperation({ summary: 'Get a welcome message' })
@ApiResponse({ status: 200, description: 'Return Hello World message' })
getHello(): string {
return this.appService.getHello();
}

// Dummy API
@Get('dummy')
@ApiOperation({ summary: 'Get dummy data' })
@ApiResponse({ status: 200, description: 'Dummy data returned successfully' })
getDummy(): string {
return 'This is dummy data';
}
}
4 changes: 3 additions & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { UsersModule } from './users/users.module';

@Module({
imports: [],
imports: [AuthModule, UsersModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
24 changes: 24 additions & 0 deletions backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Body, Controller, HttpCode, HttpStatus, Post } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { UserDto } from 'src/users/users.dto';

@ApiTags('Authentication')
@Controller()
export class AuthController {
constructor(private readonly authService: AuthService) {}

@Post('signup')
@ApiOperation({ summary: 'Register new user' })
@ApiResponse({ status: 201, description: 'API for registering new user' })
async signupUser(): Promise<string> {
return this.authService.signupUser();
}

@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Login user' })
@Post('login')
signIn(@Body() signInDto: UserDto) {
return this.authService.signIn(signInDto.username, signInDto.password);
}
}
11 changes: 11 additions & 0 deletions backend/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { UsersModule } from 'src/users/users.module';

@Module({
imports: [UsersModule],
controllers: [AuthController],
providers: [AuthService],
})
export class AuthModule {}
21 changes: 21 additions & 0 deletions backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { UsersService } from 'src/users/users.service';

@Injectable()
export class AuthService {
constructor(private readonly usersService: UsersService) {}
async signupUser() {
return 'hello';
}

async signIn(username: string, pass: string): Promise<any> {
const user = await this.usersService.findOne(username);
if (user?.password !== pass) {
throw new UnauthorizedException();
}
const { password, ...result } = user;
// TODO: Generate a JWT and return it here
// instead of the user object
return { success: true, result };
}
}
Empty file.
4 changes: 2 additions & 2 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ async function bootstrap() {
const app = await NestFactory.create(AppModule);

const config = new DocumentBuilder()
.setTitle('NestJS API')
.setDescription('NestJS API documentation')
.setTitle('NetVibe')
.setDescription('Emphasizing networking with an energetic twist')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
Expand Down
10 changes: 10 additions & 0 deletions backend/src/users/users.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';

export class UserDto {
@ApiProperty()
userId: string;
@ApiProperty()
username: string;
@ApiProperty()
password: string;
}
10 changes: 10 additions & 0 deletions backend/src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { UsersService } from './users.service';

@Module({
imports: [],
controllers: [],
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}
23 changes: 23 additions & 0 deletions backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Injectable } from '@nestjs/common';

export type User = { userId: number; username: string; password: string };

@Injectable()
export class UsersService {
private readonly users = [
{
userId: 1,
username: 'john',
password: 'changeme',
},
{
userId: 2,
username: 'maria',
password: 'guess',
},
];

async findOne(username: string): Promise<User | undefined> {
return this.users.find((user) => user.username === username);
}
}
Loading