Skip to content

Commit

Permalink
Merge pull request #263 from CDCNaman/main
Browse files Browse the repository at this point in the history
FIX: #262
  • Loading branch information
thecuriousteam authored Jun 7, 2024
2 parents fe8889b + 46037f0 commit 4b35706
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
25 changes: 9 additions & 16 deletions services/Auth/src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
const bcrypt = require("bcryptjs");
const User = require("../models/user.model");
const { errorHadnler } = require("../utils/error");
const { errorHandler } = require("../utils/error");
const cookie = require('cookie'); // Import the 'cookie' library

async function signup(req, res) {
async function signup(req, res, next) {
try {
const { name, email, password } = req.body;

//Check if input is as expected or not
if (!name || !password || !email) {
res.json (errorHadnler(401,'All fields must be filled'))
return
return next(errorHandler(401,'All fields must be filled'))
}

let user = await User.findOne({ email });
Expand All @@ -20,8 +19,7 @@ async function signup(req, res) {
// if user already exists;

if (user) {
res.json(errorHadnler(400, "user already exists"));
return
return next(errorHandler(400, "user already exists"));
}

user = new User({
Expand Down Expand Up @@ -58,7 +56,7 @@ async function signup(req, res) {

return res.status(200).json({ ...userResponse, token });
} catch (error) {
res.status(500).json({ ...error });
next(error);
}
}

Expand All @@ -68,27 +66,23 @@ async function signin(req, res, next) {

//Check if input is as expected or not
if (!email || !password) {
res.json (errorHadnler(401,'All fields must be filled'))
return
return next(errorHandler(401,'All fields must be filled'))
}

let user = await User.findOne({ email });
// console.log(user);

// checking whether user exists or not;

if (!user) {
res.json(errorHadnler(404, "User does not exists"));
return
return next(errorHandler(404, "User does not exists"));
}

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

// if the password not matched;

if (!isMatch) {
res.json(errorHadnler(401, "Invalid password"));
return
return next(errorHandler(401, "Invalid password"));
}

// generating jwt token;
Expand All @@ -115,8 +109,7 @@ async function signin(req, res, next) {

return res.status(200).json({ token, userResposne });
} catch (error) {
console.log(error);
res.status(500).json({ ...error });
next(error);
}
}

Expand Down
11 changes: 11 additions & 0 deletions services/Auth/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ async function server() {
app.listen(config.PORT, () => {
console.log(`server is running at: http://localhost:${config.PORT}`);
});
app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
const message = err.message || "Internal Server Error";
const stack = config.NODE_ENV === "development" ? err.stack : undefined;
return res.status(statusCode).json({
success: false,
statusCode,
message,
stack
});
});
}

module.exports = server;
2 changes: 1 addition & 1 deletion services/Auth/src/middleware/auth.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const userauth = async (req, res, next) => {

const user = await User.findOne({ _id: id });

if (!user) return res.status(401, "please login again");
if (!user) return next(errorHadnler(401, "please login again"));

res.cookie("token", token, { httpOnly: true, maxAge: 24 * 60 * 60 * 1000 });

Expand Down
7 changes: 3 additions & 4 deletions services/Auth/src/middleware/validate.schema.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const { errorHandler } = require('../utils/error');

const validate = (schema) => async (req, res, next) => {
try {
const parsedBody = await schema.parseAsync(req.body);
req.body = parsedBody;
next();
} catch (err) {
console.log(err);
const yourerror = err.errors[0].message;
res.status(400).json({
msg: yourerror,
});
next(errorHandler(400, yourerror, err));
}
}

Expand Down
7 changes: 5 additions & 2 deletions services/Auth/src/utils/error.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const errorHadnler = (statusCode, message) => {
const errorHandler = (statusCode, message, err = null) => {
const error = new Error();
error.statusCode = statusCode;
error.message = message;
if (err) {
error.stack = err.stack;
}
return error;
};

module.exports = { errorHadnler };
module.exports = { errorHandler };

0 comments on commit 4b35706

Please sign in to comment.