Skip to content

Commit

Permalink
Auth user issues fixed and user services improved
Browse files Browse the repository at this point in the history
  • Loading branch information
jareerzeenam committed Aug 15, 2024
1 parent dc6dfdd commit d496efc
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions services/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// registerUser
const { User } = require('../models/User.model');
const { ForbiddenError } = require('apollo-server-errors');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
Expand Down Expand Up @@ -138,10 +137,12 @@ const resetPassword = async (payload) => {
const token = payload.token;
const newPassword = payload.newPassword;

const user = await User.findOne({ email, resetToken: token });
const userRepo = new UserRepository();
const user = await userRepo.findByEmail(email);

if (
!user ||
user.resetToken !== token ||
moment(user.resetTokenExpires) < moment().subtract(1, 'hour')
) {
throw new Error('Invalid or expired token');
Expand Down Expand Up @@ -169,14 +170,15 @@ const resetPassword = async (payload) => {

// Function to generate Reset Token JWT
async function generateResetToken(email) {
const user = await User.findOne({ email });
const userRepo = new UserRepository();
const user = await userRepo.findByEmail(email);

if (!user) {
throw new Error('User not found');
}

if (user.resetTokenExpires) {
const resetTokenExpires = moment(tokenExpires);
const resetTokenExpires = moment(user.resetTokenExpires);
const tokenHasExpired = resetTokenExpires.isBefore(
moment().subtract(1, 'hour')
);
Expand Down

0 comments on commit d496efc

Please sign in to comment.