Skip to content

Commit 38888f0

Browse files
committed
fix: fix issue with eslint
1 parent 3c75a2f commit 38888f0

7 files changed

Lines changed: 31 additions & 52 deletions

File tree

src/config/passport.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import pkg from 'passport-google-oauth20';
33
const GoogleStrategy = pkg.Strategy || pkg;
44
import User from '../models/user.model.js';
55
import jwt from 'jsonwebtoken';
6-
import crypto from 'crypto';
76
import { sendPendingLinkEmail } from '../utils/email.util.js';
87

98
// helper to generate JWT (used by controller as well)
@@ -30,7 +29,8 @@ const jwtSecret = process.env.JWT_SECRET || '';
3029
if (process.env.NODE_ENV !== 'test') {
3130
// require at least 32 bytes (64 hex chars) for HMAC secrets
3231
if (typeof jwtSecret !== 'string' || jwtSecret.length < 64) {
33-
throw new Error('Weak or missing JWT_SECRET. Use a strong random secret (e.g. `node -e \"console.log(require(\\'crypto\\').randomBytes(64).toString(\\'hex\\'))\"`) and set it in environment variables.');
32+
const genCmd = `node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"`;
33+
throw new Error(`Weak or missing JWT_SECRET. Use a strong random secret (for example run: ${genCmd}) and set it in environment variables.`);
3434
}
3535
// optional: ensure it's not a default placeholder
3636
if (/replace_|your_|changeme/i.test(jwtSecret)) {

src/controllers/auth.controller.js

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ import User from '../models/user.model.js';
33
import { generateToken } from '../config/passport.config.js';
44
import crypto from 'crypto';
55

6-
// WARNING: cookie-parser middleware must be enabled in app.js for the state cookie to work
7-
// e.g. app.use(cookieParser());
8-
96
// Redirect to Google OAuth (generates a per-request state stored in an httpOnly cookie)
107
export const googleAuth = (req, res, next) => {
118
const state = crypto.randomBytes(16).toString('hex');
12-
// store state in an httpOnly cookie for CSRF protection, short lived
139
res.cookie('oauth_state', state, {
1410
httpOnly: true,
1511
secure: process.env.NODE_ENV === 'production',
@@ -37,7 +33,6 @@ export const googleCallback = (req, res, next) => {
3733
res.clearCookie('oauth_state');
3834
return res.status(403).json({ success: false, message: 'Invalid OAuth state' });
3935
}
40-
// clear cookie early
4136
res.clearCookie('oauth_state');
4237

4338
passport.authenticate('google', { session: false }, async (err, user, info) => {
@@ -69,7 +64,6 @@ export const googleCallback = (req, res, next) => {
6964
return res.redirect(`${frontend}/auth/success#token=${token}`);
7065
}
7166

72-
// return safe public user view (include email for owner)
7367
return res.json({
7468
success: true,
7569
message: 'Authentication successful',
@@ -91,59 +85,41 @@ export const googleCallback = (req, res, next) => {
9185
export const getProfile = async (req, res) => {
9286
try {
9387
const user = await User.findById(req.user.userId);
94-
9588
if (!user) {
96-
return res.status(404).json({
97-
success: false,
98-
message: 'User not found'
99-
});
89+
return res.status(404).json({ success: false, message: 'User not found' });
10090
}
101-
102-
// return public view including email because requester is the owner (authenticated)
103-
res.json({
104-
success: true,
105-
user: user.toPublic(true)
106-
});
91+
res.json({ success: true, user: user.toPublic(true) });
10792
} catch (error) {
10893
console.error('Get profile error:', error?.stack || error);
109-
res.status(500).json({
110-
success: false,
111-
message: 'Failed to get user profile'
112-
});
94+
return res.status(500).json({ success: false, message: 'Failed to get user profile' });
11395
}
11496
};
11597

11698
// Logout (invalidate tokens for this user by bumping tokenVersion)
11799
export const logout = async (req, res) => {
118100
try {
119-
// increment tokenVersion to revoke all existing tokens for this user
120101
await User.incrementTokenVersion(req.user.userId);
121-
// clear token cookie if used
122102
if (process.env.SEND_TOKEN_COOKIE === 'true') {
123103
res.clearCookie('token', { httpOnly: true, sameSite: 'lax', secure: process.env.NODE_ENV === 'production' });
124104
}
125-
return res.json({
126-
success: true,
127-
message: 'Logout successful. Tokens invalidated on server.'
128-
});
105+
return res.json({ success: true, message: 'Logout successful. Tokens invalidated on server.' });
129106
} catch (error) {
130-
console.error('Logout error:', error);
107+
console.error('Logout error:', error?.stack || error);
131108
return res.status(500).json({ success: false, message: 'Failed to logout' });
132109
}
133110
};
134111

135-
// Refresh token: ensure tokenVersion still matches before issuing new token matches before issuing new token
136-
export const refreshToken = async (req, res) => {const refreshToken = async (req, res) => {
137-
try {ry {
138-
const user = await User.findById(req.user.userId).select('-__v'); const user = await User.findById(req.user.userId).select('-__v');
139-
if (!user) { if (!user) {
140-
return res.status(404).json({ success: false, message: 'User not found' });not found' });
112+
// Refresh token: issue a new JWT if the authenticated user's tokenVersion matches current
113+
export const refreshToken = async (req, res) => {
114+
try {
115+
const user = await User.findById(req.user.userId).select('-__v');
116+
if (!user) {
117+
return res.status(404).json({ success: false, message: 'User not found' });
141118
}
142-
// issue new JWT reflecting current tokenVersionissue new JWT reflecting current tokenVersion
143119
const token = generateToken(user);
144-
res.json({ success: true, message: 'Token refreshed successfully', token });d successfully', token });
120+
return res.json({ success: true, message: 'Token refreshed successfully', token });
145121
} catch (error) {
146-
console.error('Refresh token error:', error?.stack || error);ack || error);
122+
console.error('Refresh token error:', error?.stack || error);
147123
return res.status(500).json({ success: false, message: 'Failed to refresh token' });
148124
}
149125
};

src/controllers/cart.controller.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const getCart = async (req, res, next) => {
2626
data: cart ? cart.items : [],
2727
});
2828
} catch (err) {
29+
console.error('Cart controller error (getCart):', err);
2930
next(new HttpException(500, 'Failed to fetch cart'));
3031
}
3132
}
@@ -66,6 +67,7 @@ export const addToCart = async (req, res, next) => {
6667

6768
return res.status(200).json({ success: true, data: cart.items });
6869
} catch (err) {
70+
console.error('Cart controller error (addToCart):', err);
6971
next(new HttpException(500, 'Failed to add to cart'));
7072
}
7173
}
@@ -104,6 +106,7 @@ export const updateCartItem = async (req, res, next) => {
104106

105107
return res.status(200).json({ success: true, data: cart.items });
106108
} catch (err) {
109+
console.error('Cart controller error (updateCartItem):', err);
107110
next(new HttpException(500, 'Failed to update cart item'));
108111
}
109112
}
@@ -131,6 +134,7 @@ export const removeFromCart = async (req, res, next) => {
131134

132135
return res.status(200).json({ success: true, data: cart.items });
133136
} catch (err) {
137+
console.error('Cart controller error (removeFromCart):', err);
134138
next(new HttpException(500, 'Failed to remove from cart'));
135139
}
136140
}

src/controllers/collection.controller.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const getCollectionProducts = async (req, res, next) => {
2727

2828
return res.status(200).json({ collections: name, products });
2929
} catch (err) {
30+
console.error('Collection controller error (getCollectionProducts):', err);
3031
next(new HttpException(500, "Failed to fetch collections products"));
3132
}
3233
}

src/middleware/auth.middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const optionalAuth = async (req, res, next) => {
9292
return next();
9393
}
9494
req.user = decoded;
95-
} catch (err) {
95+
} catch {
9696
// invalid or expired token -> treat as unauthenticated
9797
req.user = null;
9898
}

src/middleware/error-handler.middleware.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
* @param {Response} res - Express response object.
99
* @param {Function} _next - Express next middleware function (unused).
1010
*/
11-
export default function errorHandler(err, req, res, next) {
12-
13-
console.error("Error:", err.message);
11+
export default function errorHandler(err, _req, res) {
12+
// Log full error server-side (stack if available)
13+
console.error("Error:", err.stack || err);
1414

1515
const status = err?.statusCode ? err.statusCode : 500;
1616
const response = {
@@ -19,7 +19,7 @@ export default function errorHandler(err, req, res, next) {
1919
};
2020

2121
// stack trace (only in development)
22-
if(process.env.NODE_ENV == 'development') response.stack = err.stack;
22+
if (process.env.NODE_ENV === 'development') response.stack = err.stack;
2323

2424
res.status(status).json(response);
2525
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import express from 'express'
2-
3-
const notFound = (req,res,next) =>{
4-
res.status(404).json({
5-
success: false,
6-
message: "Route not found"
7-
})
8-
}
1+
const notFound = (req, res) => {
2+
res.status(404).json({
3+
success: false,
4+
message: "Route not found"
5+
});
6+
};
97

108
export default notFound;

0 commit comments

Comments
 (0)