-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs(backend): add some backend docs (#45) * Clean some files * Clean README * Cleaning docks * Updating docs format * Updating docs format * Updating docs format * Update Summary * docs(backend): add backend structure to doc * docs(structure): add structure db file * docs(structure): rename structure file * feat(user): add some user field, also display avatar correctly * chore(middleware): apply middleware on module instead of calling it for each handler in controller * chore(guard): wip, guard cant work because of httponly on cookies, need to add https access to enable guards working with this security * chore(security): nginx added, https worked. Also changed all call to the app by the new port * WIP: websocket with proxy * Solve vite websockets HMR * fix our websockets * Change phoneNumber to phone * Dont expose ports on the hosts for app and back * Prettify the Makefile * Stop listening on HTTP port for nginx * Implement env vars in frontend * Implement env vars in backend * UsersMiddleware now get token from auth header, and not from cookie * Backend: fix routes for User controller --------- Co-authored-by: Tanguy Rossel <tanguy.rossel@tyrossel.ch>
- Loading branch information
Showing
35 changed files
with
355 additions
and
172 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
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
10 changes: 10 additions & 0 deletions
10
backend/prisma/migrations/20230319130910_phone_edit/migration.sql
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 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `phoneNumber` on the `User` table. All the data in the column will be lost. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "User" DROP COLUMN "phoneNumber", | ||
ADD COLUMN "phone" TEXT, | ||
ADD COLUMN "twoFASecret" TEXT; |
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
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,5 +1,11 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
/** | ||
* @JwtStrategy : Guard typically uses a Strategy to validate the JWT | ||
* | ||
* @AuthGuard: Have a canActivate() method from the CanActivate interface | ||
* This method is called by NestJS before a route is activated : it returns a boolean value that determines whether the route should be activated or not | ||
*/ | ||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('jwt') {} |
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
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 |
---|---|---|
@@ -1,59 +1,67 @@ | ||
import { Controller, Get, Post, Body, Next, Patch, Param, Delete, Req, Res } from '@nestjs/common'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { | ||
Controller, | ||
Get, | ||
Post, | ||
Body, | ||
Patch, | ||
Param, | ||
Delete, | ||
Req, | ||
Res, | ||
UseGuards, | ||
ParseIntPipe | ||
} from '@nestjs/common'; | ||
import { Request, Response } from 'express'; | ||
import { UsersService } from './users.service'; | ||
import { User } from '@prisma/client'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { UsersMiddleware } from './middlewares/users.middleware'; | ||
import { JwtAuthGuard } from '../auth/guards/jwt.guard'; | ||
|
||
@Controller('users') | ||
@UseGuards(JwtAuthGuard) | ||
@ApiTags('users') | ||
export class UsersController { | ||
constructor( | ||
private readonly authMiddleware: UsersMiddleware, | ||
private readonly usersService: UsersService) {} | ||
private readonly usersService: UsersService | ||
) {} | ||
|
||
@Get('login') | ||
async login(@Req() req: Request, @Res() res: Response, @Next() next: NextFunction) { | ||
await new Promise(resolve => this.authMiddleware.use(req, res, resolve)); | ||
const user = req.user; | ||
if (!user) { | ||
res.status(401).send('Unauthorized'); | ||
} else { | ||
res.status(200).send(user); | ||
} | ||
async login(@Req() req: Request, @Res() res: Response) { | ||
res.status(200).send(req.user); | ||
} | ||
|
||
@Post('create') | ||
async createUser(@Body() data: User): Promise<User> { | ||
return this.usersService.create(data); | ||
} | ||
|
||
@Get(':id') | ||
async getUserById(@Param('id') userId: number) { | ||
return this.usersService.findById(userId); | ||
@Get('all') | ||
async getAllUsers(@Req() req: Request, @Res() res: Response) { | ||
const users = await this.usersService.findAll(); | ||
res.status(200).send({ users }); | ||
} | ||
|
||
@Get() | ||
async getAllUsers(@Req() req: Request, @Res() res: Response) { | ||
if (!req.user) { | ||
res.status(401).send('Unauthorized'); | ||
} else { | ||
const users = await this.usersService.getAllUsers(); | ||
res.send({ users }); | ||
} | ||
return this.usersService.findAll(); | ||
/** | ||
* ParseIntPipe : protection to ensures that a method handler parameter is converted to a JavaScript integer | ||
* (or throws an exception if the conversion fails). | ||
* | ||
* @param userId | ||
*/ | ||
@Get('id/:id') | ||
async getUserById(@Param('id', ParseIntPipe) userId: number) { | ||
return this.usersService.findById(userId); | ||
} | ||
|
||
@Patch(':id') | ||
@Patch('id/:id') | ||
async updateUser( | ||
@Param('id') userId: number, | ||
@Param('id', ParseIntPipe) userId: number, | ||
@Body() data: User | ||
): Promise<User> { | ||
return this.usersService.update(userId, data); | ||
} | ||
|
||
@Delete(':id') | ||
async deleteUser(@Param('id') userId: number) { | ||
@Delete('id/:id') | ||
async deleteUser(@Param('id', ParseIntPipe) userId: number) { | ||
return this.usersService.delete(userId); | ||
} | ||
} |
Oops, something went wrong.