This is a simple JWT authentication API written in Go. It allows users to sign up, sign in, and view their user profile by providing a JWT token in the request headers.
- User sign-up: Users can register by providing an email and password.
- User sign-in: Users can authenticate by providing their registered email and password, and receive a JWT token.
- User profile: Authenticated users can view their user profile by providing the JWT token in the request headers.
-
Clone this repository:
-
Navigate to the project directory:
-
Install dependencies:
go mod tidy
-
Build and run the server:
go run jwtExample.go
The server will start running at
http://localhost:8080
.
-
POST /signup
: Register a new user.- Request Body: JSON object with
email
andpassword
fields. - Response:
200 OK
on success,400 Bad Request
if the user already exists, or if the request body is invalid.
- Request Body: JSON object with
-
POST /signin
: Authenticate a user and generate a JWT token.- Request Body: JSON object with
email
andpassword
fields. - Response:
200 OK
with a JWT token on success,401 Unauthorized
if the email or password is incorrect.
- Request Body: JSON object with
-
GET /userProfile
: Get the user profile.- Request Headers:
Token
with the JWT token obtained after sign-in. - Response:
200 OK
with the user profile JSON containing the email,401 Unauthorized
if the token is missing or invalid.
- Request Headers:
- github.com/golang-jwt/jwt: Go implementation of JSON Web Tokens (JWT).