http://localhost:3300/auth
For all routes except signup, login, and refresh token, include the access token in the Authorization
header:
Authorization: Bearer <your_access_token>
Endpoint: /signup
Method: POST
Description: Creates a new user and sends an email verification link.
Request Body:
{
"email": "user@example.com",
"username": "user123",
"password": "password123",
"phone": "1234567890"
}
Response (200 OK):
{
"message": "Signup successful. Please check your email to verify your account."
}
Endpoint: /verify/:token
Method: GET
Description: Verifies the user's email using the token sent via email.
URL Parameter:
:token
- The email verification token.
Response (200 OK):
{
"message": "Email verified successfully."
}
Endpoint: /login
Method: POST
Description: Authenticates the user and returns access and refresh tokens.
Request Body:
{
"email": "user@example.com",
"password": "password123"
}
Response (200 OK):
{
"accessToken": "your_access_token_here",
"refreshToken": "your_refresh_token_here"
}
Endpoint: /request-password-reset
Method: POST
Description: Sends a password reset link to the user's email.
Request Body:
{
"email": "user@example.com"
}
Response (200 OK):
{
"message": "Password reset link has been sent to your email."
}
Endpoint: /reset-password
Method: POST
Description: Resets the user's password using the token from the reset link.
Request Body:
{
"token": "password_reset_token",
"newPassword": "newPassword123"
}
Response (200 OK):
{
"message": "Password reset successfully."
}
Endpoint: /refresh-token
Method: POST
Description: Generates a new access token using the refresh token.
Request Body:
{
"token": "your_refresh_token_here"
}
Response (200 OK):
{
"accessToken": "new_access_token_here"
}
Endpoint: /protected-route
Method: GET
Description: An example of an authenticated route that requires a valid access token.
Headers:
Authorization: Bearer <your_access_token>
Response (200 OK):
{
"message": "This is a protected resource."
}
If a token is expired or invalid, you will get the following error responses:
-
401 Unauthorized:
{ "message": "Token is required or invalid" }
-
403 Forbidden (invalid refresh token):
{ "message": "Invalid token" }
- Signup: User signs up and receives a verification email.
- Email Verification: User verifies their email.
- Login: User logs in and receives an
accessToken
andrefreshToken
. - Access Token Expiry: When the access token expires (after 15 minutes), the user sends the
refreshToken
to the/refresh-token
endpoint to get a newaccessToken
. - Password Reset: If a user forgets their password, they can request a reset link.
- Access Token: Short-lived (15 minutes).
- Refresh Token: Long-lived (7 days).
- For improved security, refresh tokens should be stored in an
HttpOnly
cookie or securely in local storage.