hngprojects / hng_boilerplate_nestjs

Description
Apache License 2.0
182 stars 105 forks source link

[FEAT] Create Endpoint to Store Session or User Cookies #197

Closed Heccubernny closed 1 month ago

Heccubernny commented 1 month ago

[FEAT] Integrate Session Management into Login Endpoint #197

Description

Implement backend functionality to support storing session or user cookies within the login endpoint, ensuring they are securely stored and applied correctly across the application.

Acceptance Criteria

Database Schema Update

API Endpoints Implementation

Session Management

Security and Performance

Documentation

Purpose

Provide backend infrastructure to store and manage user session details, enabling a secure user experience.

Requirements

Expected Outcome

Successfully store and apply user session details across the application, enabling personalized experiences, persistent login, and with accurate recall of user preferences, settings, and previous interactions.

Endpoints

Login and Store User Session

Description: Handle user login and store session details for a user.

POST /api/v1/auth/login

Content-Type: application/json

Request Body:

{
  "email": "wizkid@email.com",
  "password": "password123"
}

Response (Success): On successful login, the API should return a 200 OK status code. The response body should contain a success message:

{
  "status": "success",
  "status_code": 200,
  "message": "Login successful",
  "data": {
    "userId": "user123",
    "sessionId": "session456",
    "expiry": "2024-12-31T23:59:59.000Z"
  }
}

Response (Error): If there is an error logging the user in due to invalid login credentials, the API should return a 401 Unauthorized status code with an appropriate message:

{
  "status": "error",
  "message": "Invalid credentials",
  "statusCode": 401
}

If there is an error logging the user in (e.g., server down, network issues), the API should return a 500 Internal Server Error status code with an appropriate message:

{
  "status": "error",
  "message": "Internal server error",
  "statusCode": 500
}

Get User Session

Description: Get the current session details for a user.

GET /api/v1/session

Response (Success): On success, the API should return a 200 OK status code. The response body should contain a success message:

{
  "status": "success",
  "status_code": 200,
  "data": {
    "userId": "user123",
    "sessionId": "session456",
    "expiry": "2024-12-31T23:59:59.000Z"
  }
}

Response (Error): When the user does not have the necessary authorization to perform the requested action, the API should return a 401 Unauthorized status code with an appropriate message:

{
  "status": "error",
  "message": "You are not authorised for this action",
  "statusCode": 401
}

When the session ID is not found or has expired, the API should return a 401 Unauthorized status code with an appropriate message:

{
  "status": "error",
  "message": "Session not found or expired",
  "statusCode": 401
}

If there is an internal server error, the API should return a 500 Internal Server Error status code with an appropriate message:

{
  "status": "error",
  "message": "Internal server error",
  "statusCode": 500
}

Task Checklist

Testing

markessien commented 1 month ago

seems weird to store session cookies in an endpoint ither than login

Heccubernny commented 1 month ago

amendment made

highb33kay commented 1 month ago

@Heccubernny

"Successfully store and apply user session details across the different parts of the application."

Could you be a little more detailed?

Heccubernny commented 1 month ago

Okay

Heccubernny commented 1 month ago

@highb33kay done.

phurhard commented 1 month ago

@highb33kay please check this,

highb33kay commented 1 month ago

Like Mark mentioned why isnt this part of the login fuctionality?

Also this could be seesions management endpoint to Logout of logged in devices?