Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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.