Client Authentication (client.auth)
The Client SDK provides a complete authentication flow for your frontend applications.
Introduction
import { AerostackClient } from '@aerostack/sdk';
const client = new AerostackClient({
projectSlug: 'my-project',
baseUrl: 'https://api.aerostack.dev'
});Features
Registration (register)
Create a new user account with email and password.
try {
const { user, token } = await client.auth.register({
email: '[email protected]',
password: 'securePassword123!',
name: 'New User'
});
console.log('Registered user:', user);
} catch (error) {
if (error.code === 'AUTH_USER_EXISTS') {
// Handle existing user
}
}Login (login)
Authenticate with email and password.
const { user, token } = await client.auth.login('[email protected]', 'password');
// Save `token` securely (e.g., HTTP-only cookie or secure storage)Passwordless / OTP (sendOTP, verifyOTP)
Login using a one-time code sent to email.
// 1. Request OTP
await client.auth.sendOTP('[email protected]');
// 2. Verify Code
const { user, token } = await client.auth.verifyOTP('[email protected]', '123456');Password Reset Flow (requestPasswordReset, resetPassword)
Recover forgotten passwords securely.
// 1. User requests reset link
await client.auth.requestPasswordReset('[email protected]');
// 2. User clicks link with token -> Frontend page calls reset
await client.auth.resetPassword('reset-token-from-url', 'newPassword456!');Session Management
Refresh tokens and logout.
// Refresh access token
const newSession = await client.auth.refreshToken(refreshToken);
// Logout (invalidate session)
await client.auth.logout(accessToken);User Profile
Get and update the authenticated user’s profile.
// Get current user
const profile = await client.auth.getCurrentUser(token);
// Update profile
const updatedProfile = await client.auth.updateProfile(token, {
name: 'Updated Name'
});Social Auth (Google, GitHub) and MFA are coming soon.
Error Handling
Client SDK errors include helpful suggestions for end-users.
import { ClientError } from '@aerostack/sdk';
try {
await client.auth.login(...);
} catch (err) {
if (err instanceof ClientError) {
// structured error
console.log(err.code); // 'AUTH_INVALID_CREDENTIALS'
// show user-friendly message
alert(err.details.suggestion); // "Double-check your email and password"
}
}