@proveanything/smartlinks-auth-ui
Version:
Lightweight React authentication UI components with bearer token support and Smartlinks SDK integration
106 lines (105 loc) • 3.56 kB
JavaScript
export class AuthAPI {
constructor(apiEndpoint, clientId, clientName) {
this.baseUrl = apiEndpoint.replace(/\/$/, '');
this.clientId = clientId;
this.clientName = clientName;
}
buildUrl(path) {
return `${this.baseUrl}/api/v1/authkit/${this.clientId}${path}`;
}
async request(path, method = 'GET', body) {
const url = this.buildUrl(path);
const response = await fetch(url, {
method,
headers: {
'Content-Type': 'application/json',
'ngrok-skip-browser-warning': 'true',
},
body: body ? JSON.stringify(body) : undefined,
});
if (!response.ok) {
const error = await response.json().catch(() => ({ message: 'Request failed' }));
throw new Error(error.message || `HTTP ${response.status}`);
}
return response.json();
}
async login(email, password) {
return this.request('/auth/login', 'POST', { email, password });
}
async register(data) {
// Include clientId, redirectUrl, and clientName in registration
return this.request('/auth/register', 'POST', {
...data,
clientId: this.clientId,
clientName: this.clientName
});
}
async loginWithGoogle(idToken) {
return this.request('/auth/google', 'POST', { idToken });
}
async sendPhoneCode(phoneNumber) {
return this.request('/auth/phone/send-code', 'POST', { phoneNumber });
}
async verifyPhoneCode(verificationId, code) {
return this.request('/auth/phone/verify', 'POST', {
verificationId,
code,
});
}
// Custom Email Flow Methods (using Resend)
async requestPasswordReset(email, redirectUrl) {
return this.request('/auth/reset-password', 'POST', {
email,
redirectUrl,
clientName: this.clientName
});
}
async verifyResetToken(token) {
return this.request('/auth/verify-reset-token', 'POST', { token });
}
async completePasswordReset(token, newPassword) {
return this.request('/auth/complete-reset', 'POST', {
token,
newPassword
});
}
async sendEmailVerification(userId, email, redirectUrl) {
return this.request('/auth/send-verification', 'POST', {
userId,
email,
redirectUrl,
clientName: this.clientName
});
}
async verifyEmailWithToken(token) {
return this.request('/auth/verify-email', 'POST', { token });
}
async resendVerification(userId, email, redirectUrl) {
return this.request('/auth/resend-verification', 'POST', {
userId,
email,
redirectUrl,
clientName: this.clientName
});
}
// UI Configuration
async fetchConfig() {
try {
return await this.request('/config', 'GET');
}
catch (error) {
console.warn('Failed to fetch UI config, using defaults:', error);
return {
branding: {
title: 'Smartlinks Auth',
subtitle: 'Sign in to your account',
primaryColor: '#3B82F6',
secondaryColor: '#1D4ED8',
backgroundColor: '#e0f2fe',
buttonStyle: 'rounded',
fontFamily: 'Inter, sans-serif'
}
};
}
}
}