Skip to content

First-Derivative/hades

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go HTTP Server with Gin and JWT Authentication

This Go HTTP server is built using the Gin framework and employs JWT (JSON Web Tokens) for authentication. It allows you to secure your endpoints and control access to your resources. To run this server, you need to set up specific environment properties defined in a .env file. Also uses SQL (I recommend planetscale for hobby devs)

Prerequisites

Before running the server, make sure you have the following prerequisites installed and configured:

  • Go: Make sure you have Go installed on your system. You can download it from here.

Getting Started

  1. Clone Repository:
git clone https://github.com/First-Derivative/hades.git
cd hades
  1. Create .env File:

Create a .env file in the root directory of your project with the following properties:

PORT=8080 # Port on which the server will run
DNS="some sql dns connection string"      # Domain or hostname for sql server
JWT_SECRET=your-secret-key # Secret key for JWT token generation and validation

Build and Run:

go run main.go or go build main.go && ./main

  1. Access the Server: The server will start, and you can access it at http://127.0.0.1:8080 (or the DNS and port you specified in the .env file). Example: GET /validate: User authentication. Send a POST request with credentials to obtain a JWT token.

API Endpoints

/signup (POST)

Details
  • Description: Allows users to sign up by providing valid credentials.

  • URL: /signup

  • Method: POST

  • Request Body:

    • Format: JSON
    • Fields:
      • user (SignupRequest) - User registration details
  • Success Response (201 Created):

    • Response Body: JSON
      • token (TokenResponse) - User successfully registered
  • Error Response (400 Bad Request):

    • Response Body: JSON - error (ErrorResponse) - Invalid input data

/login (POST)

Details
  • Description: Allows users to authenticate by providing valid credentials.

  • URL: /login

  • Method: POST

  • Request Body:

    • Format: JSON
    • Fields:
      • email: email as string
      • password: password as string
  • Success Response (200 OK):

    • Response Body: JSON
      • status : success
  • Error Response (401 Unauthorized):

    • Response Body: JSON - error (ErrorResponse) - Invalid credentials

/logout (GET)

Details
  • Description: Logs out the currently authenticated user.

  • URL: /logout

  • Method: GET

  • Authentication: Requires a valid JWT token (ApiKeyAuth)

  • Success Response (204 No Content):

    • User successfully logged out
  • Error Response (401 Unauthorized):

    • Response Body: JSON - error (ErrorResponse) - Authentication required

/validate (GET)

Details
  • Description: Validates the provided JWT token.

  • URL: /validate

  • Method: GET

  • Authentication: Requires a valid JWT token (ApiKeyAuth)

  • Success Response (200 OK):

    • Response Body: JSON
      • status : authenticated
      • success: true,
      • user: user email
  • Error Response (401 Unauthorized):

    • Response Body: JSON
      • error (ErrorResponse) - Invalid token or authentication required

Request and Response Data Structures

// POST body for signup
{
  "email": "string",
  "password": "string",
  "firstName": "string" | null,
  "lastName": "string" | null
}

// POST body for login
{
  "email": "string",
  "password": "string"
}

// Error response
{
    "error": "string"
}

About

A simple HTTP server that authenticates users with JWT Token. Written in Go.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages