Skip to content
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
26 changes: 20 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,29 @@ const allowedOrigins = Array.from(
app.use(
cors({
origin: function (origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
// Allow requests with no origin (like mobile apps or curl requests)
if (!origin) {
return callback(null, true);
}

// Check if origin is in allowed list
if (allowedOrigins.includes(origin)) {
return callback(null, true);
}

// For development, allow all localhost origins
if (process.env.NODE_ENV !== 'production' && origin.includes('localhost')) {
return callback(null, true);
}

// Log blocked origins for debugging
console.log('Blocked origin:', origin);
callback(new Error('Not allowed by CORS'));
},
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
allowedHeaders: ['Content-Type', 'Authorization', 'Cookie']
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'Cookie', 'X-Requested-With'],
exposedHeaders: ['Set-Cookie']
})
);

Expand Down
2 changes: 2 additions & 0 deletions src/controllers/course.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,7 @@ export const getTopCourses = catchAsync(async (req: Request, res: Response, next
publisher: course.authorId,
category: course.category,
rating: course.rating,
ratingCount: course.reviews ? course.reviews.length : 0,
price: course.price,
estimatedPrice: course.estimatedPrice,
purchased: course.purchased,
Expand Down Expand Up @@ -1732,6 +1733,7 @@ export const getTopViewing = catchAsync(async (req: Request, res: Response, next
publisher: course.authorId,
category: course.category,
rating: course.rating,
ratingCount: course.reviews ? course.reviews.length : 0,
price: course.price,
estimatedPrice: course.estimatedPrice,
purchased: course.purchased,
Expand Down
16 changes: 8 additions & 8 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,18 @@ export const loginUser = catchAsync(async (req: Request, res: Response, next: Ne

export const logoutUser = catchAsync(async (req: Request, res: Response, next: NextFunction) => {
res.cookie('access_token', '', {
domain: '.vercel.app',
secure: true,
sameSite: 'none',
secure: process.env.NODE_ENV === 'production',
sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax',
httpOnly: true,
maxAge: 1
maxAge: 1,
path: '/'
});
res.cookie('refresh_token', '', {
domain: '.vercel.app',
secure: true,
sameSite: 'none',
secure: process.env.NODE_ENV === 'production',
sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax',
httpOnly: true,
maxAge: 1
maxAge: 1,
path: '/'
});

const userId = req.user?._id || '';
Expand Down
8 changes: 5 additions & 3 deletions src/utils/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface ITokenOptions {
sameSite: 'lax' | 'strict' | 'none' | boolean;
secure?: boolean;
path?: string;
domain?: string;
}
const isProd = process.env.NODE_ENV === 'production';

Expand All @@ -23,8 +24,8 @@ export const accessTokenOptions: ITokenOptions = {
httpOnly: true,
secure: isProd,
path: '/',
sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax'
// secure: process.env.NODE_ENV === 'production',
sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax',
domain: process.env.NODE_ENV === 'production' ? undefined : undefined // Let browser set domain automatically
};

export const refreshTokenOptions: ITokenOptions = {
Expand All @@ -33,7 +34,8 @@ export const refreshTokenOptions: ITokenOptions = {
httpOnly: true,
secure: isProd,
path: '/',
sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax'
sameSite: process.env.NODE_ENV === 'production' ? 'none' : 'lax',
domain: process.env.NODE_ENV === 'production' ? undefined : undefined // Let browser set domain automatically
};

export const sendToken = (user: UserT, statusCode: number, res: Response) => {
Expand Down