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

Commit

Permalink
♻️ Use safe email helper
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 27, 2020
1 parent 62d6ae0 commit f7697eb
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/helpers/safe-email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const safeEmail = (email: string) => email;
6 changes: 4 additions & 2 deletions src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { EmailModule } from '../email/email.module';
import { PrismaModule } from '../prisma/prisma.module';
import { UsersService } from '../user/user.service';
import { PwnedModule } from '../pwned/pwned.module';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { JwtStrategy } from './jwt.strategy';
Expand All @@ -15,11 +15,13 @@ import { JwtStrategy } from './jwt.strategy';
PrismaModule,
EmailModule,
ConfigModule,
PwnedModule,
JwtModule.register({
secret: process.env.JWT_SECRET ?? 'staart',
}),
],
controllers: [AuthController],
providers: [AuthService, UsersService, JwtStrategy],
exports: [AuthService],
providers: [AuthService, JwtStrategy],
})
export class AuthModule {}
8 changes: 4 additions & 4 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ import { JwtService } from '@nestjs/jwt';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import { AccessTokenClaims } from './auth.interface';
import { PwnedService } from '../pwned/pwned.service';
import { safeEmail } from 'src/helpers/safe-email';

@Injectable()
export class AuthService {
constructor(
private prisma: PrismaService,
private users: UsersService,
private email: EmailService,
private configService: ConfigService,
private jwtService: JwtService,
private pwnedService: PwnedService,
) {}

async validateUser(email: string, password?: string): Promise<number> {
const emailSafe = this.users.getSafeEmail(email);
const emailSafe = safeEmail(email);
const user = await this.prisma.users.findFirst({
where: { emails: { some: { emailSafe } } },
select: { id: true, password: true, emails: true },
Expand Down Expand Up @@ -67,7 +67,7 @@ export class AuthService {

async register(data: RegisterDto): Promise<Expose<users>> {
const email = data.email;
const emailSafe = this.users.getSafeEmail(email);
const emailSafe = safeEmail(email);
const ignorePwnedPassword = !!data.ignorePwnedPassword;
delete data.email;
delete data.ignorePwnedPassword;
Expand Down Expand Up @@ -98,7 +98,7 @@ export class AuthService {
}

async sendEmailVerification(email: string, resend = false) {
const emailSafe = this.users.getSafeEmail(email);
const emailSafe = safeEmail(email);
const emailDetails = await this.prisma.emails.findFirst({
where: { emailSafe },
include: { user: true },
Expand Down
2 changes: 2 additions & 0 deletions src/modules/emails/emails.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { JwtModule } from '@nestjs/jwt';
import { AuthService } from '../auth/auth.service';
import { EmailModule } from '../email/email.module';
import { PrismaModule } from '../prisma/prisma.module';
import { PwnedModule } from '../pwned/pwned.module';
import { UsersService } from '../user/user.service';
import { EmailController } from './emails.controller';
import { EmailsService } from './emails.service';
Expand All @@ -13,6 +14,7 @@ import { EmailsService } from './emails.service';
PrismaModule,
EmailModule,
ConfigModule,
PwnedModule,
JwtModule.register({
secret: process.env.JWT_SECRET ?? 'staart',
}),
Expand Down
3 changes: 2 additions & 1 deletion src/modules/emails/emails.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
emailsWhereInput,
emailsWhereUniqueInput,
} from '@prisma/client';
import { safeEmail } from 'src/helpers/safe-email';
import { Expose } from 'src/modules/prisma/prisma.interface';
import { AuthService } from '../auth/auth.service';
import { PrismaService } from '../prisma/prisma.service';
Expand All @@ -28,7 +29,7 @@ export class EmailsService {
userId: number,
data: Omit<Omit<emailsCreateInput, 'emailSafe'>, 'user'>,
): Promise<emails> {
const emailSafe = this.users.getSafeEmail(data.email);
const emailSafe = safeEmail(data.email);
const result = await this.prisma.emails.create({
data: { ...data, emailSafe, user: { connect: { id: userId } } },
});
Expand Down
3 changes: 2 additions & 1 deletion src/modules/memberships/memberships.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
membershipsWhereInput,
membershipsWhereUniqueInput,
} from '@prisma/client';
import { safeEmail } from 'src/helpers/safe-email';
import { Expose } from 'src/modules/prisma/prisma.interface';
import { AuthService } from '../auth/auth.service';
import { PrismaService } from '../prisma/prisma.service';
Expand Down Expand Up @@ -135,7 +136,7 @@ export class MembershipsService {
}

async createGroupMembership(groupId: number, data: CreateMembershipInput) {
const emailSafe = this.users.getSafeEmail(data.email);
const emailSafe = safeEmail(data.email);
let user = this.prisma.expose(
await this.prisma.users.findFirst({
where: { emails: { some: { emailSafe } } },
Expand Down
4 changes: 3 additions & 1 deletion src/modules/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Module } from '@nestjs/common';
import { AuthModule } from '../auth/auth.module';
import { PrismaModule } from '../prisma/prisma.module';
import { UserController } from './user.controller';
import { UsersService } from './user.service';

@Module({
imports: [PrismaModule],
imports: [PrismaModule, AuthModule],
controllers: [UserController],
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}
6 changes: 0 additions & 6 deletions src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,4 @@ export class UsersService {
});
return this.prisma.expose<users>(user);
}

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

0 comments on commit f7697eb

Please sign in to comment.