Skip to content

User APIs

Sulabh Mehta (Dominik) edited this page Nov 10, 2023 · 2 revisions

Models

The following models are used in the project:

  • User: Represents a user in the system.

Controllers

The following controllers handle the incoming requests:

  • UserController: Handles requests related to users.

Services

The following methods are used in the user service:

GetUserByEmail

This service retrieves a user by their email address. It takes the user's email address as input and returns a pointer to a User object if the user exists, or nil otherwise.

GetUserByProviderAccountId

This service retrieves a user by their provider account ID. It takes the user's provider account ID as input and returns a pointer to a User object if the user exists, or nil otherwise.

InsertUser

This service inserts a new user into the database. It takes a User object as input and returns the user's ID if the user is successfully inserted, or an error otherwise.

UpdateUserByProviderAccountID

This service updates a user by their provider account ID. It takes a User object as input and updates the user in the database if the user exists. Otherwise, an error is returned.

Routes

The following routes are available:

  • /api/login: Creates a new user or updates an existing user.
  • /api/user: Retrieves a user by their email address.

Routes Documentation

Create or Update User Account upon Login

/api/login

Method: POST

This endpoint creates a new user or updates an existing user. The request body should contain a JSON object with the following fields:

  • username: The user's username.
  • name: The user's name.
  • email: The user's email address.
  • provider: The provider that the user authenticated with (e.g., Google, Facebook, etc.).
  • providerAccountId: The user's ID with the provider.

If the user does not already exist, a new user will be created. Otherwise, the existing user will be updated with the new information.

Request body:

{
  "username": "string",
  "name": "string",
  "email": "string",
  "role": "string",
  "provider": "string",
  "providerAccountId": "string"
}

Response body:

{
  "_id": "primitive.ObjectID",
  "username": "string",
  "name": "string",
  "email": "string",
  "role": "string",
  "provider": "string",
  "providerAccountId": "string"
}

Example Usage

To create a new user, send a POST request to the /api/login route with the following request body:

{
  "username": "johndoe",
  "name": "John Doe",
  "email": "john.doe@example.com",
  "role": "user",
  "provider": "google",
  "providerAccountId": "1234567890"
}

If the user is created successfully, the response body will contain the user's data.

Get User by Email

/api/user

Method: POST

This endpoint retrieves a user by their email address. The request body should contain a JSON object with the following field:

  • email: The user's email address.

If a user with the specified email address exists, the user will be returned in the response body. Otherwise, a 404 error will be returned.

Request body:

{
  "email": "string",
}

Response body:

{
  "_id": "primitive.ObjectID",
  "username": "string",
  "name": "string",
  "email": "string",
  "role": "string",
  "provider": "string",
  "providerAccountId": "string"
}

Example Usage

To retrieve a user by their email address, send a POST request to the /api/user route with the following request body:

{
    "email": "john.doe@example.com",
}

If the user is found, the response body will contain the user's data.

Clone this wiki locally