Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Emails are stored in lowercase #82

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import { AuthGuard } from '../auth/guard/auth/auth.guard';
import { PrismaModule } from '../prisma/prisma.module';
import { MailModule } from '../mail/mail.module';
import { ProviderModule } from '../provider/provider.module';
import { CommonModule } from '../common/common.module';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
CommonModule,
AuthModule,
UserModule,
OauthModule,
Expand Down
5 changes: 4 additions & 1 deletion src/auth/controller/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
ApiTags,
} from '@nestjs/swagger';
import { userProperties } from '../../schemas/user.properties';
import { LowercasePipe } from '../../common/pipes/lowercase.pipe';

@Controller('auth')
@ApiTags('Auth Controller')
Expand Down Expand Up @@ -195,7 +196,9 @@ export class AuthController {
description: 'User not found',
})
@HttpCode(HttpStatus.NO_CONTENT)
async resendEmailVerificationCode(@Param('email') email: string) {
async resendEmailVerificationCode(
@Param('email', LowercasePipe) email: string,
) {
return await this.authService.resendEmailVerificationCode(email);
}

Expand Down
2 changes: 2 additions & 0 deletions src/auth/dto/email-verification.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsEmail, Matches } from 'class-validator';

export class EmailVerificationDto {
Expand All @@ -10,6 +11,7 @@ export class EmailVerificationDto {
type: String,
example: '[email protected]',
})
@Transform(({ value }) => value.toLowerCase())
email: string;
@Matches(/^[0-9]{6}$/, {
message: 'Code must be a 6 digit number',
Expand Down
2 changes: 2 additions & 0 deletions src/auth/dto/signin.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsEmail } from 'class-validator';

export class SigninDto {
@IsEmail()
@Transform(({ value }) => value.toLowerCase())
@ApiProperty({
name: 'email',
description: 'User email. Must be a valid email address.',
Expand Down
2 changes: 2 additions & 0 deletions src/auth/dto/signup.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsEmail } from 'class-validator';

export class SignupDto {
@IsEmail()
@Transform(({ value }) => value.toLowerCase())
@ApiProperty({
name: 'email',
description: 'User email. Must be a valid email address.',
Expand Down
4 changes: 3 additions & 1 deletion src/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export class AuthService {
profilePictureUrl?: string,
throwErrorIfUserExists?: boolean,
) {
email = email.toLowerCase();
let user = await this.findUserByEmail(email);
if (user && throwErrorIfUserExists) {
throw new ConflictException('User already exists');
Expand All @@ -209,7 +210,7 @@ export class AuthService {
if (!user) {
user = await this.prisma.user.create({
data: {
email: email,
email,
name: name,
profilePictureUrl: profilePictureUrl,
authType,
Expand Down Expand Up @@ -241,6 +242,7 @@ export class AuthService {
}

private async findUserByEmail(email: string) {
email = email.toLowerCase();
return await this.prisma.user.findUnique({
where: {
email,
Expand Down
5 changes: 5 additions & 0 deletions src/common/common.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Global, Module } from '@nestjs/common';

@Global()
@Module({})
export class CommonModule {}
8 changes: 8 additions & 0 deletions src/common/pipes/lowercase.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PipeTransform, Injectable } from '@nestjs/common';

@Injectable()
export class LowercasePipe implements PipeTransform {
transform(value: string) {
return value.toLowerCase();
}
}
2 changes: 1 addition & 1 deletion src/user/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class UserService {
*/
async linkSocialAccount(req: any, socialAccountType: SocialAccountType) {
const { emails, profileUrl } = req.user;
const email = emails[0].value;
const email = emails[0].value.toLowerCase();

// Check if the user exists
const currentUser = await this.prisma.user.findUniqueOrThrow({
Expand Down
Loading