Go-JWT is a project that demonstrates user authentication using JSON Web Tokens (JWT) in a Go application with Gin framework.
The project consists of the following main components:
main.go: Contains the main entry point of the application where routes are defined and server initialization takes place.controllers/userController.go: Includes functions for user signup and login processes, interacting with the database and handling JWT token generation.middleware/authorize.go: Middleware for authorizing user requests based on JWT tokens.middleware/noAuthorize.go: Middleware for handling routes that do not require authorization.
- Ensure Go is installed on your machine.
- Clone the repository.
- Install dependencies by running
go mod tidy. - Set up the required environment variables.
- Run the application using
go run main.go.
- Signup: Endpoint
/signupallows users to create a new account by providing an email and password.
curl --location 'http://127.0.0.1:3030/signup' \
--header 'Content-Type: application/json' \
--data-raw '{
"email": "a@gmail.com",
"password": "password"
}'- Signin: Endpoint
/signinenables users to log in with their credentials and receive a JWT token for authentication.
curl --location 'http://127.0.0.1:3030/signin' \
--header 'Content-Type: application/json' \
--data-raw '{
"email": "a@gmail.com",
"password": "password"
}'- Home: Endpoint
/homeusers can see if the authentication is susccess.
curl --location 'http://127.0.0.1:3030/home'- Home: Endpoint
/signoutdelete the cookie and singout the user.
curl --location 'http://127.0.0.1:3030/signout'- Authorization: Middleware ensures that certain routes are only accessible with a valid JWT token.
github.com/gin-gonic/gin: Web framework used for building the API.github.com/golang-jwt/jwt/v5: Library for JWT token generation and validation.golang.org/x/crypto/bcrypt: Package for hashing passwords securely.
Feel free to contribute by forking the repository and submitting a pull request!