Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Endpoint for registering new users (#231)
Browse files Browse the repository at this point in the history
* added API endpoint, DTO, updated API schema

* only enabled users get authenticated

* Update backend/src/db/repositories/user.repository.ts

---------

Co-authored-by: Henry Brink <[email protected]>
  • Loading branch information
OG-NI and henrybrink authored Jun 12, 2024
1 parent e1c48b0 commit 3efbdd6
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 19 deletions.
70 changes: 67 additions & 3 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@nestjs/core": "^10.0.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.3.7",
"@nestjs/swagger": "^7.3.1",
"@prisma/client": "^5.11.0",
"@types/bcrypt": "^5.0.2",
"@types/nodemailer": "^6.4.14",
Expand Down
17 changes: 3 additions & 14 deletions backend/src/api/user/patch.user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import { IsEmail, IsOptional, Length } from 'class-validator';
import { PartialType } from '@nestjs/swagger';
import { RegisterUserDTO } from './register.user.dto';

export class PatchUserDTO {
@IsEmail()
@IsOptional()
email: string | undefined;

@Length(12, 72)
@IsOptional()
password: string | undefined;

@Length(2, 128)
@IsOptional()
displayName: string | undefined;
}
export class PatchUserDTO extends PartialType(RegisterUserDTO) {}
12 changes: 12 additions & 0 deletions backend/src/api/user/register.user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IsEmail, Length } from 'class-validator';

export class RegisterUserDTO {
@IsEmail()
email: string;

@Length(12, 72)
password: string;

@Length(2, 128)
displayName: string;
}
12 changes: 12 additions & 0 deletions backend/src/api/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Controller,
Get,
Patch,
Post,
Req,
Res,
UseGuards,
Expand All @@ -11,6 +12,7 @@ import { Prisma, User } from '@prisma/client';
import { Response } from 'express';
import { AutoGuard } from '../../auth/auto.guard';
import { PatchUserDTO } from './patch.user.dto';
import { RegisterUserDTO } from './register.user.dto';
import { AuthService } from '../../auth/auth.service';
import { UserRepository } from '../../db/repositories/user.repository';
import { NestRequest } from '../../types/request.type';
Expand All @@ -31,6 +33,16 @@ export class UserController {
return sanitizedUser;
}

@Post('/')
async postRegister(@Body() userPatch: RegisterUserDTO) {
this.userRepository.createUser(
userPatch.email,
userPatch.displayName,
userPatch.password,
false,
);
}

/**
* /user/me
*
Expand Down
6 changes: 5 additions & 1 deletion backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export class AuthService {
): Promise<User | null> {
const user = await this.userRepository.findByEmail(username);

if (user && (await bcrypt.compare(password, user.password))) {
if (
user &&
user.enabled &&
(await bcrypt.compare(password, user.password))
) {
return user;
}

Expand Down
2 changes: 2 additions & 0 deletions backend/src/db/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ export class UserRepository {
email: string,
displayName: string,
password: string,
enabled: boolean = false,
): Promise<User> {
return await this.prisma.user.create({
data: {
email,
displayName,
password,
enabled,
},
});
}
Expand Down
28 changes: 27 additions & 1 deletion docs/public/api/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,39 @@ externalDocs:
url: https://github.com/SE-TINF22B2/G5-DuoGradus
tags:
- name: User
description: Endpoints relatexd to the user, his account and other personal configurations
description: Endpoints related to the user, his account and other personal configurations
- name: Datasource
description: Thirdparty data sources, like FitBit
- name: Leaderboard
- name: Goal
description: User and system defined Goals
paths:
'/user':
post:
summary: Registers a new user
tags:
- User
requestBody:
content:
application/json:
schema:
type: object
properties:
email:
type: string
displayName:
type: string
password:
type: string
example:
name: Ingolf Neiße
email: [email protected]
password: password123
responses:
'200':
description: User registered successfully
'400':
description: Invalid content
'/user/me':
get:
summary: Displays the current users settings
Expand Down

0 comments on commit 3efbdd6

Please sign in to comment.