Fix file paths and add password reset functionality
This commit is contained in:
@ -2,7 +2,7 @@ const { tokenModel } = require("../database/model/tokenModel.js");
|
||||
const { userModel } = require("../database/model/userModel");
|
||||
const { hash, compareHash } = require("./bcrypt.js");
|
||||
const { generateUUID } = require("./generateUUID.js");
|
||||
const { isValid } = require("./isValid");
|
||||
const { isValid , resetIsValid } = require("./isValid");
|
||||
|
||||
async function getTokenByToken(token) {
|
||||
const splitAuthToken = token.split("-");
|
||||
@ -53,7 +53,7 @@ async function addToken(userId, permission, isKey ,expiry) {
|
||||
async function addPasswordResetToken(data , token){
|
||||
let hashtoken = await hash(token);
|
||||
let currentDate = new Date();
|
||||
let tokenToLive = new Date(currentDate.getTime() + 15 * 60000);
|
||||
let tokenToLive = new Date(currentDate.getTime() + 5 * 60000);
|
||||
|
||||
let tokenRes = await tokenModel.create({
|
||||
userid: data.id,
|
||||
@ -62,7 +62,7 @@ async function addPasswordResetToken(data , token){
|
||||
isKey: "isNotKey",
|
||||
expiration: tokenToLive,
|
||||
});
|
||||
return true;
|
||||
return tokenRes.id
|
||||
}
|
||||
|
||||
async function checkToken(id) {
|
||||
@ -77,6 +77,31 @@ async function checkToken(id) {
|
||||
return tokenRes;
|
||||
}
|
||||
|
||||
async function checkTokenByrowID(token) {
|
||||
if (!token) return false;
|
||||
//split
|
||||
const splitAuthToken = token.split("-");
|
||||
const rowid = splitAuthToken[0];
|
||||
const suppliedToken = splitAuthToken.slice(1).join("-");
|
||||
|
||||
let tokenRes = await tokenModel.findByPk(rowid);
|
||||
//console.log(tokenRes);
|
||||
|
||||
if (!tokenRes) return false;
|
||||
|
||||
if (!compareHash(suppliedToken, tokenRes.token)) return false;
|
||||
|
||||
|
||||
module.exports = { addToken, getTokenByToken , checkToken , addPasswordResetToken};
|
||||
//pass tokemRes.expiration to isValid
|
||||
if (!isValid(tokenRes.expiration)) {
|
||||
//add boolean to token table
|
||||
tokenRes.destroy();
|
||||
return false;
|
||||
}
|
||||
|
||||
return tokenRes;
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = { addToken, getTokenByToken , checkToken , addPasswordResetToken , checkTokenByrowID};
|
||||
|
@ -11,4 +11,16 @@ function isValid(time) {
|
||||
|
||||
}
|
||||
|
||||
module.exports = { isValid };
|
||||
//5 minutes
|
||||
function resetIsValid(time) {
|
||||
if (
|
||||
Math.floor(new Date(time).getTime() / 1000) <
|
||||
Math.floor(new Date().getTime() / 1000)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
module.exports = { isValid , resetIsValid };
|
||||
|
@ -84,7 +84,7 @@ async function sendResetPasswordEmail(email, resetToken) {
|
||||
subject: "Reset Password",
|
||||
html: `
|
||||
<h1>Reset Password</h1>
|
||||
<p><strong>Reset Password Link:</strong> <a href="localhost/api/v0/auth/resetpassword/${resetToken}">Reset Password Link </p>
|
||||
<p><strong>Reset Password Link:</strong> <a href="localhost/resetpassword/${resetToken}">Reset Password Link </p>
|
||||
<p><strong>From:</strong> Eco Saver</p>
|
||||
<p>Kindly click on the link to reset your password!</p>
|
||||
<p>Regards,</p>
|
||||
|
@ -163,6 +163,23 @@ async function checkEmailDetails(email) {
|
||||
|
||||
}
|
||||
|
||||
async function resetPass(userid , data ){
|
||||
let hashed = await hash(data.password);
|
||||
let updateUser = await userModel.update(
|
||||
{
|
||||
password: hashed,
|
||||
},
|
||||
{
|
||||
where: {
|
||||
id: userid,
|
||||
},
|
||||
}
|
||||
);
|
||||
if (!updateUser) return false;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
getUserByID,
|
||||
@ -171,5 +188,7 @@ module.exports = {
|
||||
loginUser,
|
||||
updateProfile,
|
||||
checkEmail,
|
||||
checkEmailDetails
|
||||
checkEmailDetails,
|
||||
resetPass,
|
||||
|
||||
};
|
Reference in New Issue
Block a user