nicolas-arnaud
2 years ago
23 changed files with 5080 additions and 1201 deletions
File diff suppressed because it is too large
@ -1,14 +1,54 @@ |
|||||
import { Module } from '@nestjs/common' |
import { Module } from '@nestjs/common' |
||||
import { UsersModule } from 'src/users/users.module' |
import { UsersModule } from 'src/users/users.module' |
||||
import { PassportModule } from '@nestjs/passport' |
import { PassportModule } from '@nestjs/passport' |
||||
import { ConfigService } from '@nestjs/config' |
import { ConfigModule, ConfigService } from '@nestjs/config' |
||||
import { AuthController } from './auth.controller' |
import { AuthController } from './auth.controller' |
||||
import { FtStrategy } from './42.strategy' |
import { FtStrategy } from './42.strategy' |
||||
import { SessionSerializer } from './session.serializer' |
import { SessionSerializer } from './session.serializer' |
||||
|
import { JwtModule } from '@nestjs/jwt' |
||||
|
import { MailerModule } from '@nestjs-modules/mailer' |
||||
|
import { AuthService } from './auth.service' |
||||
|
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter' |
||||
|
|
||||
|
const mail_user = |
||||
|
process.env.MAIL_USER && process.env.MAIL_USER !== '' |
||||
|
? process.env.MAIL_USER |
||||
|
: '' |
||||
|
const mail_pass = |
||||
|
process.env.MAIL_PASSWORD && process.env.MAIL_PASSWORD !== '' |
||||
|
? process.env.MAIL_PASSWORD |
||||
|
: '' |
||||
|
|
||||
@Module({ |
@Module({ |
||||
imports: [UsersModule, PassportModule], |
imports: [ |
||||
providers: [ConfigService, FtStrategy, SessionSerializer], |
UsersModule, |
||||
|
PassportModule, |
||||
|
ConfigModule.forRoot(), |
||||
|
JwtModule.register({ |
||||
|
secret: process.env.JWT_SECRET, |
||||
|
signOptions: { expiresIn: '60s' } |
||||
|
}), |
||||
|
MailerModule.forRoot({ |
||||
|
transport: { |
||||
|
service: 'gmail', |
||||
|
auth: { |
||||
|
user: mail_user, |
||||
|
pass: mail_pass |
||||
|
} |
||||
|
}, |
||||
|
template: { |
||||
|
dir: 'src/auth/mails', |
||||
|
adapter: new HandlebarsAdapter(), |
||||
|
options: { |
||||
|
strict: true |
||||
|
} |
||||
|
}, |
||||
|
defaults: { |
||||
|
from: '"No Reply" vaganiwast@gmail.com' |
||||
|
} |
||||
|
}) |
||||
|
], |
||||
|
providers: [ConfigService, FtStrategy, SessionSerializer, AuthService], |
||||
controllers: [AuthController] |
controllers: [AuthController] |
||||
}) |
}) |
||||
export class AuthModule {} |
export class AuthModule {} |
||||
|
@ -0,0 +1,55 @@ |
|||||
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common' |
||||
|
import { type User } from 'src/users/entity/user.entity' |
||||
|
import { UsersService } from 'src/users/users.service' |
||||
|
import { MailerService } from '@nestjs-modules/mailer' |
||||
|
import { UserDto } from 'src/users/dto/user.dto' |
||||
|
|
||||
|
@Injectable() |
||||
|
export class AuthService { |
||||
|
constructor ( |
||||
|
private readonly usersService: UsersService, |
||||
|
private readonly mailerService: MailerService |
||||
|
) {} |
||||
|
|
||||
|
async sendConfirmedEmail (user: User) { |
||||
|
const { email, username } = user |
||||
|
await this.mailerService.sendMail({ |
||||
|
to: email, |
||||
|
subject: 'Welcome to ft_transcendence! Email Confirmed', |
||||
|
template: 'confirmed', |
||||
|
context: { |
||||
|
username, |
||||
|
email |
||||
|
} |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
async sendConfirmationEmail (user: User) { |
||||
|
user.authToken = Math.floor(10000 + Math.random() * 90000).toString() |
||||
|
this.usersService.save(user) |
||||
|
await this.mailerService.sendMail({ |
||||
|
to: user.email, |
||||
|
subject: 'Welcome to ft_transcendence! Confirm Email', |
||||
|
template: 'confirm', |
||||
|
context: { |
||||
|
username: user.username, |
||||
|
code: user.authToken |
||||
|
} |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
async verifyAccount (code: string): Promise<boolean> { |
||||
|
const user = await this.usersService.findByCode(code) |
||||
|
if (!user) { |
||||
|
throw new HttpException( |
||||
|
'Verification code has expired or not found', |
||||
|
HttpStatus.UNAUTHORIZED |
||||
|
) |
||||
|
} |
||||
|
user.authToken = '' |
||||
|
user.isVerified = true |
||||
|
await this.usersService.save(user) |
||||
|
await this.sendConfirmedEmail(user) |
||||
|
return true |
||||
|
} |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
<!DOCTYPE html> |
||||
|
<html> |
||||
|
<head> |
||||
|
<meta charset="utf-8"> |
||||
|
<meta http-equiv="x-ua-compatible" content="ie=edge"> |
||||
|
<title>Transcendence confirmation mail</title> |
||||
|
</head> |
||||
|
<body> |
||||
|
<h2>Hello {{username}}! </h2> |
||||
|
<p> Once you clicked on the next verify button, you will have access to the app</p> |
||||
|
<form action="http://localhost:3001/log/verify" method="post"> |
||||
|
<button type="submit" name="code" value={{code}}>Verify</button> |
||||
|
</form |
||||
|
</body> |
||||
|
</html> |
@ -0,0 +1,12 @@ |
|||||
|
<!DOCTYPE html> |
||||
|
<html> |
||||
|
<head> |
||||
|
<meta charset="utf-8"> |
||||
|
<meta http-equiv="x-ua-compatible" content="ie=edge"> |
||||
|
<title>Welcome to transcendence</title> |
||||
|
</head> |
||||
|
<body> |
||||
|
<h2>Hello {{username}}! </h2> |
||||
|
<p>You well verified your account for this session.</p> |
||||
|
</body> |
||||
|
</html> |
Loading…
Reference in new issue