-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/advanced-computer-lab-2023/…
- Loading branch information
Showing
10 changed files
with
186 additions
and
29 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
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import ResetPasswordService from "../service/reset-password-service.js"; | ||
import UserService from "../service/user-service.js"; | ||
import nodemailer from 'nodemailer'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { BAD_REQUEST_CODE_400 } from "../utils/Constants.js"; | ||
export const resetPassword = (app) => { | ||
|
||
const resetPassword = new ResetPasswordService(); | ||
const user = new UserService(); | ||
|
||
const generateUniqueToken = () => { | ||
return uuidv4(); | ||
} | ||
|
||
function generateRandom6DigitNumber() { | ||
const min = 100000; // Minimum 6-digit number | ||
const max = 999999; // Maximum 6-digit number | ||
|
||
// Generate a random number in the range [min, max] | ||
const randomNumber = Math.floor(Math.random() * (max - min + 1)) + min; | ||
|
||
return randomNumber; | ||
} | ||
|
||
app.post('/reset-password', async (req, res) => { | ||
const { email } = req.body; | ||
const OTP = generateRandom6DigitNumber(); | ||
|
||
try { | ||
const userRecord = await user.findUserByEmail(email); | ||
console.log(userRecord); | ||
if(!userRecord ){ | ||
throw new Error('invalid user'); | ||
} else if(!userRecord.email){ | ||
throw new Error('invalid Email'); | ||
} | ||
|
||
// TODO: access the reset password database | ||
await resetPassword.removeRecordByEmail(email); | ||
await resetPassword.addRecord(email, OTP); | ||
|
||
const transporter = nodemailer.createTransport({ | ||
service: 'Gmail', | ||
host: "setup.gmail.com", | ||
port: 587, | ||
secure: false, | ||
auth: { | ||
user: process.env.RESETEMAIL, | ||
pass: process.env.RESETPASSWORD, | ||
}, | ||
}); | ||
|
||
|
||
const mailOptions = { | ||
from: { | ||
name:'acl lab', | ||
address:`${process.env.RESETEMAIL}`}, | ||
to: [email], | ||
subject: 'Password Reset', | ||
text: `Click this link to reset your password: http://localhost:3000/reset-password/${OTP}`, | ||
}; | ||
|
||
transporter.sendMail(mailOptions, (error, info) => { | ||
if (error) { | ||
console.log(error); | ||
res.status(500).json({ message: 'Failed to send email' }); | ||
} else { | ||
console.log(`Email sent: ${info.response}`); | ||
res.json({ message: 'Email sent' }); | ||
} | ||
}); | ||
} catch(err){ | ||
console.log(err); | ||
res.status(BAD_REQUEST_CODE_400).send({ errMessage: err.message}); | ||
} | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const resetSchema = mongoose.Schema({ | ||
email: { | ||
type:String, | ||
required:true | ||
}, | ||
OTP: { | ||
type:String, | ||
required:true | ||
}, | ||
resetTokenExpiration: { | ||
type:Date, | ||
required:true, | ||
default: new Date((new Date()).getTime() + 24 * 60 * 60 * 1000) | ||
}, | ||
}); | ||
|
||
const ResetPassword = mongoose.model('ResetPassword', resetSchema); | ||
|
||
export default ResetPassword; |
20 changes: 20 additions & 0 deletions
20
authentication/src/database/repository/reset-password-repository.js
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,20 @@ | ||
import ResetPassword from "../models/ResetPassword.js"; | ||
|
||
class ResetPasswordRepository{ | ||
async getRecordByEmail(email){ | ||
const user = await ResetPassword.findOne({email: email}); | ||
return user; | ||
} | ||
|
||
async removeRecordByEmail(email){ | ||
const user = await ResetPassword.deleteOne({email: email}); | ||
return user; | ||
} | ||
|
||
async addRecord(email, OTP){ | ||
const record = new ResetPassword({email, OTP}); | ||
await record.save(); | ||
} | ||
} | ||
|
||
export default ResetPasswordRepository; |
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,26 @@ | ||
import ResetPasswordRepository from "../database/repository/reset-password-repository.js"; | ||
|
||
class ResetPasswordService { | ||
constructor() { | ||
this.repository = new ResetPasswordRepository(); | ||
|
||
} | ||
|
||
async getRecordByEmail(email){ | ||
const user = await this.repository.getRecordByEmail(email); | ||
return user; | ||
} | ||
|
||
async removeRecordByEmail(email){ | ||
const user = await this.repository.removeRecordByEmail(email); | ||
return user; | ||
} | ||
|
||
async addRecord(email, OTP){ | ||
const user = await this.repository.addRecord(email, OTP); | ||
return user; | ||
} | ||
|
||
} | ||
|
||
export default ResetPasswordService; |
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