Skip to content

Commit

Permalink
send verification email after signing up
Browse files Browse the repository at this point in the history
  • Loading branch information
mutsinziisaac committed May 22, 2024
1 parent f5fcbf5 commit b3b60a6
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 54 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
PORT =
JWT_SECRET =
DB_URL =

DEV_DB_NAME =
DEV_DB_USER =
Expand All @@ -14,4 +15,7 @@ TEST_DB_HOST =
PRO_DB_NAME =
PRO_DB_USER =
PRO_DB_PASS =
PRO_DB_HOST =
PRO_DB_HOST =

EMAIL_USER =
EMAIL_PASS =
19 changes: 19 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"express": "^4.19.2",
"joi": "^17.13.1",
"jsonwebtoken": "^9.0.2",
"nodemailer": "^6.9.13",
"nodemon": "^3.1.0",
"pg": "^8.11.5",
"pg-hstore": "^2.3.4",
Expand All @@ -35,6 +36,7 @@
"@types/jsonwebtoken": "^9.0.6",
"@types/morgan": "^1.9.9",
"@types/node": "^20.12.11",
"@types/nodemailer": "^6.4.15",
"@types/pg": "^8.11.6",
"@types/sequelize": "^4.28.20",
"@types/supertest": "^6.0.2",
Expand Down
53 changes: 27 additions & 26 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,37 @@ import { UserSignupAttributes } from "../types/user.types";
import { UserService } from "../services/user.services";
import { hashPassword } from "../utils/password.utils";
import { generateToken } from "../utils/tokenGenerator.utils";
import { sendVerificationEmail } from "../services/email.services";

export const userSignup = async (req: Request, res: Response) => {
try{
try {
const hashedpassword: any = await hashPassword(req.body.password);

const user: UserSignupAttributes = {
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: hashedpassword,
role: req.body.role,
phone: req.body.phone,
};
const email=req.body.email
if(email==undefined){

}
const user: UserSignupAttributes = {
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: hashedpassword,
role: req.body.role,
phone: req.body.phone,
};
const email = req.body.email;
if (email == undefined) {
}

const createdUser = await UserService.register(user);
const token = await generateToken(createdUser);
const createdUser = await UserService.register(user);
const token = await generateToken(createdUser);
sendVerificationEmail(user.email);

return res.status(200).json({
status: "success",
message: "User created successfully",
token: token,
data: {
user: createdUser,
},
});
}catch(error){
console.log(error,"Error in creating account");
return res.status(200).json({
status: "success",
message: "User created successfully",
token: token,
data: {
user: createdUser,
},
});
} catch (error) {
console.log(error, "Error in creating account");
}
};
};
14 changes: 7 additions & 7 deletions src/database/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ dotenv.config();
module.exports = {
development: {
url: `${process.env.DB_URL}`,
dialect: 'postgres',
dialect: "postgres",
dialectModule: pg,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false
}
}
rejectUnauthorized: false,
},
},
},
test: {
url: `postgres://${process.env.TEST_DB_USER}:${process.env.TEST_DB_PASS}@${process.env.TEST_DB_HOST}/${process.env.TEST_DB_NAME}`,
dialect: 'postgres',
dialect: "postgres",
dialectModule: pg,
},
production: {
url: `postgres://${process.env.PRO_DB_USER}:${process.env.PRO_DB_PASS}@${process.env.PRO_DB_HOST}/${process.env.PRO_DB_NAME}`,
dialect: 'postgres',
dialect: "postgres",
dialectModule: pg,
},
};
};
14 changes: 7 additions & 7 deletions src/database/config/sequelize.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize';
import dotenv from 'dotenv';
import pg from 'pg';
import { Sequelize } from "sequelize";
import dotenv from "dotenv";
import pg from "pg";

dotenv.config();

Expand All @@ -11,12 +11,12 @@ if (!databaseUrl) {
}

export const sequelize = new Sequelize(databaseUrl, {
dialect: 'postgres',
dialect: "postgres",
dialectModule: pg,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false
}
}
rejectUnauthorized: false,
},
},
});
31 changes: 31 additions & 0 deletions src/services/email.services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import nodemailer from "nodemailer";
import dotenv from "dotenv";
dotenv.config();

const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});

export function sendVerificationEmail(email: any) {
const verificationLink = `http://yourdomain.com/verify-email?token=token}`;

const mailOptions = {
from: process.env.EMAIL_USER,
to: email,
subject: "Email Verification",
text: `Please verify your email by clicking on the following link: ${verificationLink}`,
html: `<p>Please verify your email by clicking on the following link:</p><a href="${verificationLink}">Verify Email</a>`,
};

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error("Error sending email:", error);
} else {
console.log("Email sent:", info.response);
}
});
}
29 changes: 16 additions & 13 deletions src/utils/tokenGenerator.utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import jwt from 'jsonwebtoken';
import { UserAttributes } from '../types/user.types';
import dotenv from 'dotenv';
import jwt from "jsonwebtoken";
import { UserAttributes } from "../types/user.types";
import dotenv from "dotenv";

dotenv.config();

const generateToken = async (user: UserAttributes) => {
return jwt.sign({
role: user.role,
email: user.email,
id: user.id
}, process.env.JWT_SECRET || "", {
expiresIn: '24h'
});
}

export { generateToken };
return jwt.sign(
{
role: user.role,
email: user.email,
id: user.id,
},
process.env.JWT_SECRET || "",
{
expiresIn: "24h",
}
);
};
export { generateToken };

0 comments on commit b3b60a6

Please sign in to comment.