# API Palace

# Login Endpoint - Español

### Descripción

Este endpoint autentica a los usuarios validando un token de acceso de Auth0 y un correo electrónico codificado en Base64. Si la autenticación es exitosa, genera un JWT para la sesión del usuario y una URL que permitira al usuario ser redirigido a la aplicación.

### URL

`POST /api/login`

### Autenticación

**Token Requerido**: Access token de Auth0

- **Obtener el Token**: Debes registrar tu aplicación con Auth0 y configurarla para utilizar el flujo de autorización que mejor se adapte a tus necesidades (por ejemplo, Client Credentials, Authorization Code, etc.). El token de acceso debe solicitarse a Auth0 utilizando el `client_id`, `client_secret` y los scopes adecuados que permitan acceder a este endpoint.
- **Uso del Token**: El token de acceso debe ser incluido en todas las solicitudes al endpoint de login en el header de autorización como un Bearer Token.

### Headers

<table border="1" id="bkmrk-key-value-descriptio" style="border-collapse: collapse; width: 100%;"><thead><tr><th style="width: 25.5066%;">Key</th><th style="width: 27.192%;">Value</th><th style="width: 47.4207%;">Description</th></tr></thead><tbody><tr><td style="width: 25.5066%;">Authorization</td><td style="width: 27.192%;">Bearer {token}</td><td style="width: 47.4207%;">Token de acceso de Auth0 necesario para la autenticación.</td></tr></tbody></table>

### Parámetros del cuerpo (Body)

<table border="1" id="bkmrk-par%C3%A1metro-requerido-" style="border-collapse: collapse; width: 100%;"><thead><tr><th style="width: 25.9833%;">Parámetro</th><th style="width: 11.323%;">Requerido</th><th style="width: 7.98371%;">Tipo</th><th style="width: 54.71%;">Descripción</th></tr></thead><tbody><tr><td style="width: 25.9833%;">email</td><td style="width: 11.323%;">Sí</td><td style="width: 7.98371%;">String</td><td style="width: 54.71%;">Correo electrónico del usuario codificado en base64.</td></tr><tr><td style="width: 25.9833%;">redirect\_url</td><td style="width: 11.323%;">Sí</td><td style="width: 7.98371%;">String</td><td style="width: 54.71%;">URL a la que se redirigirá al usuario si el token del callback está vencido o es inválido.</td></tr></tbody></table>

### Ejemplo de Petición

```bash
curl -X POST "https://yourdomain.com/api/login" \
-H "Authorization: Bearer {auth0_access_token}" \
-H "Content-Type: application/json" \
-d '{"email": "dXNlckBleGFtcGxlLmNvbQ==", "redirect_url": "https://yourdomain.com/login"}'
```

### Respuestas

#### Success Response

**Código**: 200 OK

**Contenido del cuerpo**:

```json
{
  "status": "success",
  "url": "https://yourdomain.com/site/callback?token=jwt_token_here&redirect_url=url_to_redirect",
  "token": "jwt_token_here",
  "expires_in": 1718082360,
  "message": "User logged in"
}
```

**Descripción**: La respuesta incluye el estado de la operación, un mensaje descriptivo, y una URL con el JWT generado y la url de redirección en caso de error como parámetro.

#### Error Responses

**Código**: 401 Unauthorized

**Contenido del cuerpo**:

```json
{
  "status": "error",
  "message": "Unauthorized or invalid token"
}
```

**Descripción**: Se retorna cuando el token de Auth0 no es válido o ha expirado.

**Código**: 400 Bad Request

**Contenido del cuerpo**:

```json
{
  "status": "error",
  "message": "Email is required"
}
```

**Descripción**: Se retorna cuando no se ha incluido el correo electrónico en la solicitud.

### Datos de Respuesta

<table border="1" id="bkmrk-campo-tipo-descripci" style="border-collapse: collapse; width: 100%;"><thead><tr><th style="width: 15.3821%;">Campo</th><th style="width: 22.1626%;">Tipo</th><th style="width: 62.4553%;">Descripción</th></tr></thead><tbody><tr><td style="width: 15.3821%;">status</td><td style="width: 22.1626%;">string</td><td style="width: 62.4553%;">Estado de la respuesta, puede ser "success" o "error".</td></tr><tr><td style="width: 15.3821%;">url</td><td style="width: 22.1626%;">string</td><td style="width: 62.4553%;">URL a la que el cliente puede ser redirigido, incluye el JWT en respuestas exitosas y la url de redireccion en caso que el callback genere error</td></tr><tr><td style="width: 15.3821%;">token</td><td style="width: 22.1626%;">string</td><td style="width: 62.4553%;">El JWT generado para la sesión del usuario.</td></tr><tr><td style="width: 15.3821%;">expires\_in</td><td style="width: 22.1626%;">int</td><td style="width: 62.4553%;">Tiempo de expiración del token en segundos.</td></tr><tr><td style="width: 15.3821%;">message</td><td style="width: 22.1626%;">string</td><td style="width: 62.4553%;">Mensaje descriptivo sobre el resultado de la operación.</td></tr></tbody></table>

### Tabla de Posibles Valores de Error en Message

<table border="1" id="bkmrk-c%C3%B3digo-mensaje-descr" style="width: 100%; height: 208.578px; border-collapse: collapse; border-style: solid;"><thead><tr style="height: 29.7969px;"><th style="width: 8.93921%;">Código</th><th style="width: 27.6483%;">Mensaje</th><th style="width: 63.4125%;">Descripción</th></tr></thead><tbody><tr style="height: 29.7969px;"><td style="width: 8.93921%;">401</td><td style="width: 27.6483%;">Unauthorized or invalid token</td><td style="width: 63.4125%;">Indica que el token de Auth0 proporcionado es inválido o está vencido.</td></tr><tr style="height: 29.7969px;"><td style="width: 8.93921%;">400</td><td style="width: 27.6483%;">Email is required</td><td style="width: 63.4125%;">No se proporcionó un correo electrónico en el cuerpo de la solicitud.</td></tr><tr style="height: 29.7969px;"><td style="width: 8.93921%;">400</td><td style="width: 27.6483%;">Username invalid</td><td style="width: 63.4125%;">El nombre de usuario no existe en el sistema.</td></tr><tr style="height: 29.7969px;"><td style="width: 8.93921%;">400</td><td style="width: 27.6483%;">User doesn't have any licence</td><td style="width: 63.4125%;">El usuario no tiene la licencia necesaria.</td></tr><tr style="height: 29.7969px;"><td style="width: 8.93921%;">400</td><td style="width: 27.6483%;">User doesn't have a profile</td><td style="width: 63.4125%;">El usuario no tiene un perfil asociado.</td></tr><tr style="height: 29.7969px;"><td style="width: 8.93921%;">400</td><td style="width: 27.6483%;">Unknown error</td><td style="width: 63.4125%;">Ocurrió un error no especificado.</td></tr></tbody></table>

###   

# Login Endpoint - English

### Description

This endpoint authenticates users by validating an Auth0 access token and an email encoded in Base64. If the authentication is successful, it generates a JWT for the user's session and a URL that will allow the user to be redirected to the application.

### URL

**POST** `/api/login`

### Authentication

**Required Token**: Auth0 access token

- **Obtaining the Token**: You must register your application with Auth0 and configure it to use the authorization flow that best suits your needs (e.g., Client Credentials, Authorization Code, etc.). The access token should be requested from Auth0 using the `client_id`, `client_secret`, and appropriate scopes that allow access to this endpoint.
- **Using the Token**: The access token must be included in all requests to the login endpoint in the authorization header as a Bearer Token.

### Headers

<table border="1" id="bkmrk-key-value-descriptio" style="border-collapse: collapse; width: 100%;"><thead><tr><th>Key</th><th>Value</th><th>Description</th></tr></thead><tbody><tr><td>Authorization</td><td>`Bearer {token}`</td><td>Auth0 access token required for authentication.</td></tr></tbody></table>

### Body Parameters

<table border="1" id="bkmrk-parameter-required-t" style="width: 100%; border-collapse: collapse;"><thead><tr style="height: 29.7969px;"><th style="width: 14.3383%; height: 29.7969px;">Parameter</th><th style="width: 9.94485%; height: 29.7969px;">Required</th><th style="width: 7.05065%; height: 29.7969px;">Type</th><th style="width: 68.5335%; height: 29.7969px;">Description</th></tr></thead><tbody><tr style="height: 29.7969px;"><td style="width: 14.3383%; height: 29.7969px;">`email`</td><td style="width: 9.94485%; height: 29.7969px;">Yes</td><td style="width: 7.05065%; height: 29.7969px;">string</td><td style="width: 68.5335%; height: 29.7969px;">The user's email encoded in Base64 format.</td></tr><tr style="height: 29.7969px;"><td style="width: 14.3383%; height: 29.7969px;">`redirect_url`</td><td style="width: 9.94485%; height: 29.7969px;">No</td><td style="width: 7.05065%; height: 29.7969px;">string</td><td style="width: 68.5335%; height: 29.7969px;">URL to which the user will be redirected if the callback token is expired or invalid.</td></tr></tbody></table>

### Request Example

```bash
curl -X POST "https://yourdomain.com/api/login" \
-H "Authorization: Bearer {auth0_access_token}" \
-H "Content-Type: application/json" \
-d '{"email": "dXNlckBleGFtcGxlLmNvbQ==", "redirect_url": "https://yourdomain.com/login"}'
```

### Success Response

**Code**: 200 OK

```json
{
  "status": "success",
  "url": "https://yourdomain.com/site/callback?token=jwt_token_here&redirect_url=url_to_redirect",
  "token": "jwt_token_here",
  "expires_in": 1718082360,
  "message": "User logged in"
}
```

**Description**: The response includes the status of the operation, a descriptive message, and a URL with the generated JWT and the redirect URL in case of an error as a parameter.

### Error Responses

- **401 Unauthorized** (with `redirect_url` provided):

If the Auth0 token is invalid or has expired, and a `redirect_url` is provided, the user is redirected to that URL.

- **400 Bad Request** (Email missing):

```json
{
  "status": "error",
  "message": "Email is required"
}
```

### Response Data

<table border="1" id="bkmrk-field-type-descripti" style="border-collapse: collapse; width: 100%;"><thead><tr><th style="width: 16.9249%;">Field</th><th style="width: 11.2496%;">Type</th><th style="width: 71.8254%;">Description</th></tr></thead><tbody><tr><td style="width: 16.9249%;">`status`</td><td style="width: 11.2496%;">string</td><td style="width: 71.8254%;">The status of the response, can be "success" or "error".</td></tr><tr><td style="width: 16.9249%;">`url`</td><td style="width: 11.2496%;">string</td><td style="width: 71.8254%;">URL to which the client may be redirected, includes the JWT in successful responses and the redirect URL in case the callback generates an error.</td></tr><tr><td style="width: 16.9249%;">`token`</td><td style="width: 11.2496%;">string</td><td style="width: 71.8254%;">The JWT generated for the user session.</td></tr><tr><td style="width: 16.9249%;">`expires_in`</td><td style="width: 11.2496%;">int</td><td style="width: 71.8254%;">Token expiration time in seconds.</td></tr><tr><td style="width: 16.9249%;">`message`</td><td style="width: 11.2496%;">string</td><td style="width: 71.8254%;">Descriptive message about the outcome of the operation.</td></tr></tbody></table>

### Possible Error Values

<table border="1" id="bkmrk-code-message-descrip" style="border-collapse: collapse; width: 100%; height: 225.375px;"><thead><tr style="height: 29.7969px;"><th style="width: 8.34327%; height: 29.7969px;">Code</th><th style="width: 26.9741%; height: 29.7969px;">Message</th><th style="width: 64.6826%; height: 29.7969px;">Description</th></tr></thead><tbody><tr style="height: 46.5938px;"><td style="width: 8.34327%; height: 46.5938px;">401</td><td style="width: 26.9741%; height: 46.5938px;">Unauthorized or invalid token</td><td style="width: 64.6826%; height: 46.5938px;">Indicates that the provided Auth0 token is invalid or expired. If `redirect_url` is provided, the user is redirected.</td></tr><tr style="height: 29.7969px;"><td style="width: 8.34327%; height: 29.7969px;">400</td><td style="width: 26.9741%; height: 29.7969px;">Email is required</td><td style="width: 64.6826%; height: 29.7969px;">Email was not provided in the request body.</td></tr><tr style="height: 29.7969px;"><td style="width: 8.34327%; height: 29.7969px;">400</td><td style="width: 26.9741%; height: 29.7969px;">Username invalid</td><td style="width: 64.6826%; height: 29.7969px;">The username does not exist in the system.</td></tr><tr style="height: 29.7969px;"><td style="width: 8.34327%; height: 29.7969px;">400</td><td style="width: 26.9741%; height: 29.7969px;">User doesn't have any licence</td><td style="width: 64.6826%; height: 29.7969px;">The user lacks the necessary licensing.</td></tr><tr style="height: 29.7969px;"><td style="width: 8.34327%; height: 29.7969px;">400</td><td style="width: 26.9741%; height: 29.7969px;">User doesn't have a profile</td><td style="width: 64.6826%; height: 29.7969px;">The user does not have an associated profile.</td></tr><tr style="height: 29.7969px;"><td style="width: 8.34327%; height: 29.7969px;">400</td><td style="width: 26.9741%; height: 29.7969px;">Unknown error</td><td style="width: 64.6826%; height: 29.7969px;">An unspecified error occurred.</td></tr></tbody></table>

This documentation now accurately reflects the behavior of the endpoint when dealing with expired or invalid Auth0 tokens, including the redirection to a specified URL if provided. This ensures a clear and comprehensive understanding for all potential users and developers interacting with the API.