Authentication Integration Guide
This guide shows how to implement authentication in your application using the Console API.
React Example
import { useState, useEffect } from 'react';
const API_URL = 'https://api.console.solucao42.com.br/v1';
// Login function
async function login(companySlug, email, password) {
const response = await fetch(`${API_URL}/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ company_slug: companySlug, email, password })
});
const data = await response.json();
// Store tokens
localStorage.setItem('access_token', data.access_token);
localStorage.setItem('refresh_token', data.refresh_token);
return data.user;
}
// Refresh token
async function refreshToken() {
const refresh_token = localStorage.getItem('refresh_token');
const response = await fetch(`${API_URL}/auth/refresh`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ refresh_token })
});
const data = await response.json();
localStorage.setItem('access_token', data.access_token);
localStorage.setItem('refresh_token', data.refresh_token);
return data.access_token;
}
// API client with auto-refresh
async function apiRequest(endpoint, options = {}) {
let token = localStorage.getItem('access_token');
const response = await fetch(`${API_URL}${endpoint}`, {
...options,
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
...options.headers,
},
});
// If 401, try refreshing token
if (response.status === 401) {
token = await refreshToken();
return apiRequest(endpoint, options);
}
return response.json();
}
Node.js Example
See API Reference: Authentication for more details.