https://github.com/cryognar/jwtgo
π₯ Example of microservice JWT authentication using MongoDB and clean architecture principles.
https://github.com/cryognar/jwtgo
bcrypt boilerplate clean-architecture cleanenv docker docker-compose gin go golang grpc jwt layout logrus microservice mongodb nginx protobuf reverse-proxy
Last synced: 3 months ago
JSON representation
π₯ Example of microservice JWT authentication using MongoDB and clean architecture principles.
- Host: GitHub
- URL: https://github.com/cryognar/jwtgo
- Owner: cryognar
- License: apache-2.0
- Created: 2025-01-02T20:17:59.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2025-02-26T18:58:06.000Z (3 months ago)
- Last Synced: 2025-03-04T12:52:37.360Z (3 months ago)
- Topics: bcrypt, boilerplate, clean-architecture, cleanenv, docker, docker-compose, gin, go, golang, grpc, jwt, layout, logrus, microservice, mongodb, nginx, protobuf, reverse-proxy
- Language: Go
- Homepage:
- Size: 358 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jwtgo
A Go (Golang) backend clean architecture project with Gin, MongoDB and JWT Authentication middleware.
The project was created for educational purposes and is not ideal. It has its shortcomings, which are gradually being corrected.
## Project architecture
The architecture of a web application consists of these layers:
- Reverse Proxy
- API Gateway
- Microservice
- DatabaseThe architecture of the microservice application consists of these layers:
- Server
- Service
- Repository
The API is accessed via Reverse Proxy, in our case it is Nginx. It handles all incoming requests and prevents access to microservices and API gateway directly.
## Major packages used in project
- **[Gin](https://pkg.go.dev/github.com/gin-gonic/gin)**: Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
- **[gRPC](https://pkg.go.dev/google.golang.org/grpc)**: The Go implementation of gRPC: A high performance, open source, general RPC framework that puts mobile and HTTP/2 first. For more information see the Go gRPC docs, or jump directly into the quick start.
- **[protobuf](https://pkg.go.dev/google.golang.org/protobuf)**: Go support for Google's protocol buffers.
- **[MongoDB](https://pkg.go.dev/go.mongodb.org/mongo-driver)**: The Official Golang driver for MongoDB.
- **[JWT](https://pkg.go.dev/github.com/golang-jwt/jwt/v5)**: Go implementation of JSON Web Tokens (JWT).
- **[Cleanenv](https://pkg.go.dev/github.com/ilyakaznacheev/cleanenv)**: Clean and minimalistic environment configuration reader for Golang.
- **[Bcrypt](https://pkg.go.dev/golang.org/x/crypto/bcrypt)**: Package bcrypt implements Provos and Mazières's bcrypt adaptive hashing algorithm.
- **[Logrus](https://pkg.go.dev/github.com/sirupsen/logrus)**: Structured, pluggable logging for Go.
- **[Validator](https://pkg.go.dev/github.com/go-playground/validator/v10)**: Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving.## Request flow without JWT authentication middleware
## Request flow with JWT authentication middleware
## How to run the project?
First, download it and navigate to the root directory:
```bash
# Move to your workspace
cd your-workspace# Clone the project into your workspace
git clone https://github.com/Astagnar/jwtgo.git# Move to the project root directory
cd jwtgo
```### Run with Docker
- Create a `.env` file, similar to `.env.sample`.
- Install the [Docker](https://www.docker.com/get-started/), [Protoc](https://grpc.io/docs/protoc-installation/), [Taskfile](https://taskfile.dev/installation/) if it is not installed on your computer.
- Fill in the `.env` file with your data.
- Run the application build with the following command:```bash
task build
```
- Access API using http://localhost.## Examples of API requests and responses
### SignUp endpoint
- Request:
```
curl --location 'http://localhost/auth/signup' \
--header 'Content-Type: application/json' \
--data-raw '{
"email": "[email protected]",
"password": "securepassword"
}'
```
- Response:
```
HTTP/1.1 200 OK
Content-Type: application/json
```
```json
{
"message": "User successfully registered"
}
```### SignIn endpoint
- Request:
```
curl --location 'http://localhost/auth/signin' \
--header 'Content-Type: application/json' \
--data-raw '{
"email": "[email protected]",
"password": "securepassword"
}'
```- Response:
```
HTTP/1.1 200 OK
Content-Type: application/json
Set-Cookie: access_token=access_token; Path=/; HttpOnly; SameSite=Strict
Set-Cookie: refresh_token=refresh_token; Path=/; HttpOnly; SameSite=Strict
```
```json
{
"message": "User successfully logged in"
}
```### SignOut endpoint
- Request:
```
curl --location 'http://localhost/auth/signout' \
--header 'Content-Type: application/json' \
-b 'access_token=access_token; refresh_token=refresh_token'
```- Response:
```
HTTP/1.1 200 OK
Content-Type: application/json
Set-Cookie: access_token=access_token; Path=/; HttpOnly; SameSite=Strict
Set-Cookie: refresh_token=refresh_token; Path=/; HttpOnly; SameSite=Strict
```
```json
{
"message": "User successfully logged out"
}
```### Refresh endpoint
- Request:
```
curl --location 'http://localhost/auth/refresh' \
--header 'Content-Type: application/json' \
-b 'access_token=access_token; refresh_token=refresh_token'
```- Response:
```
HTTP/1.1 200 OK
Content-Type: application/json
Set-Cookie: access_token=access_token; Path=/; HttpOnly; SameSite=Strict
Set-Cookie: refresh_token=refresh_token; Path=/; HttpOnly; SameSite=Strict
```
```json
{
"message": "Tokens successfully updated"
}
```## Complete project folder structure
```
βββ build
β βββ package
β βββ api.Dockerfile
β βββ auth.Dockerfile
β βββ user.Dockerfile
βββ cmd
β βββ api
β β βββ main.go
β βββ auth
β β βββ main.go
β βββ user
β βββ main.go
βββ configs
β βββ nginx.conf
βββ deployments
β βββ docker-compose.yaml
βββ internal
β βββ app
β β βββ api
β β β βββ config
β β β β βββ config.go
β β β βββ controller
β β β β βββ http
β β β β βββ dto
β β β β β βββ user.go
β β β β βββ mapper
β β β β β βββ user.go
β β β β βββ middleware
β β β β β βββ security.go
β β β β β βββ validation.go
β β β β βββ v1
β β β β βββ auth.go
β β β βββ main.go
β β βββ auth
β β β βββ config
β β β β βββ config.go
β β β βββ interface
β β β β βββ service
β β β β βββ auth.go
β β β βββ server
β β β β βββ grpc
β β β β βββ dto
β β β β β βββ user.go
β β β β βββ mapper
β β β β β βββ user.go
β β β β βββ v1
β β β β βββ auth.go
β β β βββ service
β β β β βββ auth.go
β β β βββ main.go
β β βββ user
β β βββ adapter
β β β βββ mongodb
β β β βββ entity
β β β β βββ user.go
β β β βββ mapper
β β β β βββ user.go
β β β βββ repository
β β β βββ user.go
β β βββ config
β β β βββ config.go
β β βββ entity
β β β βββ user.go
β β βββ interface
β β β βββ repository
β β β β βββ user.go
β β β βββ service
β β β βββ user.go
β β βββ server
β β β βββ grpc
β β β βββ dto
β β β β βββ user.go
β β β βββ mapper
β β β β βββ user.go
β β β βββ v1
β β β βββ user.go
β β βββ service
β β β βββ user.go
β β βββ main.go
β βββ pkg
β βββ error
β β βββ auth.go
β β βββ jwt.go
β β βββ repository.go
β β βββ server.go
β βββ interface
β β βββ service
β β βββ jwt.go
β β βββ password.go
β βββ proto
β β βββ auth
β β β βββ auth.pb.go
β β β βββ auth_grpc.pb.go
β β βββ user
β β βββ user.pb.go
β β βββ user_grpc.pb.go
β βββ request
β β βββ schema
β β β βββ response.go
β β βββ response.go
β βββ service
β βββ schema
β β βββ jwt.go
β βββ jwt.go
β βββ password.go
βββ pkg
β βββ client
β β βββ mongodb.go
β βββ logging
β β βββ logger.go
β βββ proto
β βββ auth
β β βββ auth.proto
β βββ user
β βββ user.proto
βββ .env
βββ .gitignore
βββ go.mod
βββ go.sum
βββ LICENSE
βββ README.md
βββ taskfile.yaml
```