I'm junior software developer which main task is develop a auth nodejs app. When I took the part of reset_password functionality, return me error message that I'm not able to solve.
This is my env file:
TOKEN_SECRET: T0K€N
RESET_PASSWORD_TOKEN: T0K€N
This is my user's model:
../models/user.js
const mongoose = require('mongoose');
const userSchema = mongoose.Schema({
name: {
type: String,
required: true,
min: 6,
max: 255
},
email: {
type: String,
required: true,
min: 6,
max: 1024
},
password: {
type: String,
required: true,
minlength: 6
},
subscribe: {
type: Boolean,
default: false
},
date: {
type: Date,
default: Date.now
},
role: {
type: String,
required: false,
minlength: 4
},
address: {
type: String,
required: false,
minlength: 4,
defaultValue: ""
},
nonce: {
type: String,
required: false,
minlength: 4,
defaultValue: ""
},
})
module.exports = mongoose.model('User', userSchema);
This is my token's model:
../models/token.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema({
token: {
type: String,
required: true,
min: 6
},
id_user:{
type: String,
required: true,
min: 1
}
})
module.exports = mongoose.model('expired_tokens', Schema);
And finally, my user's controller:
const Joi = require('@hapi/joi');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const nodemailer = require("nodemailer");
//models
const User = require('../models/user');
const expired_token = require('../models/expired_token');
//nodemailer
const sendMail = require('../nodemailer');
ResetPassword: async (req, res) => {
const user = await User.findOne({ email: req.body.email });
if (!user) {
return res.status(400).json({
data: "",
message: "User with this email doesn't exist",
status: "error"
})
}
const token = jwt.sign({ id: user._id, email: user.email }, process.env.RESET_PASSWORD_TOKEN );
const data = {
from: 'noreply@hello.com',
to: email,
subject: 'Recuperacion de contraseña',
template: "reset password",
context: {
url: 'http://localhosy:3005/auth/reset_password?token=' + token,
name: user.fullName.split('').split('')[0]
}
}
const link = `${process.env.BASE_URL}/reset_password/${user._id}/${token.token} `;
await sendEmail(user.email, "Password Reset", link);
},
//routes
router.route('/reset_password')
.post(verifyToken, controller.ResetPassword);
Nevertheless, when I tested in postman, return me the error message:
Probably, the error has to do with the enviroment variable in env. file. So, I'd be really grateful if someone could bring me his help or advice in order to solve this situation.
Thanks and have a nice day!
