A simple Go application that demonstrates user authentication using JWT (JSON Web Tokens).
- User Signup: Allows new users to register.
- User Login: Authenticates users and provides a JWT token.
- Token Validation: Validates the provided JWT token.
-
Environment Variables: Ensure you have a
.env
file with the necessary environment variables. The application usesgodotenv
to load these variables. -
Database Connection: The application connects to a PostgreSQL database. The connection string is fetched from the environment variable
DB
. -
Database Synchronization: The application uses GORM for ORM and automatically migrates the user model to the database.
-
Signup:
POST /signup
- Payload:
{ "Email": "example@email.com", "Password": "your_password" }
- Payload:
-
Login:
POST /login
- Payload:
{ "Email": "example@email.com", "Password": "your_password" }
- Response: Returns a JWT token.
- Payload:
-
Validate:
GET /validate
- Requires JWT token in the
Authorization
cookie. Validates the token and returns the associated user.
- Requires JWT token in the
- RequireAuth: This middleware checks for the
Authorization
cookie in the request, decodes the JWT token, validates it, and attaches the associated user to the request.
- User Model: Represents a user with fields
Email
andPassword
. The email is unique for each user.