-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from akbatra567/main
feat: [#7] - Add. static authentication for SignIn api
- Loading branch information
Showing
12 changed files
with
123 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |