This is an API for managing user profiles and authentication, built using Golang and MySQL. It provides endpoints for user registration, login, updating profiles, and retrieving profiles with role-based access control. JWT is used for authentication and authorization.
- User registration with username uniqueness validation.
- User login with JWT generation and validation.
- Update user profiles with validation and formatting.
- Retrieve user profiles with role-based access control.
- Delete a user by ID with role-based access control.
- Retrieve users with filtering and pagination.
- Clone the repository and navigate to the root folder:
git clone https://github.com/mucha-fauzy/users-management-crud-api.git
cd users-management-crud-api
-
Install the required dependencies.
-
Create
.env
and set your MySQL or other DB configurations. Refer to./infras/mysql.go
for the required parameters. -
Set up the database tables by running the migration scripts
go run ./migrations/users_table.go
. -
Seed the Admin ID for testing
go run ./seeders/auth_seed.go
.
username = "admin_fauzy"
password = "passwordkuat"
- Generate the necessary wire code:
go generate ./...
- Build the application:
go build
- Run the application:
go run .
The API will be accessible at http://localhost:8080.
Send a POST request to /v1/auth/register
with a JSON payload containing the registration details.
Send a POST request to /v1/auth/login
with a JSON payload containing the login credentials. The response will contain a JWT token.
Send a PATCH request to /v1/profiles
with a JSON payload containing the profile fields to update. Requires a valid JWT for authentication.
Send a GET request to /v1/profiles
to retrieve the profile of the authenticated user. Requires a valid JWT for authentication.
Send a GET request to /v1/users
to retrieve the users data. Requires a valid JWT with admin role for authentication. You can use the following query parameters for filtering and pagination:
- name: Filter users by name (optional).
- city: Filter users by city (optional).
- province: Filter users by province (optional).
- jobRole: Filter users by job role (optional).
- status: Filter users by status (optional).
- page: Page number for pagination (optional, default: 1).
- size: Number of items per page (optional, default: 5).
The response will include a paginated list of users. The pagination information will be provided in the response body. Here's what the pagination information means:
Response Headers:
Total Data
: The total number of users that match the filter criteria.Total Pages
: The total number of pages based on the providedsize
.Current Page
: The current page number.Next Page
: The page number for the next page (if available).Previous Page
: The page number for the previous page (if available).
Send a DELETE request to /v1/users/{user_id}
to delete a user. Requires a valid JWT with admin role for authentication. Users with the 'admin' role cannot be deleted.
To facilitate testing and interacting with the Users Management API, I provide a Postman collection named Users-Management-API.postman_collection.json
. This collection includes a set of pre-configured requests that you can use to test various API endpoints easily.
- Download the
Users-Management-API.postman_collection.json
file from this repository. - Open Postman and click on the "Import" button in the top-left corner.
- Select the downloaded JSON file and import it into Postman.
For each request in the Postman collection, I included test scripts to validate the response and ensure that the API endpoints are functioning correctly. To run the test scripts:
- Open the imported Postman collection.
- Select the request you want to test.
- Click the "Send" button to make the API request.
- Postman will automatically execute the test scripts and display the results.
Feel free to use the Postman collection to explore the API's capabilities and verify its functionality. The included test scripts help ensure that your API is working as expected.
This README provides a basic overview of the API and its features. Please refer to the source code for more detailed information and implementation details.