This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ludovicGendre
committed
Jan 27, 2022
1 parent
3f66a00
commit b37adf3
Showing
8 changed files
with
154 additions
and
3 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
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,6 +1,9 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNotEmpty, IsString } from 'class-validator'; | ||
|
||
export class AuthTokenDto { | ||
@ApiProperty() | ||
@IsString() | ||
@IsNotEmpty() | ||
public token!: string; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
Controller, | ||
ClassSerializerInterceptor, | ||
UseInterceptors, | ||
Post, | ||
Body, | ||
} from '@nestjs/common'; | ||
import { AuthTokenDto } from 'src/dtos'; | ||
import { EmailConfirmationService } from './emailConfirmation.service'; | ||
// import JwtAuthenticationGuard from '../authentication/jwt-authentication.guard'; | ||
// import RequestWithUser from '../emailSchedule.controller'; | ||
|
||
@Controller('email-confirmation') | ||
@UseInterceptors(ClassSerializerInterceptor) | ||
export class EmailConfirmationController { | ||
constructor( | ||
private readonly emailConfirmationService: EmailConfirmationService, | ||
) {} | ||
|
||
@Post('confirm') | ||
async confirm(@Body() confirmationData: AuthTokenDto) { | ||
const email = await this.emailConfirmationService.decodeConfirmationToken( | ||
confirmationData.token, | ||
); | ||
await this.emailConfirmationService.confirmEmail(email); | ||
} | ||
|
||
// @Post('resend-confirmation-link') | ||
// @UseGuards(JwtAuthenticationGuard) | ||
// async resendConfirmationLink(@Req() request: RequestWithUser) { | ||
// await this.emailConfirmationService.resendConfirmationLink(request.user.id); | ||
// } | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { | ||
Injectable, | ||
CanActivate, | ||
ExecutionContext, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { Request } from 'express'; | ||
import { UserDto } from 'src/dtos'; | ||
|
||
export interface RequestWithUser extends Request { | ||
user: UserDto; | ||
} | ||
|
||
@Injectable() | ||
export class EmailConfirmationGuard implements CanActivate { | ||
canActivate(context: ExecutionContext) { | ||
const request: RequestWithUser = context.switchToHttp().getRequest(); | ||
|
||
if (!request.user?.isEmailConfirmed) { | ||
throw new UnauthorizedException('Confirm your email first'); | ||
} | ||
|
||
return true; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PrismaService } from 'src/prisma.service'; | ||
import { UserService } from 'src/user/user.service'; | ||
import EmailService from '../email.service'; | ||
import { EmailConfirmationController } from './emailConfirmation.controller'; | ||
import { EmailConfirmationService } from 'src/email/confirmation/emailConfirmation.service'; | ||
import { JwtModule, JwtService } from '@nestjs/jwt'; | ||
import { AuthService } from 'src/auth/auth.service'; | ||
import { AuthController } from 'src/auth/auth.controller'; | ||
import { AuthModule } from 'src/auth/auth.module'; | ||
import { JwtStrategy } from 'src/auth/jwt-strategy'; | ||
import { ConfigService } from 'config/config.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
JwtModule.registerAsync({ | ||
inject: [ConfigService], | ||
useFactory: (configService: ConfigService) => ({ | ||
secret: configService.jwtSecret, | ||
signOptions: { expiresIn: '8h' }, | ||
}), | ||
}), | ||
], | ||
controllers: [EmailConfirmationController, AuthController], | ||
providers: [ | ||
AuthService, | ||
EmailService, | ||
JwtStrategy, | ||
EmailConfirmationService, | ||
PrismaService, | ||
UserService, | ||
], | ||
}) | ||
export class EmailConfirmationModule {} |
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
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