import { Router } from 'express'; import multer from 'multer'; import authController from '../controllers/authController'; import { auth } from '../middleware/auth'; import passport from '../config/passport'; const router = Router(); // Configure multer for profile image uploads const storage = multer.memoryStorage(); const profileUpload = multer({ storage, limits: { fileSize: 5 * 1024 * 1024, // 5MB limit for profile images files: 1 }, fileFilter: (req, file, cb) => { if (file.mimetype.startsWith('image/')) { cb(null, true); } else { cb(new Error('Only image files are allowed for profile pictures'), false); } } }); /** * @swagger * /api/auth/register: * post: * summary: Register a new user * tags: [Authentication] * requestBody: * required: true * content: * application/json: * schema: * type: object * required: * - username * - email * - password * properties: * username: * type: string * email: * type: string * password: * type: string * firstName: * type: string * lastName: * type: string * responses: * 201: * description: User registered successfully * 400: * description: Validation error */ router.post('/register', authController.register); /** * @swagger * /api/auth/login: * post: * summary: Login user * tags: [Authentication] * requestBody: * required: true * content: * application/json: * schema: * type: object * required: * - email * - password * properties: * email: * type: string * password: * type: string * responses: * 200: * description: Login successful * 401: * description: Invalid credentials */ router.post('/login', authController.login); /** * @swagger * /api/auth/forgot-password: * post: * summary: Request password reset * tags: [Authentication] * requestBody: * required: true * content: * application/json: * schema: * type: object * required: * - email * properties: * email: * type: string * responses: * 200: * description: Reset email sent */ router.post('/forgot-password', authController.forgotPassword); /** * @swagger * /api/auth/reset-password: * post: * summary: Reset password using token * tags: [Authentication] * requestBody: * required: true * content: * application/json: * schema: * type: object * required: * - token * - newPassword * properties: * token: * type: string * newPassword: * type: string * responses: * 200: * description: Password reset successful */ router.post('/reset-password', authController.resetPassword); /** * @swagger * /api/auth/refresh: * post: * summary: Refresh access token * tags: [Authentication] * requestBody: * required: true * content: * application/json: * schema: * type: object * required: * - refreshToken * properties: * refreshToken: * type: string * responses: * 200: * description: Token refreshed * 401: * description: Invalid refresh token */ router.post('/refresh', authController.refreshToken); /** * @swagger * /api/auth/logout: * post: * summary: Logout user * tags: [Authentication] * requestBody: * content: * application/json: * schema: * type: object * properties: * refreshToken: * type: string * responses: * 200: * description: Logged out successfully */ router.post('/logout', authController.logout); /** * @swagger * /api/auth/profile: * get: * summary: Get current user profile * tags: [Authentication] * security: * - bearerAuth: [] * responses: * 200: * description: User profile * 401: * description: Unauthorized */ router.get('/profile', auth, authController.getProfile); // Google OAuth routes (only enabled if ENABLE_GOOGLE_AUTH=true) if (process.env.ENABLE_GOOGLE_AUTH === 'true') { /** * @swagger * /api/auth/google: * get: * summary: Google OAuth login * tags: [Authentication] * responses: * 302: * description: Redirect to Google OAuth */ router.get('/google', authController.googleAuth); /** * @swagger * /api/auth/google/callback: * get: * summary: Google OAuth callback * tags: [Authentication] * responses: * 302: * description: Redirect to frontend with tokens */ router.get('/google/callback', passport.authenticate('google', { failureRedirect: '/login' }), authController.googleCallback ); } /** * @swagger * /api/auth/verify-email: * post: * summary: Verify email address * tags: [Authentication] * requestBody: * required: true * content: * application/json: * schema: * type: object * required: * - token * properties: * token: * type: string * responses: * 200: * description: Email verified successfully */ router.post('/verify-email', authController.verifyEmail); /** * @swagger * /api/auth/resend-verification: * post: * summary: Resend email verification * tags: [Authentication] * requestBody: * required: true * content: * application/json: * schema: * type: object * required: * - email * properties: * email: * type: string * responses: * 200: * description: Verification email sent */ router.post('/resend-verification', authController.resendVerification); /** * @swagger * /api/auth/profile-image: * post: * summary: Upload profile image * tags: [Authentication] * security: * - bearerAuth: [] * requestBody: * required: true * content: * multipart/form-data: * schema: * type: object * properties: * profileImage: * type: string * format: binary * description: Profile image file * required: * - profileImage * responses: * 200: * description: Profile image uploaded successfully * 400: * description: No file provided or invalid file type * 401: * description: Unauthorized */ router.post('/profile-image', auth, profileUpload.single('profileImage'), authController.uploadProfileImage); export default router;