Appearance
SDK Methods
Complete reference for all ExtensionLogin SDK methods.
Initialization
init(options)
Initialize the SDK with your API key.
javascript
ExtensionLogin.init({
apiKey: 'el_live_xxxxxx',
debug: false,
autoIdentify: false,
storage: 'local',
apiUrl: 'https://api.extensionlogin.com'
});Parameters:
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | Required | Your ExtensionLogin API key |
debug | boolean | false | Enable debug logging |
autoIdentify | boolean | false | Auto-restore user from storage |
storage | 'local' | 'sync' | 'none' | 'local' | Chrome storage type |
apiUrl | string | Production URL | API endpoint (for self-hosted) |
Returns: void
Example:
javascript
import ExtensionLogin from 'extensionlogin';
ExtensionLogin.init({
apiKey: process.env.NODE_ENV === 'production'
? 'el_live_xxx'
: 'el_test_xxx',
debug: process.env.NODE_ENV !== 'production',
autoIdentify: true
});User Methods
identify(userData)
Identify a user and capture their data.
javascript
const result = await ExtensionLogin.identify({
email: '[email protected]',
name: 'John Doe',
firstName: 'John',
lastName: 'Doe',
phone: '+1-555-123-4567',
metadata: { plan: 'pro' }
});Parameters:
| Field | Type | Required | Description |
|---|---|---|---|
email | string | Yes | User's email address |
name | string | No | Full name |
firstName | string | No | First name |
lastName | string | No | Last name |
phone | string | No | Phone number |
metadata | object | No | Custom key-value data |
Returns: Promise<IdentifyResult>
typescript
interface IdentifyResult {
success: boolean;
user?: User;
error?: {
code: string;
message: string;
};
crm?: {
[crmName: string]: {
success: boolean;
error?: string;
};
};
}Example:
javascript
const result = await ExtensionLogin.identify({
email: '[email protected]',
name: 'John Doe'
});
if (result.success) {
console.log('User ID:', result.user.id);
console.log('Email:', result.user.email);
} else {
console.error('Error:', result.error.message);
}getUser()
Get the currently identified user.
javascript
const user = ExtensionLogin.getUser();Returns: User | null
typescript
interface User {
id: string;
email: string;
name?: string;
firstName?: string;
lastName?: string;
phone?: string;
picture?: string; // Google OAuth only
googleId?: string; // Google OAuth only
metadata?: object;
createdAt: string;
updatedAt: string;
}Example:
javascript
const user = ExtensionLogin.getUser();
if (user) {
console.log(`Welcome, ${user.name || user.email}!`);
console.log('Member since:', new Date(user.createdAt).toLocaleDateString());
} else {
console.log('No user logged in');
}isAuthenticated()
Check if a user is currently identified.
javascript
const isLoggedIn = ExtensionLogin.isAuthenticated();Returns: boolean
Example:
javascript
if (ExtensionLogin.isAuthenticated()) {
showDashboard();
} else {
showLoginForm();
}logout()
Clear the current user session.
javascript
await ExtensionLogin.logout();Returns: Promise<void>
Example:
javascript
document.getElementById('logout-btn').addEventListener('click', async () => {
await ExtensionLogin.logout();
window.location.reload();
});Google OAuth
loginWithGoogle()
Initiate Google OAuth login flow.
javascript
const result = await ExtensionLogin.loginWithGoogle();Returns: Promise<LoginResult>
typescript
interface LoginResult {
success: boolean;
user?: User;
error?: {
code: string;
message: string;
};
}Example:
javascript
document.getElementById('google-btn').addEventListener('click', async () => {
const result = await ExtensionLogin.loginWithGoogle();
if (result.success) {
console.log('Logged in as:', result.user.email);
console.log('Profile picture:', result.user.picture);
} else {
if (result.error.code === 'USER_CANCELLED') {
console.log('User cancelled sign-in');
} else {
console.error('Login failed:', result.error.message);
}
}
});loginWithGoogleToken(token)
Login using an existing Google OAuth token.
javascript
const result = await ExtensionLogin.loginWithGoogleToken(token);Parameters:
| Field | Type | Description |
|---|---|---|
token | string | Google OAuth access token |
Returns: Promise<LoginResult>
Example:
javascript
// If you already have a token from Chrome identity API
chrome.identity.getAuthToken({ interactive: true }, async (token) => {
if (token) {
const result = await ExtensionLogin.loginWithGoogleToken(token);
if (result.success) {
console.log('User:', result.user);
}
}
});Events
onAuthChange(callback)
Listen for authentication state changes.
javascript
ExtensionLogin.onAuthChange((user) => {
// Called when user logs in or out
});Parameters:
| Field | Type | Description |
|---|---|---|
callback | (user: User | null) => void | Called with user or null |
Returns: () => void - Unsubscribe function
Example:
javascript
const unsubscribe = ExtensionLogin.onAuthChange((user) => {
if (user) {
console.log('User logged in:', user.email);
updateUI(user);
} else {
console.log('User logged out');
showLoginScreen();
}
});
// Later, to stop listening:
unsubscribe();on(event, callback)
Listen for specific SDK events.
javascript
ExtensionLogin.on('identify', (data) => {
console.log('User identified:', data.user);
});Parameters:
| Field | Type | Description |
|---|---|---|
event | string | Event name |
callback | function | Event handler |
Returns: () => void - Unsubscribe function
Events:
| Event | Data | Description |
|---|---|---|
init | { apiKey } | SDK initialized |
identify | { user, crm } | User identified |
logout | {} | User logged out |
error | { code, message } | Error occurred |
Example:
javascript
// Track all identifications
ExtensionLogin.on('identify', ({ user, crm }) => {
analytics.track('user_identified', {
userId: user.id,
email: user.email,
crmSuccess: Object.values(crm).every(c => c.success)
});
});
// Handle errors
ExtensionLogin.on('error', ({ code, message }) => {
errorReporting.capture(new Error(`${code}: ${message}`));
});off(event, callback)
Remove an event listener.
javascript
ExtensionLogin.off('identify', myCallback);Parameters:
| Field | Type | Description |
|---|---|---|
event | string | Event name |
callback | function | Handler to remove |
Utility Methods
getVersion()
Get the SDK version.
javascript
const version = ExtensionLogin.getVersion();
console.log(version); // "1.2.3"Returns: string
isInitialized()
Check if the SDK has been initialized.
javascript
const ready = ExtensionLogin.isInitialized();Returns: boolean
Example:
javascript
if (!ExtensionLogin.isInitialized()) {
ExtensionLogin.init({ apiKey: 'el_live_xxx' });
}reset()
Reset the SDK state (clears user and configuration).
javascript
ExtensionLogin.reset();Returns: void
WARNING
This clears all SDK state including the current user. Use logout() if you just want to log out the user.
TypeScript Types
Full TypeScript definitions are included:
typescript
import ExtensionLogin from 'extensionlogin';
import type {
InitOptions,
User,
IdentifyData,
IdentifyResult,
LoginResult,
StorageType
} from 'extensionlogin';
const options: InitOptions = {
apiKey: 'el_live_xxx',
debug: true
};
ExtensionLogin.init(options);
const userData: IdentifyData = {
email: '[email protected]',
name: 'John Doe'
};
const result: IdentifyResult = await ExtensionLogin.identify(userData);Next Steps
- Events - Detailed event reference
- Error Handling - Error codes and handling
- Examples - Real-world examples