This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add registration with email conflict check
- Loading branch information
1 parent
49302a6
commit c929153
Showing
5 changed files
with
53 additions
and
9 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
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 {} |
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,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 {} |
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,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); | ||
} | ||
} |
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