This is a backend API service in Golang that handles authorization and authentication for a web app where users in an organization can sign in and list all other users in their organization. The API follows REST API conventions and covers the following functionalities:
- User Login
- User Logout
- Admin User adds a new User account (by providing the username & password)
- Admin User deletes an existing User account from their organization
- List all Users in their organization
Commands can be run over killercoda interface.
- Go 1.21 or later ~
https://go.dev/doc/install
- MongoDB v4.4
https://www.mongodb.com/docs/manual/administration/install-community/
- Postman or any API development environment
https://www.postman.com/downloads/
$ git clone https://github.com/<username>/<repository>.git
$ cd <repository>
$ go mod tidy
- Switch to the
/configs/config.go
and set the values for your desired variables.
$ go run main.go
The API will be available at http://localhost:<configs.Cfg.Port>
. (Set the value for the Port according to your own requirement.)
I chose to use the Echo framework for this project due to its simplicity, performance, and ease of use. Echo is a lightweight web framework that provides a minimalistic approach to building web applications and APIs.
I chose MongoDB as the database for this project due to its flexibility, scalability, and ease of use. MongoDB is a document-oriented NoSQL database that allows for easy data modeling and flexible data structures.
I decided not to use an ORM for this project due to the simplicity of the data model and the limited number of database operations required by the API. Instead, I used the official MongoDB driver for Go to interact with the database.
For JWT token generation, I used the golang-jwt library, which provides a simple and easy-to-use interface for generating and verifying JWT tokens.
I followed REST API conventions for designing the API endpoints and used HTTP methods and status codes to represent the different actions and outcomes of the API requests. I also used middleware to handle authentication and authorization and to enforce input validation and error handling. This API consists of the following routes:
POST /login
- For logging in an user Paramenters:username
,password
POST /logout
- For logginf out an user Parameters:username
,password
POST /refresh
- For refreshing a token Parameters:username
,passowrd
GET /
- For getting all users in an organization Parameters-username
POST /add
- For adding one user to the organization Parameters-username
,newUsername
,newPassword
,isAdmin
,organization
POST /delete
- For deleting a user from the organization Parameters-username
,delUsername