Authentication

This guide covers the authentication mechanisms used in the NSGG Backend API.

JWT Authentication

The API uses JSON Web Tokens (JWT) for authentication. Each authenticated request must include a valid JWT token in the Authorization header.

Obtaining Tokens

POST /api/v1/auth/login/

Request Body:

{
    "email": "user@example.com",
    "password": "your_password"
}

Response:

{
    "access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
    "refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..."
}

Using Tokens

Include the access token in the Authorization header:

GET /api/v1/users/me/
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...

Refreshing Tokens

When the access token expires, use the refresh token to obtain a new one:

POST /api/v1/auth/refresh/

Request Body:

{
    "refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..."
}

Response:

{
    "access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..."
}

Registration

Create a new user account:

POST /api/v1/auth/register/

Request Body:

{
    "email": "user@example.com",
    "password": "secure_password",
    "first_name": "John",
    "last_name": "Doe"
}

Password Reset

Request Reset

POST /api/v1/auth/password/reset/

Request Body:

{
    "email": "user@example.com"
}

Confirm Reset

POST /api/v1/auth/password/reset/confirm/

Request Body:

{
    "token": "reset_token_from_email",
    "password": "new_password"
}

Error Responses

Invalid Credentials

{
    "detail": "No active account found with the given credentials",
    "code": "invalid_credentials"
}

Token Invalid/Expired

{
    "detail": "Token is invalid or expired",
    "code": "token_not_valid"
}

Rate Limiting

{
    "detail": "Request was throttled",
    "code": "throttled"
}

Security Considerations

  1. Token Storage
  2. Store tokens securely
  3. Never store in localStorage
  4. Use httpOnly cookies when possible

  5. Token Expiration

  6. Access tokens expire in 60 minutes
  7. Refresh tokens expire in 24 hours
  8. Implement proper token refresh logic

  9. HTTPS

  10. All authentication requests must use HTTPS
  11. Non-HTTPS requests will be rejected

  12. Rate Limiting

  13. Login attempts: 5 per minute
  14. Password reset: 3 per hour
  15. Token refresh: 10 per minute