Skip to content

shuvosvc/go-gin-str

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Authentication API

A simple Go backend API for user authentication with signup and signin, using Gin, Gorm, and MySQL.
Follows clean project structure, proper error handling, and JWT-based authentication.

🧱 Features

Signup (/api/user/signup)

Signin (/api/user/signin) with JWT token generation

Password hashing with bcrypt

Input validation (email & password)

Clean project structure: controllers, models, routes, utils, config

Environment variables support (.env)

Auto database migration using Gorm

⚡ Tech Stack

Language: Go

Framework: Gin (Express-like)

ORM: Gorm

Database: MySQL

Auth: JWT

Password hashing: bcrypt

📂 Project Structure
go-auth-api/
├── cmd/
│   └── main.go            # Entry point
├── config/
│   └── config.go          # DB & env config
├── controllers/
│   └── user_controller.go # Signup & signin
├── models/
│   └── user_model.go      # User struct
├── routes/
│   └── user_routes.go     # API routes
├── utils/
│   ├── hash.go            # Password hashing, JWT
│   ├── response.go        # Standard JSON responses
│   └── validation.go      # Input validation
├── go.mod
├── go.sum
└── .env                   # Environment variables (ignored by git)

🔧 Setup Instructions
1️⃣ Clone the repository
git clone <your-repo-url>
cd go-auth-api

2️⃣ Install dependencies
go mod tidy

3️⃣ Create .env file

Create a .env file in the project root:

DB_USER=nura
DB_PASS=7061svcnura
DB_HOST=localhost
DB_PORT=3306
DB_NAME=test
JWT_SECRET=supersecret
PORT=8080

4️⃣ Run the server
go run cmd/main.go


Server runs on http://localhost:8080.

🧪 API Endpoints
1️⃣ Signup

URL: /api/user/signup

Method: POST

Body:

{
  "first_name": "Arafat",
  "last_name": "Rahman",
  "email": "arafat@example.com",
  "password": "123456"
}


Response:

{
  "success": true,
  "message": "Signup successful",
  "data": {
    "id": 1,
    "first_name": "Arafat",
    "last_name": "Rahman",
    "email": "arafat@example.com"
  }
}

2️⃣ Signin

URL: /api/user/signin

Method: POST

Body:

{
  "email": "arafat@example.com",
  "password": "123456"
}


Response:

{
  "success": true,
  "message": "Signin successful",
  "data": {
    "token": "<jwt_token>"
  }
}

🔒 JWT Authentication (Optional)

Use the JWT returned from signin for protected routes.

Add middleware to verify token in requests.

📝 Notes

Passwords are stored hashed (bcrypt)

Emails must be unique

Environment variables are loaded via .env

Auto migration is handled by Gorm

💻 Running in Development

Use hot reload with Air:

go install github.com/cosmtrek/air@latest
air


Server will reload automatically on file changes.

⚡ License

MIT License – free to use and modify.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages