diff --git a/services/Auth/src/controllers/auth.controller.js b/services/Auth/src/controllers/auth.controller.js index d87a358..f2330d6 100644 --- a/services/Auth/src/controllers/auth.controller.js +++ b/services/Auth/src/controllers/auth.controller.js @@ -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 }); @@ -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({ @@ -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); } } @@ -68,18 +66,15 @@ 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); @@ -87,8 +82,7 @@ async function signin(req, res, next) { // if the password not matched; if (!isMatch) { - res.json(errorHadnler(401, "Invalid password")); - return + return next(errorHandler(401, "Invalid password")); } // generating jwt token; @@ -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); } } diff --git a/services/Auth/src/main.js b/services/Auth/src/main.js index c398a1d..4e7e2ce 100644 --- a/services/Auth/src/main.js +++ b/services/Auth/src/main.js @@ -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; diff --git a/services/Auth/src/middleware/auth.middleware.js b/services/Auth/src/middleware/auth.middleware.js index b89162f..aca3865 100644 --- a/services/Auth/src/middleware/auth.middleware.js +++ b/services/Auth/src/middleware/auth.middleware.js @@ -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 }); diff --git a/services/Auth/src/middleware/validate.schema.js b/services/Auth/src/middleware/validate.schema.js index 5d98c70..7e8f81c 100644 --- a/services/Auth/src/middleware/validate.schema.js +++ b/services/Auth/src/middleware/validate.schema.js @@ -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)); } } diff --git a/services/Auth/src/utils/error.js b/services/Auth/src/utils/error.js index 3d45bbb..db5c1e9 100644 --- a/services/Auth/src/utils/error.js +++ b/services/Auth/src/utils/error.js @@ -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 };