Areal API support multiple authentication mechanisms:
JWT over cookies (recommended)
JWT over Bearer
ApiKey over headers (on-demand)
MFA over Authenticator App (Google Authenticator, PingID, Authy, etc.)
Authentication Flow
Below is a high-level sequence diagram illustrating the main phases of the authentication lifecycle:
sequenceDiagram
participant User as 👤 User
participant Frontend as 🖥️ Areal Dashboard
participant API as 🖥️ Areal API
%% Phase 1: Login
rect rgb(220,255,220)
User->>Frontend: Enter credentials
Frontend->>API: POST /login (credentials)
API-->>Frontend: Set secure cookies (access & refresh tokens)
Frontend-->>User: Login success
end
%% Phase 2: Authenticated Request
rect rgb(200,220,255)
User->>Frontend: Perform action
Frontend->>API: Authenticated request (cookies sent automatically)
API-->>Frontend: Protected resource/data
end
%% Phase 3: Token Refresh
rect rgb(255,240,200)
Frontend->>API: Token expired, use refresh token (cookie)
API-->>Frontend: Issue new access token (cookie)
end
%% Phase 4: Logout
rect rgb(255,220,220)
User->>Frontend: Click logout
Frontend->>API: POST /logout
API-->>Frontend: Clear cookies
Frontend-->>User: Logged out
end
importrequestsBASE_URL='http://dev-api.v2.areal.ai/api/v2'# 0. Login - details in Authentication sectionlogin_response=requests.post(f'{BASE_URL}/accounts/login/',{'username':'test@areal.ai','password':'test123',})client=requests.Session()client.cookies.update({'access_token':login_response.cookies['access_token'],'refresh_token':login_response.cookies['refresh_token'],})# this client is now authenticated for the duration of access_token# after that you can refresh it using the /accounts/refresh endpoint
usingSystem;usingSystem.Net.Http;usingSystem.Threading.Tasks;usingSystem.Text;usingSystem.Text.Json;usingSystem.Net;varbaseUrl="http://dev-api.v2.areal.ai/api/v2";// 0. Login - details in Authentication sectionvarloginData=new{username="test@areal.ai",password="test123"};varhandler=newHttpClientHandler{UseCookies=true,CookieContainer=newCookieContainer()};varclient=newHttpClient(handler);varloginContent=newStringContent(JsonSerializer.Serialize(loginData),Encoding.UTF8,"application/json");varloginResponse=awaitclient.PostAsync($"{baseUrl}/accounts/login/",loginContent);loginResponse.EnsureSuccessStatusCode();// Cookies (access_token and refresh_token) are automatically stored in handler.CookieContainer// This client is now authenticated for the duration of access_token// after that you can refresh it using the /accounts/refresh endpoint
importjava.net.HttpURLConnection;importjava.net.URL;importjava.io.OutputStream;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.net.CookieManager;importjava.net.CookieHandler;importjava.net.HttpCookie;importjava.nio.charset.StandardCharsets;publicclassLogin{publicstaticvoidmain(String[]args)throwsException{StringbaseUrl="http://dev-api.v2.areal.ai/api/v2";// 0. Login - details in Authentication sectionCookieManagercookieManager=newCookieManager();CookieHandler.setDefault(cookieManager);URLurl=newURL(baseUrl+"/accounts/login/");HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();connection.setRequestMethod("POST");connection.setRequestProperty("Content-Type","application/json");connection.setDoOutput(true);StringloginData="{\"username\":\"test@areal.ai\",\"password\":\"test123\"}";try(OutputStreamos=connection.getOutputStream()){byte[]input=loginData.getBytes(StandardCharsets.UTF_8);os.write(input,0,input.length);}intresponseCode=connection.getResponseCode();if(responseCode==HttpURLConnection.HTTP_OK){// Cookies are automatically stored in the CookieManager// this client is now authenticated for the duration of access_token// after that you can refresh it using the /accounts/refresh endpoint}connection.disconnect();}}
Security Highlights
Secure Login: Credentials are never stored or transmitted in plain text.
Token-Based Authentication: Access and refresh tokens are used to manage sessions securely.
Cookie Usage: Secure, HTTP-only cookies are used to store tokens, protecting them from XSS attacks.
Session Refresh: Seamless token refresh ensures uninterrupted user experience.
Logout: Users can securely terminate their sessions at any time.
Best-Practice Security: All authentication flows use encryption, secure cookie flags, and protection against common web vulnerabilities.