Login API in Golang
This project is a sample implementation of a login API in Golang, using Echo and JWT, and GORM as the database.
Features
- User registration (sign up)
- User authentication (sign in)
- User logout (sign out)
- JWT-based authentication
Project Structure
rbac-api/
├── main.go
├── db/
│ └── db.go
├── handlers/
│ └── auth.go
├── models/
│ └── user.go
└── utils/
└── jwt.go
Installation
- Clone the repository:
git clone https://github.com/aleksanderpalamar/login-api.git
cd login-api
- Install the dependencies:
go mod download
- Build the application:
go build
- Run the applications:
./main
Running the Application
To start the application, run:
go run main.go
The server will start on http://localhost:3000.
API Endpoints
- Sign Up
- URL: /signup
- Method: POST
- Request Body:
{
"username": "testuser",
"password": "testpass",
"role": "user"
}
{
"message": "User created successfully"
}
Sign Out
- URL: /signout
- Method: POST
- Response:
{
"message": "signed out"
}
Utility Functions
JWT Utility
The JWT utility provides functions for generating and validating JWT tokens.
GenerateJWT
Generates a JWT token for a given username.
func GenerateToken(username, password string) (string, error)
ValidateJWT
Validates a given JWT token.
func ValidateJWT(tokenString string) (*Claims, error)
Dependencies
Echo - High performance, minimalist Go web framework
JWT - JWT (JSON Web Tokens) implementation in Golang
License
This project is licensed under the MIT License. See the LICENSE file for details.
Credits