Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fabioluz/go-auth
API with Authentication in Go
https://github.com/fabioluz/go-auth
api gin-gonic go jwt mongodb
Last synced: 1 day ago
JSON representation
API with Authentication in Go
- Host: GitHub
- URL: https://github.com/fabioluz/go-auth
- Owner: fabioluz
- Created: 2024-02-28T12:42:51.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-04-22T00:16:19.000Z (9 months ago)
- Last Synced: 2024-11-08T23:39:41.281Z (about 2 months ago)
- Topics: api, gin-gonic, go, jwt, mongodb
- Language: Go
- Homepage:
- Size: 50.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# GO Auth
This an example of API with authentication in Go. It gives some separation of concerns by organizing the layers into different packages, isolating the capabilities on their own files, and abstracting the dependencies using design patterns. All dependencies are interpreted in the `main.go` file.
Libraries used:
* Gin for the API.
* JWT for generating authorization token.
* MongoDB Client for database connection.The application is capable of creating, authorizing, and updating users. All the operations are logged in the database and can be also retrieved in the API.
You can run everything via docker compose.
## Prerequisites
- Docker installed on your machine
## Setting Up the Application
Firstly, you need to create a `.env` file in the root folder containing:
```
DB_URI=mongodb://appmongo1:27017
DB_REPLICA_SET=myReplicaSet
JWT_SECRET=
```You can change the details of `DB_URI` and `DB_REPLICA_SET` as needed.
Then, build the API and MongoDB cluster by running then via docker compose:
```bash
docker compose up -d
```## Using the application
### Creating an user
```
POST http://localhost:8080/users/
{
"email": "[email protected]",
"password": "nicepassword",
"confirmPassword": "nicepassword",
"name": "wow nice name"
}
```### Authenticating an user
```
POST http://localhost:8080/authenticate/
{
"email": "[email protected]",
"password": "nicepassword"
}
```This should return the `token` and `id` that you can use to access the API.
### Authorizing an user
Add an HTTP `Authorization` header with the format `Bearer {token}`
### Fetching logged-in user info
```
GET http://localhost:8080/me
HEADER Autorization: Bearer {token}
```Returns the details of the logged-in user.
### Fetching user logs
```
GET http://localhost:8080/users/{id}/logs
HEADER Authorization: Bearer {token}
```Returns the logs for the specified user.