Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add registration with email conflict check
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 22, 2020
1 parent 49302a6 commit c929153
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { RateLimiterInterceptor, RateLimiterModule } from 'nestjs-rate-limiter';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './modules/auth/auth.module';
import { PrismaModule } from './modules/prisma/prisma.module';
import { UsersModule } from './modules/user/user.module';

@Module({
imports: [PrismaModule, UsersModule, AuthModule],
imports: [PrismaModule, UsersModule, AuthModule, RateLimiterModule],
controllers: [AppController],
providers: [AppService],
providers: [
AppService,
{
provide: APP_INTERCEPTOR,
useClass: RateLimiterInterceptor,
},
],
})
export class AppModule {}
6 changes: 6 additions & 0 deletions src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Body, Controller, Post } from '@nestjs/common';
import { users } from '@prisma/client';
import { RateLimit } from 'nestjs-rate-limiter';
import { OmitSecrets } from 'src/modules/prisma/prisma.interface';
import { RegisterDto } from './auth.dto';
import { AuthService } from './auth.service';
Expand All @@ -9,6 +10,11 @@ export class AuthController {
constructor(private authService: AuthService) {}

@Post('register')
@RateLimit({
points: 10,
duration: 60,
errorMessage: 'Wait for 60 seconds before trying to create an account',
})
async update(@Body() data: RegisterDto): Promise<OmitSecrets<users>> {
return this.authService.register(data);
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from '../prisma/prisma.module';
import { UsersService } from '../user/user.service';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';

@Module({
imports: [PrismaModule],
controllers: [AuthController],
providers: [AuthService],
providers: [AuthService, UsersService],
})
export class AuthModule {}
35 changes: 29 additions & 6 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
import { Injectable } from '@nestjs/common';
import { users, usersCreateInput } from '@prisma/client';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { users } from '@prisma/client';
import { OmitSecrets } from '../prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
import { UsersService } from '../user/user.service';
import { RegisterDto } from './auth.dto';

@Injectable()
export class AuthService {
constructor(private prisma: PrismaService) {}
constructor(private prisma: PrismaService, private users: UsersService) {}

async register(data: usersCreateInput): Promise<users> {
return this.prisma.users.create({
data,
async register(data: RegisterDto): Promise<OmitSecrets<users>> {
const email = data.email;
const emailSafe = this.users.getSafeEmail(email);
delete data.email;

const users = await this.users.users({
take: 1,
where: { emails: { some: { emailSafe } } },
});
if (users.length)
throw new HttpException(
'A user with this email already exists',
HttpStatus.CONFLICT,
);

const user = await this.prisma.users.create({
data: {
...data,
emails: {
create: { email: email, emailSafe },
},
},
});
return this.prisma.expose(user);
}
}
6 changes: 6 additions & 0 deletions src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@ export class UsersService {
});
return this.prisma.expose<users>(user);
}

getSafeEmail(email: string) {
email = email.toLowerCase();
email = `${email.split('@', 1)[0].split('+')}[0]@${email.split('@', 1)[1]}`;
return email;
}
}

0 comments on commit c929153

Please sign in to comment.