Skip to content

Commit

Permalink
signup funcitonality
Browse files Browse the repository at this point in the history
  • Loading branch information
ArslanKamchybekov committed Sep 13, 2024
1 parent 451c929 commit d2b3ae2
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 12 deletions.
38 changes: 30 additions & 8 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0",
"dotenv": "^16.4.5",
"ejs": "^3.1.10",
"mongodb": "^6.8.1",
"moongoose": "^0.0.5",
"nodemailer": "^6.9.15",
"nodemon": "^3.1.4",
"passport-jwt": "^4.0.1",
"reflect-metadata": "^0.2.0",
Expand All @@ -38,9 +40,11 @@
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/ejs": "^3.1.5",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/nodemailer": "^6.4.15",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
Expand Down
33 changes: 29 additions & 4 deletions backend/src/auth/auth.controller.ts
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);
}
}
}
54 changes: 54 additions & 0 deletions backend/src/utils/sendMail.ts
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;

0 comments on commit d2b3ae2

Please sign in to comment.