icynet-auth-server/src/modules/features/register/register.controller.ts

105 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-03-09 18:37:04 +00:00
import {
Body,
Controller,
Get,
Post,
Query,
Render,
Req,
Res,
2022-08-22 17:39:31 +00:00
UnauthorizedException,
2022-03-09 18:37:04 +00:00
} from '@nestjs/common';
2022-03-16 18:37:50 +00:00
import { Throttle } from '@nestjs/throttler';
2022-03-09 18:37:04 +00:00
import { Request, Response } from 'express';
2022-08-22 17:39:31 +00:00
import { ConfigurationService } from 'src/modules/config/config.service';
2022-03-09 18:37:04 +00:00
import { UserService } from 'src/modules/objects/user/user.service';
import { FormUtilityService } from 'src/modules/utility/services/form-utility.service';
import { RegisterDto } from './register.interfaces';
@Controller('/register')
export class RegisterController {
constructor(
private readonly userService: UserService,
private readonly formUtil: FormUtilityService,
2022-08-22 17:39:31 +00:00
private readonly config: ConfigurationService,
2022-03-09 18:37:04 +00:00
) {}
@Get()
@Render('register')
2022-03-20 14:50:12 +00:00
public registerView(@Req() req: Request): Record<string, any> {
2022-08-22 17:39:31 +00:00
return this.formUtil.populateTemplate(req, {
registrationAuthorized: this.config.get<boolean>('app.registrations'),
});
2022-03-09 18:37:04 +00:00
}
@Post()
2022-03-16 18:37:50 +00:00
@Throttle(3, 10)
2022-03-09 18:37:04 +00:00
public async registerRequest(
@Req() req: Request,
@Res() res: Response,
@Body() body: RegisterDto,
2022-03-20 17:05:21 +00:00
@Query('redirectTo') redirectTo?: string,
2022-03-09 18:37:04 +00:00
) {
2022-08-17 18:56:47 +00:00
const { username, display_name, email, password, password_repeat } =
this.formUtil.trimmed(body, ['username', 'display_name', 'email']);
2022-03-09 18:37:04 +00:00
2022-08-22 17:39:31 +00:00
if (!this.config.get<boolean>('app.registrations')) {
throw new UnauthorizedException(
'Registrations are disabled by administrator.',
);
}
2022-03-09 18:37:04 +00:00
try {
if (
!username ||
!display_name ||
!email ||
!password ||
!password_repeat
) {
throw new Error('Please fill out all of the fields!');
}
if (!username || !username.match(this.formUtil.usernameRegex)) {
throw new Error(
'Username must be alphanumeric and between 3 to 26 characters long (_, - and . are also allowed)',
);
}
if (display_name.length < 3 || display_name.length > 32) {
throw new Error(
'Display name must be between 3 and 32 characters long.',
);
}
// https://stackoverflow.com/a/21456918
if (!password.match(this.formUtil.passwordRegex)) {
throw new Error(
'Password must be at least 8 characters long, contain a capital and lowercase letter and a number',
);
}
if (!email.match(this.formUtil.emailRegex)) {
throw new Error('Invalid email address!');
}
if (password !== password_repeat) {
throw new Error('The passwords do not match!');
}
2022-03-20 17:05:21 +00:00
await this.userService.userRegistration(body, redirectTo);
2022-03-09 18:37:04 +00:00
req.flash('message', {
error: false,
text: `An activation email has been sent to ${email}!`,
});
2022-03-20 17:05:21 +00:00
res.redirect('/login' + (redirectTo ? '?redirectTo=' + redirectTo : ''));
2022-03-09 18:37:04 +00:00
} catch (e: any) {
req.flash('message', { error: true, text: e.message });
req.flash('form', { ...body, password_repeat: undefined });
res.redirect(req.originalUrl);
}
}
}