-
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
1 parent
451c929
commit d2b3ae2
Showing
4 changed files
with
117 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 +1,37 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { Controller, Post, Req, Res, Next } from '@nestjs/common'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { AuthService } from './auth.service'; | ||
import { User } from '../user/user.schema' | ||
import sendEmail from '../utils/sendMail'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private readonly authService: AuthService) {} | ||
|
||
@Get() | ||
get(): string { | ||
return "Auth is running!"; | ||
@Post('signup') | ||
async signup(@Req() req: Request, @Res() res: Response, @Next() next: NextFunction){ | ||
const { email, password } = req.body; | ||
|
||
const user = await User.findOne({ email }); | ||
if(user) return res.status(400).json({ message: 'User already exists' }); | ||
|
||
const newUser = { email, password }; | ||
|
||
const activationToken = await this.generateActivationToken(newUser); | ||
const activationUrl = `${process.env.CLIENT_URL}/auth/activate/${activationToken}`; | ||
|
||
try { | ||
await sendEmail({ | ||
email: newUser.email, | ||
subject: 'Activate your account', | ||
template: 'activation.ejs', | ||
data: { activationUrl } | ||
}); | ||
|
||
res.status(201).json({ message: 'User created. Check your email to activate your account' }); | ||
} catch (error: any) { | ||
console.error('Error sending email:', error); | ||
next(error); | ||
} | ||
} | ||
} |
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,54 @@ | ||
import * as nodeMailer from 'nodemailer'; | ||
import { Transporter } from 'nodemailer'; | ||
import * as ejs from 'ejs'; | ||
import * as path from 'path'; | ||
require('dotenv').config(); | ||
|
||
interface EmailOptions { | ||
email: string; | ||
subject: string; | ||
template: string; | ||
data: { [key: string]: any }; | ||
} | ||
|
||
const renderFileAsync = (filePath: string, data: any): Promise<string> => { | ||
return new Promise((resolve, reject) => { | ||
ejs.renderFile(filePath, data, (err, str) => { | ||
if (err) return reject(err); | ||
resolve(str); | ||
}); | ||
}); | ||
}; | ||
|
||
const sendMail = async (options: EmailOptions): Promise<void> => { | ||
const transporter: Transporter = nodeMailer.createTransport({ | ||
host: process.env.SMTP_HOST, | ||
port: parseInt(process.env.SMTP_PORT || '587'), | ||
service: process.env.SMTP_SERVICE, | ||
auth: { | ||
user: process.env.SMTP_MAIL, | ||
pass: process.env.SMTP_PASSWORD, | ||
}, | ||
}); | ||
|
||
const { email, subject, template, data } = options; | ||
|
||
try { | ||
const templatePath = path.join(__dirname, '../mails', template); | ||
const html: string = await renderFileAsync(templatePath, data); | ||
|
||
const mailOptions = { | ||
from: process.env.SMTP_MAIL, | ||
to: email, | ||
subject, | ||
html, | ||
}; | ||
|
||
await transporter.sendMail(mailOptions); | ||
} catch (error) { | ||
console.error('Error sending email:', error); | ||
throw error; | ||
} | ||
}; | ||
|
||
export default sendMail; |