Pular para o conteúdo principal

Authentication API

Endpoints for user authentication, session management, and context switching.

Validate Company

Check if a company slug exists before showing the login form.

GET/v1/auth/validate-company

Query Parameters:

ParameterTypeRequiredDescription
slugstringYesCompany slug to validate

Example Request:

curl "https://api.console.solucao42.com.br/v1/auth/validate-company?slug=acme-corp"

Response:

{
"exists": true
}

Login

Authenticate with email and password.

POST/v1/auth/login

Request Body:

FieldTypeRequiredDescription
company_slugstringYesCompany identifier
emailstringYesUser email
passwordstringYesUser password

Example Request:

curl -X POST https://api.console.solucao42.com.br/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"company_slug": "acme-corp",
"email": "[email protected]",
"password": "SecurePassword123!"
}'

Response: 200 OK

{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"expires_in": 604800,
"user": {
"_id": "507f1f77bcf86cd799439011",
"email": "[email protected]",
"name": "John Doe",
"company_id": "507f1f77bcf86cd799439012"
}
}

Errors:

StatusError CodeDescription
400INVALID_CREDENTIALSWrong email or password
403FORBIDDENUser not authorized for this company
404COMPANY_NOT_FOUNDCompany slug doesn't exist
422VALIDATION_ERRORInvalid request body

Login with Google

Authenticate using a Google OAuth token.

POST/v1/auth/google

Request Body:

FieldTypeRequiredDescription
company_slugstringYesCompany identifier
google_tokenstringYesGoogle OAuth token

Example Request:

curl -X POST https://api.console.solucao42.com.br/v1/auth/google \
-H "Content-Type: application/json" \
-d '{
"company_slug": "acme-corp",
"google_token": "ya29.a0AfH6SMBx..."
}'

Response: Same as email/password login.


Get Current User

Returns the authenticated user's profile and current context.

GET/v1/auth/me

Headers:

HeaderRequiredDescription
AuthorizationYesBearer <token>

Example Request:

curl https://api.console.solucao42.com.br/v1/auth/me \
-H "Authorization: Bearer YOUR_TOKEN"

Response: 200 OK

{
"user": {
"_id": "507f1f77bcf86cd799439011",
"email": "[email protected]",
"name": "John Doe",
"company_id": "507f1f77bcf86cd799439012"
},
"context": {
"company_id": "507f1f77bcf86cd799439012"
}
}

Errors:

StatusError CodeDescription
401UNAUTHORIZEDToken invalid or expired

Passwordless Login

Authenticate using a temporary code sent via email.

Request Login Code

POST/v1/auth/passwordless/request

Request Body:

FieldTypeRequiredDescription
company_slugstringYesCompany identifier
emailstringYesUser email

Response: 200 OK with { "message": "..." }

Verify Login Code

POST/v1/auth/passwordless/verify

Request Body:

FieldTypeRequiredDescription
company_slugstringYesCompany identifier
emailstringYesUser email
codestringYes6-digit code from email

Response: Same as standard login.


Multi-Factor Authentication (2FA)

Verify 2FA Login

Verify a TOTP token during the login flow.

POST/v1/auth/2fa/login

Request Body:

FieldTypeRequiredDescription
pending_2fa_tokenstringYesToken from initial login response
totp_tokenstringNo6-digit code
backup_codestringNoOne-time recovery code

Single Sign-On (SSO)

Start SSO Flow

Initiate the redirect to the Identity Provider.

GET/v1/auth/sso/:company_slug/start

Query Parameters:

ParameterTypeRequiredDescription
redirect_uristringYesWhere to return after IdP auth

Response: 200 OK with { "url": "https://idp.com/auth/..." }

SSO Callback

Endpoint used by the IdP to return the user to the platform.

GET/POST/v1/auth/sso/:company_slug/callback

Parameters: Standard SAML or OIDC parameters (code, state, SAMLResponse).


Logout

End the current session.

POST/v1/auth/logout

Headers:

HeaderRequiredDescription
AuthorizationNoBearer <token> (optional)

Example Request:

curl -X POST https://api.console.solucao42.com.br/v1/auth/logout \
-H "Authorization: Bearer YOUR_TOKEN"

Response: 200 OK

{
"success": true
}
observação

The frontend should remove the token from storage after calling this endpoint.


JWT Token Structure

When decoded, the JWT payload contains:

{
"sub": "507f1f77bcf86cd799439011",
"user_id": "507f1f77bcf86cd799439011",
"company_id": "507f1f77bcf86cd799439012",
"email": "[email protected]",
"is_owner": false,
"iss": "solucao42-console-api",
"aud": "https://console.solucao42.com.br",
"jti": "unique-token-id",
"iat": 1709216800,
"exp": 1709821600
}
ClaimDescription
subSubject (user ID)
user_idUser identifier
company_idCompany (tenant) identifier
emailUser's email
is_ownerWhether user is company owner
issToken issuer
audToken audience
jtiUnique token ID
iatIssued at (Unix timestamp)
expExpires at (Unix timestamp)

Code Examples

async function login(companySlug, email, password) {
const response = await fetch('https://api.console.solucao42.com.br/v1/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ company_slug: companySlug, email, password }),
});

if (!response.ok) {
const error = await response.json();
throw new Error(error.error || 'Login failed');
}

const data = await response.json();
sessionStorage.setItem('s42_auth_token', data.token);
return data.user;
}

async function getMe(token) {
const response = await fetch('https://api.console.solucao42.com.br/v1/auth/me', {
headers: { 'Authorization': `Bearer ${token}` },
});
return response.json();
}