From c38267e3ee29505e2857eaeb4e3bf2ac56226cf8 Mon Sep 17 00:00:00 2001 From: pakeku Date: Sun, 18 May 2025 09:07:48 -0400 Subject: [PATCH 1/2] remove console.error log --- src/utils/git-user-name.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/git-user-name.ts b/src/utils/git-user-name.ts index 7dc31b0..ba5f02e 100644 --- a/src/utils/git-user-name.ts +++ b/src/utils/git-user-name.ts @@ -9,7 +9,6 @@ function getGitUserName(): string { const name = execSync('git config --get user.name', { encoding: 'utf8' }).trim(); return name || 'unknown'; } catch (err) { - console.error('Error getting git user name:', err); return 'unknown'; } } From 0ca10f41104fdbdc9803f8fd1217eaa2eff963ba Mon Sep 17 00:00:00 2001 From: pakeku Date: Sun, 18 May 2025 09:39:46 -0400 Subject: [PATCH 2/2] improve docs --- src/documentation/swaggerOptions.ts | 22 +++++-- src/routes/authRoute.ts | 98 ++++++++++++++++++++++++++++- src/utils/git-user-name.ts | 1 + 3 files changed, 116 insertions(+), 5 deletions(-) diff --git a/src/documentation/swaggerOptions.ts b/src/documentation/swaggerOptions.ts index 5c62a57..a2bb2d2 100644 --- a/src/documentation/swaggerOptions.ts +++ b/src/documentation/swaggerOptions.ts @@ -1,16 +1,30 @@ import swaggerJsdoc, { Options } from 'swagger-jsdoc'; -import { name } from '../../package.json'; +import { description, name, version } from '../../package.json'; const options: Options = { - apis: ['./src/**/*.ts'], // recursive, includes subfolders like ./src/routes + apis: ['./src/**/*.ts'], definition: { + components: { + securitySchemes: { + bearerAuth: { + bearerFormat: 'JWT', + scheme: 'bearer', + type: 'http', + }, + }, + }, info: { - description: 'A sample API documentation', + description, title: name, - version: '1.0.0', + version, }, openapi: '3.0.0', + security: [ + { + bearerAuth: [], + }, + ], }, }; diff --git a/src/routes/authRoute.ts b/src/routes/authRoute.ts index f006429..08261e5 100644 --- a/src/routes/authRoute.ts +++ b/src/routes/authRoute.ts @@ -19,7 +19,39 @@ interface User { password: string; } -// Register endpoint +// register endpoint +/** + * @swagger + * /auth/register: + * post: + * summary: Register a new user + * tags: [Auth] + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * required: + * - email + * - password + * properties: + * email: + * type: string + * format: email + * password: + * type: string + * format: password + * responses: + * 201: + * description: User registered successfully + * 400: + * description: Email and password are required + * 409: + * description: Email already taken + * 500: + * description: Internal server error + */ router.post('/register', async (req: Request, res: Response): Promise => { const { email, password } = req.body as User; @@ -51,6 +83,46 @@ router.post('/register', async (req: Request, res: Response): Promise => { }); // Login endpoint +/** + * @swagger + * /auth/login: + * post: + * summary: Log in an existing user + * tags: [Auth] + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * required: + * - email + * - password + * properties: + * email: + * type: string + * format: email + * password: + * type: string + * format: password + * responses: + * 200: + * description: User logged in successfully + * content: + * application/json: + * schema: + * type: object + * properties: + * message: + * type: string + * token: + * type: string + * 401: + * description: Invalid credentials + * 500: + * description: Internal server error + */ + router.post('/login', async (req: Request, res: Response): Promise => { const { email, password } = req.body as User; @@ -81,6 +153,30 @@ router.post('/login', async (req: Request, res: Response): Promise => { }); // responds with user data +/** + * @swagger + * /auth/me: + * get: + * summary: Get current authenticated user info + * tags: [Auth] + * security: + * - bearerAuth: [] + * responses: + * 200: + * description: Returns user email + * content: + * application/json: + * schema: + * type: object + * properties: + * email: + * type: string + * 401: + * description: No token provided or unauthorized + * 500: + * description: Internal server error + */ + router.get('/me', authMiddleware, async (req: Request, res: Response): Promise => { const token = req.headers.authorization?.split(' ')[1]; diff --git a/src/utils/git-user-name.ts b/src/utils/git-user-name.ts index ba5f02e..7dc31b0 100644 --- a/src/utils/git-user-name.ts +++ b/src/utils/git-user-name.ts @@ -9,6 +9,7 @@ function getGitUserName(): string { const name = execSync('git config --get user.name', { encoding: 'utf8' }).trim(); return name || 'unknown'; } catch (err) { + console.error('Error getting git user name:', err); return 'unknown'; } }