add some features
This commit is contained in:
+117
-9
@@ -1,3 +1,40 @@
|
||||
/**
|
||||
* @openapi
|
||||
* /api/v1/auth:
|
||||
* post:
|
||||
* summary: Admin login
|
||||
* tags:
|
||||
* - Authentication
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required:
|
||||
* - username
|
||||
* - password
|
||||
* properties:
|
||||
* username:
|
||||
* type: string
|
||||
* password:
|
||||
* type: string
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Login successful
|
||||
* 401:
|
||||
* description: Invalid credentials
|
||||
* get:
|
||||
* summary: Check session
|
||||
* tags:
|
||||
* - Authentication
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Session valid
|
||||
* 401:
|
||||
* description: Session invalid
|
||||
*/
|
||||
|
||||
/*
|
||||
* Authentication routes for the PoC application.
|
||||
*
|
||||
@@ -22,15 +59,39 @@ import {
|
||||
const router = Router();
|
||||
|
||||
/**
|
||||
* Admin login endpoint.
|
||||
* Expects: { username: string, password: string }
|
||||
* Returns: { success: true, token: string } or { success: false, message: string }
|
||||
* @openapi
|
||||
* /api/v1/auth/admin/login:
|
||||
* post:
|
||||
* summary: Admin login
|
||||
* tags:
|
||||
* - Authentication
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required:
|
||||
* - username
|
||||
* - password
|
||||
* properties:
|
||||
* username:
|
||||
* type: string
|
||||
* password:
|
||||
* type: string
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Login successful
|
||||
* 401:
|
||||
* description: Invalid credentials
|
||||
*/
|
||||
router.post(
|
||||
'/admin/login',
|
||||
asyncHandler(async (req, res) => {
|
||||
const { username, password } = req.body;
|
||||
|
||||
console.log(`PUBLIC: POST /api/v1/auth/admin/login - user: ${username}`);
|
||||
|
||||
if (!username || !password) {
|
||||
return res.status(400).json({ success: false, message: 'Username and password required.' });
|
||||
}
|
||||
@@ -56,15 +117,39 @@ router.post(
|
||||
);
|
||||
|
||||
/**
|
||||
* User login endpoint.
|
||||
* Expects: { email: string, password: string }
|
||||
* Returns: { success: true, token: string, user: object } or { success: false, message: string }
|
||||
* @openapi
|
||||
* /api/v1/auth/user/login:
|
||||
* post:
|
||||
* summary: User login
|
||||
* 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(
|
||||
'/user/login',
|
||||
asyncHandler(async (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
|
||||
console.log(`PUBLIC: POST /api/v1/auth/user/login - email: ${email}`);
|
||||
|
||||
if (!email || !password) {
|
||||
return res.status(400).json({ success: false, message: 'Email and password required.' });
|
||||
}
|
||||
@@ -90,11 +175,21 @@ router.post(
|
||||
);
|
||||
|
||||
/**
|
||||
* Logout endpoint - clears session.
|
||||
* @openapi
|
||||
* /api/v1/auth/logout:
|
||||
* post:
|
||||
* summary: Logout
|
||||
* tags:
|
||||
* - Authentication
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Logged out
|
||||
*/
|
||||
router.post('/logout', (req, res) => {
|
||||
const token = req.cookies?.auth_token || req.headers.authorization?.replace('Bearer ', '');
|
||||
|
||||
console.log(`PUBLIC: POST /api/v1/auth/logout`);
|
||||
|
||||
if (token) {
|
||||
removeSession(token);
|
||||
}
|
||||
@@ -104,12 +199,25 @@ router.post('/logout', (req, res) => {
|
||||
});
|
||||
|
||||
/**
|
||||
* Check current session validity.
|
||||
* Returns session data if valid, 401 if not.
|
||||
* @openapi
|
||||
* /api/v1/auth/check:
|
||||
* get:
|
||||
* summary: Check session validity
|
||||
* tags:
|
||||
* - Authentication
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Session valid
|
||||
* 401:
|
||||
* description: Session invalid
|
||||
*/
|
||||
router.get('/check', (req, res) => {
|
||||
const token = req.cookies?.auth_token || req.headers.authorization?.replace('Bearer ', '');
|
||||
|
||||
console.log(`PUBLIC: GET /api/v1/auth/check`);
|
||||
|
||||
if (!token) {
|
||||
return res.status(401).json({ authenticated: false, message: 'No session token.' });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user