Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: #262 #263

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };