Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
praptisharma28 committed Feb 1, 2024
1 parent 5a59e0c commit f4434ed
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
65 changes: 39 additions & 26 deletions Chatgpt_Clone/controllers/authController.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,75 @@
const errorHandler = require("../middelwares/errorMiddleware");
const errorHandler = require("../middlewares/errorMiddleware");
const userModel = require("../models/userModel");
const errorResponse = require("../utils/errroResponse");
const errorResponse = require("../utils/errorResponse");

// JWT TOKEN
exports.sendToken = (user, statusCode, res) => {
const sendToken = (user, statusCode, res) => {
const token = user.getSignedToken(res);
res.status(statusCode).json({
success: true,
token,
});
};

//REGISTER
exports.registerContoller = async (req, res, next) => {
// REGISTER
exports.registerController = async (req, res, next) => {
try {
const { username, email, password } = req.body;
//exisitng user
const exisitingEmail = await userModel.findOne({ email });
if (exisitingEmail) {
return next(new errorResponse("Email is already register", 500));
// Check if email already exists
const existingEmail = await userModel.findOne({ email });

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
if (existingEmail) {
return next(new errorResponse("Email is already registered", 400));
}
// Create new user
const user = await userModel.create({ username, email, password });
this.sendToken(user, 201, res);
sendToken(user, 201, res);
} catch (error) {
console.log(error);
console.error(error);
next(error);
}
};

//LOGIN
// LOGIN
exports.loginController = async (req, res, next) => {
try {
const { email, password } = req.body;
//validation
// Validate email and password
if (!email || !password) {
return next(new errorResponse("Please provide email or password"));
return next(new errorResponse("Please provide email and password", 400));
}
// Find user by email
const user = await userModel.findOne({ email });

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
if (!user) {
return next(new errorResponse("Invalid Creditial", 401));
return next(new errorResponse("Invalid credentials", 401));
}
// Check if password matches
const isMatch = await user.matchPassword(password);
if (!isMatch) {
return next(new errorResponse("Invalid Creditial", 401));
return next(new errorResponse("Invalid credentials", 401));
}
//res
this.sendToken(user, 200, res);
// Send token
sendToken(user, 200, res);
} catch (error) {
console.log(error);
console.error(error);
next(error);
}
};

//LOGOUT
exports.logoutController = async (req, res) => {
res.clearCookie("refreshToken");
return res.status(200).json({
success: true,
message: "Logout Succesfully",
});
// LOGOUT
exports.logoutController = async (req, res, next) => {
try {
// Clear refresh token
res.clearCookie("refreshToken");
// Send success response
res.status(200).json({
success: true,
message: "Logout successfully",
});
} catch (error) {
console.error(error);
next(error);
}
};

// Apply error handler middleware
exports.errorHandler = errorHandler;
22 changes: 10 additions & 12 deletions Chatgpt_Clone/routes/authRoutes.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
const express = require("express");
const {
registerContoller,
loginController,
logoutController,
} = require("../controllers/authController");
const { registerController, loginController, logoutController, errorHandler } = require("../controllers/authController");

//router object
// Create router object
const router = express.Router();

//routes
// REGISTER
router.post("/register", registerContoller);
// Register route
router.post("/register", registerController);

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a database access
, but is not rate-limited.

//LOGIN
// Login route
router.post("/login", loginController);

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a database access
, but is not rate-limited.

//LOGOUT
router.post("/logout", logoutController);
// Logout route
router.get("/logout", logoutController);

// Error handler middleware
router.use(errorHandler);

module.exports = router;

0 comments on commit f4434ed

Please sign in to comment.