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
22 changes: 18 additions & 4 deletions src/documentation/swaggerOptions.ts
Original file line number Diff line number Diff line change
@@ -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: [],
},
],
},
};

Expand Down
98 changes: 97 additions & 1 deletion src/routes/authRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
const { email, password } = req.body as User;

Expand Down Expand Up @@ -51,6 +83,46 @@ router.post('/register', async (req: Request, res: Response): Promise<void> => {
});

// 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<void> => {
const { email, password } = req.body as User;

Expand Down Expand Up @@ -81,6 +153,30 @@ router.post('/login', async (req: Request, res: Response): Promise<void> => {
});

// 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<void> => {
const token = req.headers.authorization?.split(' ')[1];

Expand Down