399 lines
12 KiB
JavaScript
399 lines
12 KiB
JavaScript
const { Router } = require("express");
|
|
const router = Router();
|
|
const userController = require("../controllers/userController");
|
|
const electionController = require("../controllers/electionController");
|
|
|
|
/**
|
|
* @swagger
|
|
* /auth/login:
|
|
* post:
|
|
* summary: User login
|
|
* description: Allows users to log in with their username or email and password.
|
|
* tags:
|
|
* - Auth
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* username:
|
|
* type: string
|
|
* password:
|
|
* type: string
|
|
* remember_me:
|
|
* type: boolean
|
|
* example:
|
|
* username: johndoe
|
|
* password: password123
|
|
* remember_me: true
|
|
* responses:
|
|
* '200':
|
|
* description: User logged in successfully.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* token:
|
|
* type: string
|
|
* example:
|
|
* token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
|
|
* '401':
|
|
* description: Unauthorized. Indicates that the provided credentials are invalid.
|
|
* '403':
|
|
* description: Forbidden. Indicates that the account is inactive. Contact the admin for assistance.
|
|
* '422':
|
|
* description: Validation error. Indicates that the request body contains invalid data.
|
|
*/
|
|
// Login User
|
|
router.post("/login", userController.login);
|
|
|
|
/**
|
|
* @swagger
|
|
* /auth/otp:
|
|
* post:
|
|
* summary: Send OTP to user for login
|
|
* description: Allows users to log in with their phone number by sending an OTP.
|
|
* tags:
|
|
* - Auth
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* phone:
|
|
* type: string
|
|
* example:
|
|
* phone: "09124569875"
|
|
* responses:
|
|
* 200:
|
|
* description: OTP sent successfully.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* message:
|
|
* type: string
|
|
* example:
|
|
* message: "OTP sent successfully."
|
|
*/
|
|
router.post("/otp", userController.otp);
|
|
|
|
/**
|
|
* @swagger
|
|
* /auth/otp/verify:
|
|
* post:
|
|
* summary: Verify the OTP sent to user
|
|
* description: Allows users to log in by verifying the OTP sent to their phone number.
|
|
* tags:
|
|
* - Auth
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* phone:
|
|
* type: string
|
|
* code:
|
|
* type: string
|
|
* example:
|
|
* phone: "09124569875"
|
|
* code: "12345"
|
|
* responses:
|
|
* 200:
|
|
* description: User logged in successfully.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* token:
|
|
* type: string
|
|
* example:
|
|
* token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
|
|
* 401:
|
|
* description: Unauthorized. Indicates that the provided credentials are invalid.
|
|
* 403:
|
|
* description: Forbidden. Indicates that the account is inactive. Contact the admin for assistance.
|
|
* 422:
|
|
* description: Validation error. Indicates that the request body contains invalid data.
|
|
*/
|
|
router.post("/otp/verify", userController.verifyOtp);
|
|
/**
|
|
* @swagger
|
|
* /auth/logout:
|
|
* delete:
|
|
* summary: User logout
|
|
* description: Logs out the user by invalidating the token associated with the request.
|
|
* tags:
|
|
* - Auth
|
|
* responses:
|
|
* '200':
|
|
* description: User logged out successfully.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* message:
|
|
* type: string
|
|
* example:
|
|
* message: "You are logged out."
|
|
* '401':
|
|
* description: Unauthorized. Indicates that the user is not logged in.
|
|
*/
|
|
// Logout User
|
|
router.delete("/logout", userController.logout);
|
|
/**
|
|
* @swagger
|
|
* /auth/user:
|
|
* get:
|
|
* summary: Get user information
|
|
* description: Retrieves information about the authenticated user.
|
|
* tags:
|
|
* - Auth
|
|
* responses:
|
|
* '200':
|
|
* description: User information retrieved successfully.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* user:
|
|
* $ref: '#/components/schemas/User'
|
|
* '401':
|
|
* description: Unauthorized. Indicates that the user is not authenticated or the token is invalid.
|
|
*/
|
|
// Data User
|
|
router.get("/user", userController.getuser);
|
|
/**
|
|
* @swagger
|
|
* /auth/forget-password:
|
|
* post:
|
|
* summary: Generate token for password reset
|
|
* description: Sends a token to the user's email for password reset.
|
|
* tags:
|
|
* - Auth
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* email:
|
|
* type: string
|
|
* responses:
|
|
* '200':
|
|
* description: Token generated successfully and sent to the user's email.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: string
|
|
* description: Token generated for password reset.
|
|
* '422':
|
|
* description: Validation error. Indicates that the provided email is not valid.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* validation:
|
|
* type: object
|
|
* description: Error messages for validation failures.
|
|
* '404':
|
|
* description: User not found. Indicates that the provided email does not belong to any user.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* message:
|
|
* type: string
|
|
* description: Error message indicating that the user was not found.
|
|
* '500':
|
|
* description: Internal server error. Indicates that an unexpected error occurred.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* message:
|
|
* type: string
|
|
* description: Error message indicating the internal server error.
|
|
*/
|
|
// Change Password
|
|
router.post("/changePassword", userController.forgetPassGenerateToken);
|
|
/**
|
|
* @swagger
|
|
* /auth/changePassword/{token}:
|
|
* put:
|
|
* summary: Reset password using token
|
|
* description: Resets the user's password using the provided token.
|
|
* tags:
|
|
* - Auth
|
|
* parameters:
|
|
* - in: path
|
|
* name: token
|
|
* required: true
|
|
* description: The token sent to the user's email for password reset.
|
|
* schema:
|
|
* type: string
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* password:
|
|
* type: string
|
|
* description: The new password.
|
|
* responses:
|
|
* '200':
|
|
* description: Password reset successful.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* message:
|
|
* type: string
|
|
* description: Success message indicating that the password was reset successfully.
|
|
* '422':
|
|
* description: Validation error. Indicates that the provided password does not meet the requirements or the passwords do not match.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* validation:
|
|
* type: object
|
|
* description: Error messages for validation failures.
|
|
* '500':
|
|
* description: Internal server error. Indicates that an unexpected error occurred.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* message:
|
|
* type: string
|
|
* description: Error message indicating the internal server error.
|
|
*/
|
|
// Change Password :token param
|
|
router.put("/changePassword/:token", userController.forgetPassGetToken);
|
|
|
|
///////// voter
|
|
|
|
/**
|
|
* @swagger
|
|
* /auth/voter/login:
|
|
* post:
|
|
* summary: Voter login
|
|
* description: Logs in a voter to participate in an election.
|
|
* tags:
|
|
* - Auth
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* username:
|
|
* type: string
|
|
* description: The username of the voter.
|
|
* password:
|
|
* type: string
|
|
* description: The password of the voter.
|
|
* responses:
|
|
* '200':
|
|
* description: Voter login successful.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* token:
|
|
* type: string
|
|
* description: The token associated with the voter.
|
|
* '422':
|
|
* description: Validation error. Indicates that the provided username or password is incorrect.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* validation:
|
|
* type: object
|
|
* description: Error message for validation failure.
|
|
* '500':
|
|
* description: Internal server error. Indicates that an unexpected error occurred.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* message:
|
|
* type: string
|
|
* description: Error message indicating the internal server error.
|
|
*/
|
|
|
|
//Voter Login
|
|
router.post("/voter/login", electionController.voterLogin);
|
|
/**
|
|
* @swagger
|
|
* /auth/voter/user:
|
|
* get:
|
|
* summary: Get voter user
|
|
* description: Retrieves the details of the logged-in voter.
|
|
* tags:
|
|
* - Auth
|
|
* security:
|
|
* - BearerAuth: []
|
|
* responses:
|
|
* '200':
|
|
* description: Voter user details retrieved successfully.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* user:
|
|
* type: object
|
|
* description: Details of the logged-in voter.
|
|
* '422':
|
|
* description: Validation error. Indicates that the token is invalid or the voter does not exist.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* validation:
|
|
* type: object
|
|
* description: Error message for validation failure.
|
|
* '500':
|
|
* description: Internal server error. Indicates that an unexpected error occurred.
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* message:
|
|
* type: string
|
|
* description: Error message indicating the internal server error.
|
|
*/
|
|
// user Voter Data
|
|
router.get("/voter/user", electionController.getVoterUser);
|
|
|
|
module.exports = router;
|