LocChat/server/controllers/users.controller.ts

78 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-12-03 20:40:37 -05:00
import { Body, Controller, Get, HttpException, HttpStatus, Post, Res } from '@nestjs/common';
2021-11-20 21:34:10 -05:00
import * as bcrypt from 'bcrypt';
2021-11-20 20:18:58 -05:00
import { Response } from 'express';
2021-11-23 16:04:12 -05:00
import { JwtBody } from 'server/decorators/jwt_body.decorator';
2021-12-01 22:18:26 -05:00
import { Roles } from 'server/decorators/roles.decorator';
import { Skip } from 'server/decorators/skip.decorator';
2021-11-20 20:18:58 -05:00
import { CreateUserDto } from 'server/dto/create_user.dto';
2021-11-23 16:04:12 -05:00
import { JwtBodyDto } from 'server/dto/jwt_body.dto';
import { RefreshToken } from 'server/entities/refresh_token.entity';
2021-12-01 22:18:26 -05:00
import { RoleKey } from 'server/entities/role.entity';
2021-11-20 20:18:58 -05:00
import { User } from 'server/entities/user.entity';
2021-12-01 22:18:26 -05:00
import { UserRole } from 'server/entities/user_role.entity';
2021-11-23 16:04:12 -05:00
import { AuthGuard } from 'server/providers/guards/auth.guard';
import { JwtService } from 'server/providers/services/jwt.service';
import { RefreshTokensService } from 'server/providers/services/refresh_tokens.service';
2021-12-01 22:18:26 -05:00
import { RolesService } from 'server/providers/services/roles.service';
2021-11-20 20:18:58 -05:00
import { UsersService } from 'server/providers/services/users.service';
@Controller()
export class UsersController {
2021-11-23 16:04:12 -05:00
constructor(
private usersService: UsersService,
2021-12-01 22:18:26 -05:00
private rolesService: RolesService,
2021-11-23 16:04:12 -05:00
private jwtService: JwtService,
private refreshTokenService: RefreshTokensService,
) {}
2021-12-01 22:18:26 -05:00
@Get('/users')
@Roles(RoleKey.ADMIN)
async index() {
const users = await this.usersService.findAll();
return { users };
}
2021-11-23 16:04:12 -05:00
@Get('/users/me')
async getCurrentUser(@JwtBody() jwtBody: JwtBodyDto) {
const user = await this.usersService.find(jwtBody.userId);
return { user };
}
2021-11-20 20:18:58 -05:00
@Post('/users')
2021-12-01 22:18:26 -05:00
@Skip(AuthGuard)
2021-11-23 16:04:12 -05:00
async create(@Body() userPayload: CreateUserDto, @Res({ passthrough: true }) res: Response) {
2021-11-20 20:18:58 -05:00
const newUser = new User();
newUser.email = userPayload.email;
2021-12-03 20:31:24 -05:00
newUser.firstName = userPayload.firstName;
newUser.lastName = userPayload.lastName;
2021-11-23 16:04:12 -05:00
newUser.passwordHash = await bcrypt.hash(userPayload.password, 10);
2021-12-01 22:18:26 -05:00
const [role] = await this.rolesService.findByKey(RoleKey.USER);
const userRole = new UserRole();
userRole.role = role;
newUser.userRoles = [userRole];
2021-11-20 20:18:58 -05:00
try {
const user = await this.usersService.create(newUser);
2021-11-23 16:04:12 -05:00
// create refresh token in database for user
const newRefreshToken = new RefreshToken();
newRefreshToken.user = user;
const refreshToken = await this.refreshTokenService.create(newRefreshToken);
// issue jwt and refreshJwtToken
2021-12-01 22:18:26 -05:00
// note the roles hard coded to just USER.
// If you want to allow users to sign up as different roles then
// you will need to update this here.
const token = this.jwtService.issueToken({ userId: user.id, roles: [RoleKey.USER] });
2021-11-23 16:04:12 -05:00
const refreshJwtToken = this.jwtService.issueRefreshToken({ id: refreshToken.id, userId: user.id });
// only refresh token should go in the cookie
res.cookie('_refresh_token', refreshJwtToken, {
httpOnly: true, // prevents javascript code from accessing cookie (helps protect against XSS attacks)
});
2021-11-20 20:18:58 -05:00
return { user, token };
} catch (e) {
2021-11-23 16:04:12 -05:00
throw new HttpException(`User creation failed. ${e.message}`, HttpStatus.BAD_REQUEST);
2021-11-20 20:18:58 -05:00
}
}
}