Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pageton/authify
Authify is a simple and efficient authentication system built with Go and Fiber, designed to handle user authentication securely and flexibly for web applications.
https://github.com/pageton/authify
Last synced: about 2 months ago
JSON representation
Authify is a simple and efficient authentication system built with Go and Fiber, designed to handle user authentication securely and flexibly for web applications.
- Host: GitHub
- URL: https://github.com/pageton/authify
- Owner: pageton
- License: mit
- Created: 2024-10-16T16:23:10.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-10-21T12:33:53.000Z (3 months ago)
- Last Synced: 2024-10-23T12:20:04.271Z (3 months ago)
- Language: Go
- Homepage:
- Size: 187 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Authify Service
Authify is an authentication service built using Go, Fiber, and SQLite. It provides functionalities for user registration, login, and JWT-based authentication.
## Features
- **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## Project Structure
```
βββ π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
```## Installation
1. Clone the repository:
```bash
git clone https://github.com/pageton/authify.git
cd authify
```2. Install dependencies:
```bash
go mod tidy
```3. Copy the example environment file:
```bash
cp .env.example .env
```## Database Setup
To apply the schema for the SQLite database, run the following command:
```bash
sqlite3 ./db/database/data.db < ./db/migrations/schema.sql
```This will create the necessary tables and indexes for your application.
## Using sqlc
To generate the Go code from your SQL queries, use `sqlc`. Ensure that you have the correct `sqlc.yaml` configuration file.
### Run the following command to generate Go code from SQL:
```bash
make sqlc-generate
```## .env Configuration
The `.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:
```bash
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 as `0.0.0.0:3000`.
- `LIMIT`: The maximum number of requests allowed per second (rate limiting).
### Example `.env` file:
```env
SECRET_KEY=your_generated_secret_key
DATABASE_PATH=./db/database/data.db
PORT=:3000
LIMIT=5
```## Running the Project
### Development Mode
To run the project in development mode:
```bash
go run ./cmd
```This will execute the `main.go` file located in the `cmd` folder.
### Build and Run
To build and run the project in production mode:
1. Build the project:
```bash
go build -o auth ./cmd
```2. Run the built binary:
```bash
./auth
```## Docker Setup
### Build the Docker Image
```bash
make docker-build
```### Run the Docker Container
```bash
make docker-run
```For public deployment, ensure that the port is set to `0.0.0.0:3000` in the `.env` file.
## API Endpoints
### Registration Endpoint
- **POST** `/register`
- Body:
```json
{
"username": "your_username",
"password": "your_password"
}
```### Login Endpoint
- **POST** `/login`
- Body:
```json
{
"username": "your_username",
"password": "your_password"
}
```### Logout Endpoint
- **POST** `/logout`
- Body:
```json
{
"user_id": "user_id_to_logout"
}
```### Protected Endpoint
- **GET** `/protected`
- Headers: Must include a valid JWT token in the authorization header.```bash
Authorization: Bearer
```## Makefile Commands
1. **run**:
- Runs the Go project by executing the main file located in `cmd/main.go`.2. **migrate db-up**:
- Runs database migrations using the setup file in `db/migrations/db_migrations/setup.go`.3. **build**:
- Compiles the Go project into a binary named `authfiy`.4. **clean**:
- Cleans up the project by removing the compiled binary.5. **rebuild**:
- Cleans and rebuilds the project from scratch.6. **all**:
- Builds and then immediately runs the project.7. **docker-build**:
- Builds the Docker image for the project using the Dockerfile.8. **docker-run**:
- Runs the Docker container and logs the output. The container will run on the host network using the specified port from `.env`.9. **docker-clean**:
- Stops and removes the Docker container if it's running.10. **docker-restart**:
- Stops, removes, and then restarts the Docker container.11. **docker-compose-up**:
- Runs the project using `docker-compose`.12. **sqlc-generate**:
- Generates Go code from SQL queries based on the configuration file located at `db/migrations/sqlc.yaml`.13. **help**:
- Displays all available `Makefile` commands with a brief description.## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.