Skip to content

Commit

Permalink
Fix username and email validation in loginUser
Browse files Browse the repository at this point in the history
and process spelling in auth middleware
  • Loading branch information
hiteshchoudhary committed Dec 13, 2023
1 parent 8974baa commit 591c8f7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,17 @@ const loginUser = asyncHandler(async (req, res) =>{
//send cookie

const {email, username, password} = req.body
console.log(email);

if (!username || !email) {
if (!username && !email) {
throw new ApiError(400, "username or email is required")
}

// Here is an alternative of above code based on logic discussed in video:
// if (!(username || email)) {
// throw new ApiError(400, "username or email is required")

// }

const user = await User.findOne({
$or: [{username}, {email}]
Expand Down
11 changes: 6 additions & 5 deletions src/middlewares/auth.middleware.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { ApiError } from "../utils/ApiError";
import { asyncHandler } from "../utils/asyncHandler";
import { ApiError } from "../utils/ApiError.js";
import { asyncHandler } from "../utils/asyncHandler.js";
import jwt from "jsonwebtoken"
import { User } from "../models/user.model";
import { User } from "../models/user.model.js";

export const verifyJWT = asyncHandler(async(req, _, next) => {
try {
const token = req.cookies?.accessToken || req.header("Authorization")?.replace("Bearer ", "")


console.log(token);
if (!token) {
throw new ApiError(401, "Unauthorized request")
}

const decodedToken = jwt.verify(token, proccess.env.ACCESS_TOKEN_SECRET)
const decodedToken = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET)

const user = await User.findById(decodedToken?._id).select("-password -refreshToken")

Expand Down

0 comments on commit 591c8f7

Please sign in to comment.