@proveanything/smartlinks-auth-ui
Version:
Lightweight React authentication UI components with bearer token support and Smartlinks SDK integration
142 lines (141 loc) • 4.1 kB
JavaScript
/**
* Smartlinks SDK Integration
* Wraps @proveanything/smartlinks for authenticated API calls
*/
/**
* Initialize Smartlinks SDK wrapper
* This provides authenticated API calls using the bearer token from auth
*/
export class SmartlinksClient {
constructor(config) {
this.config = config;
// Initialize the Smartlinks SDK
// Assuming the SDK export looks like: import Smartlinks from '@proveanything/smartlinks'
try {
// Dynamic import to avoid build errors if SDK not installed
// In production, this would be a direct import
// import Smartlinks from '@proveanything/smartlinks';
// this.sdk = new Smartlinks(config);
console.log('Smartlinks SDK initialized with config:', {
apiEndpoint: config.apiEndpoint,
clientId: config.clientId,
});
}
catch (error) {
console.warn('Smartlinks SDK not available:', error);
}
}
/**
* Set authentication token for subsequent API calls
*/
setToken(token) {
this.config.bearerToken = token;
if (this.sdk?.setToken) {
this.sdk.setToken(token);
}
}
/**
* Clear authentication token
*/
clearToken() {
this.config.bearerToken = undefined;
if (this.sdk?.clearToken) {
this.sdk.clearToken();
}
}
/**
* Get current bearer token
*/
getToken() {
if (this.sdk?.getToken) {
return this.sdk.getToken();
}
return this.config.bearerToken || null;
}
/**
* Make authenticated API request to Smartlinks backend
*/
async request(endpoint, method = 'GET', body, headers) {
const url = `${this.config.apiEndpoint}${endpoint}`;
const requestHeaders = {
'Content-Type': 'application/json',
...(this.config.bearerToken && {
'Authorization': `Bearer ${this.config.bearerToken}`,
}),
...(this.config.clientId && {
'X-Client-ID': this.config.clientId,
}),
...headers,
};
// Use SDK if available
if (this.sdk?.request) {
return this.sdk.request(endpoint, {
method,
headers: requestHeaders,
body: body ? JSON.stringify(body) : undefined,
});
}
// Fallback to fetch
const response = await fetch(url, {
method,
headers: requestHeaders,
body: body ? JSON.stringify(body) : undefined,
});
if (!response.ok) {
const error = await response.json().catch(() => ({
message: `HTTP ${response.status}`
}));
throw new Error(error.message || `Request failed: ${response.status}`);
}
return response.json();
}
/**
* Get current user profile from Smartlinks
*/
async getCurrentUser() {
return this.request('/user/me');
}
/**
* Update user profile
*/
async updateProfile(updates) {
return this.request('/user/me', 'PATCH', updates);
}
/**
* Get user's account data
*/
async getAccountData() {
return this.request('/account/me');
}
/**
* Update account data
*/
async updateAccountData(updates) {
return this.request('/account/me', 'PATCH', updates);
}
/**
* Refresh authentication token
*/
async refreshToken() {
const response = await this.request('/auth/refresh', 'POST');
this.setToken(response.token);
return response.token;
}
/**
* Logout and invalidate token on server
*/
async logout() {
try {
await this.request('/auth/logout', 'POST');
}
finally {
this.clearToken();
}
}
}
/**
* Create Smartlinks client instance
*/
export function createSmartlinksClient(config) {
return new SmartlinksClient(config);
}