Skip to content

Commit

Permalink
Merge pull request #154 from Frnn4268/Frnn
Browse files Browse the repository at this point in the history
Adding new Redis content inside of authController.js
  • Loading branch information
Frnn4268 authored Nov 7, 2024
2 parents bf23ce4 + 99c3bff commit 0895fe2
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions backend/src/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,42 @@ exports.signup = async (req, res, next) => {
};

// Login user
exports.login = async(req, res, next) => {
exports.login = async (req, res, next) => {
try {
const {email, password} = req.body
const { email, password } = req.body;

const user = await User.findOne({ email })
const user = await User.findOne({ email });

if(!user) return next(new createError('Usuario no encontrado', 404))
if (!user) return next(new createError('Usuario no encontrado', 404));

if(user.active === false) return next(new createError('Usuario sin autorización', 403))
if (user.active === false) return next(new createError('Usuario sin autorización', 403));

const isPasswordValid = await bcrypt.compare(password, user.password)
const isPasswordValid = await bcrypt.compare(password, user.password);

if (!isPasswordValid) {
return next(new createError('Contraseña o email incorrecto', 401))
return next(new createError('Contraseña o email incorrecto', 401));
}

// Assing JWT (json web token) to user
const token = jwt.sign({_id: user._id}, process.env.SECRET_KEY, {
expiresIn: '90d',
})

// Store token in Redis
await redisClient.set(user._id.toString(), token, {
EX: 90 * 24 * 60 * 60 // 90 days in seconds
});
// Check if token exists in Redis
const cachedToken = await redisClient.get(user._id.toString());

let token;
if (cachedToken) {
token = cachedToken;
} else {
// Assing JWT (json web token) to user
token = jwt.sign({ _id: user._id }, process.env.SECRET_KEY, {
expiresIn: '90d',
});

// Store token in Redis
await redisClient.set(user._id.toString(), token, {
EX: 90 * 24 * 60 * 60 // 90 days in seconds
});
}

res.status(200).json({
status: 'succes',
status: 'success',
token,
message: 'Inicio de sesión exitoso',
user: {
Expand All @@ -104,9 +112,9 @@ exports.login = async(req, res, next) => {
role: user.role,
active: user.active
}
})
});

} catch (error) {
next(error)
next(error);
}
}
};

0 comments on commit 0895fe2

Please sign in to comment.