Authify is an authentication service built using Go, Fiber, and SQLite. It provides functionalities for user registration, login, and JWT-based authentication.
- Fast and efficient authentication service
- Supports user registration and login functionalities
- JWT-based secure authentication
- SQLite database support with both on-disk and in-memory options
- Synchronous API ensuring better performance and concurrency handling
- Rate limiting for request control
- CORS support for secure API access from different origins
- Middleware handling for error logging, request logging, and authentication checks
└── 📁authify
└── 📁cmd
└── main.go
└── 📁config
└── config.go
└── 📁db
└── 📁database
└── data.db
└── 📁migrations
└── 📁db_migrations
└── setup.go
└── queries.sql
└── schema.sql
└── sqlc.yaml
└── 📁model
└── db.go
└── models.go
└── queries.sql.go
└── 📁handler
└── login_handler.go
└── logout_handler.go
└── register_handler.go
└── 📁middleware
└── auth_middleware.go
└── cors_middleware.go
└── error_handling.go
└── rate_limiting.go
└── request_logging.go
└── 📁models
└── user.go
└── 📁services
└── jwt_service.go
└── .env
└── .env.example
└── .gitignore
└── docker-compose.yml
└── Dockerfile
└── go.mod
└── go.sum
└── LICENSE
└── Makefile
└── README.md
-
Clone the repository:
git clone https://github.com/pageton/authify.git cd authify -
Install dependencies:
go mod tidy
-
Copy the example environment file:
cp .env.example .env
To apply the schema for the SQLite database, run the following command:
sqlite3 ./db/database/data.db < ./db/migrations/schema.sqlThis will create the necessary tables and indexes for your application.
To generate the Go code from your SQL queries, use sqlc. Ensure that you have the correct sqlc.yaml configuration file.
make sqlc-generateThe .env file contains configuration variables that the project uses. Here are the key variables:
-
SECRET_KEY: The secret key used for JWT encryption. You can generate a 256-bit key using OpenSSL:openssl rand -base64 32
-
DATABASE_PATH: The path to the SQLite database file. -
PORT: The port on which the server runs. For Docker or public deployment, set it as0.0.0.0:3000. -
LIMIT: The maximum number of requests allowed per second (rate limiting).
SECRET_KEY=your_generated_secret_key
DATABASE_PATH=./db/database/data.db
PORT=:3000
LIMIT=5To run the project in development mode:
go run ./cmdThis will execute the main.go file located in the cmd folder.
To build and run the project in production mode:
-
Build the project:
go build -o auth ./cmd
-
Run the built binary:
./auth
make docker-buildmake docker-runFor public deployment, ensure that the port is set to 0.0.0.0:3000 in the .env file.
- POST
/register- Body:
{ "username": "your_username", "password": "your_password" }
- Body:
- POST
/login- Body:
{ "username": "your_username", "password": "your_password" }
- Body:
- POST
/logout- Body:
{ "user_id": "user_id_to_logout" }
- Body:
-
GET
/protected- Headers: Must include a valid JWT token in the authorization header.
Authorization: Bearer <your_jwt_token>
-
run:
- Runs the Go project by executing the main file located in
cmd/main.go.
- Runs the Go project by executing the main file located in
-
migrate db-up:
- Runs database migrations using the setup file in
db/migrations/db_migrations/setup.go.
- Runs database migrations using the setup file in
-
build:
- Compiles the Go project into a binary named
authfiy.
- Compiles the Go project into a binary named
-
clean:
- Cleans up the project by removing the compiled binary.
-
rebuild:
- Cleans and rebuilds the project from scratch.
-
all:
- Builds and then immediately runs the project.
-
docker-build:
- Builds the Docker image for the project using the Dockerfile.
-
docker-run:
- Runs the Docker container and logs the output. The container will run on the host network using the specified port from
.env.
- Runs the Docker container and logs the output. The container will run on the host network using the specified port from
-
docker-clean:
- Stops and removes the Docker container if it's running.
-
docker-restart:
- Stops, removes, and then restarts the Docker container.
-
docker-compose-up:
- Runs the project using
docker-compose.
- Runs the project using
-
sqlc-generate:
- Generates Go code from SQL queries based on the configuration file located at
db/migrations/sqlc.yaml.
- Generates Go code from SQL queries based on the configuration file located at
-
help:
- Displays all available
Makefilecommands with a brief description.
- Displays all available
This project is licensed under the MIT License - see the LICENSE file for details.