import { HttpService, UserApi } from 'authsafe-ts';
const user = await oauthService.getUser();
const httpService = new HttpService(user?.access_token);
const userApi = new UserApi(httpService);await userApi.getUser(userId: string): Promise<UserProfile>interface UserProfile {
id: string;
email: string;
name?: string;
picture?: string;
email_verified: boolean;
created_at: string;
updated_at: string;
metadata?: Record<string, any>;
}const profile = await userApi.getUser('user-123');
console.log('User:', profile.name, profile.email);await userApi.updateUser(userId: string, data: Partial<UserProfile>): Promise<UserProfile>const updated = await userApi.updateUser('user-123', {
name: 'John Doe',
metadata: { company: 'Acme Corp' },
});await userApi.changePassword(
userId: string,
currentPassword: string,
newPassword: string
): Promise<{ success: boolean; message: string }>try {
const result = await userApi.changePassword(
'user-123',
'currentPass123',
'newSecurePass456',
);
if (result.success) {
console.log('Password changed successfully');
}
} catch (error) {
console.error('Password change failed:', error.message);
}await userApi.deleteUser(userId: string): Promise<{ success: boolean }>const result = await userApi.deleteUser('user-123');
if (result.success) {
console.log('Account deleted');
await oauthService.signoutRedirect();
}await userApi.listUsers(params?: {
page?: number;
limit?: number;
search?: string;
}): Promise<{
users: UserProfile[];
total: number;
page: number;
limit: number;
}>const { users, total } = await userApi.listUsers({
page: 1,
limit: 20,
search: 'john',
});
console.log(`Found ${total} users`);import { OAuthService, HttpService, UserApi } from 'authsafe-ts';
// Initialize
const oauthService = new OAuthService({ clientId, redirectUri });
const user = await oauthService.getUser();
const httpService = new HttpService(user?.access_token);
const userApi = new UserApi(httpService);
// Get current user profile
const userId = user?.profile.sub;
const profile = await userApi.getUser(userId);
console.log('Current profile:', profile);
// Update profile
const updated = await userApi.updateUser(userId, {
name: 'Jane Smith',
metadata: {
department: 'Engineering',
role: 'Developer',
},
});
console.log('Updated profile:', updated);async function handlePasswordChange(currentPwd: string, newPwd: string) {
const user = await oauthService.getUser();
const httpService = new HttpService(user?.access_token);
const userApi = new UserApi(httpService);
try {
const result = await userApi.changePassword(
user.profile.sub,
currentPwd,
newPwd,
);
if (result.success) {
alert('Password changed successfully');
}
} catch (error) {
if (error.message.includes('incorrect')) {
alert('Current password is incorrect');
} else {
alert('Password change failed');
}
}
}async function deleteAccount() {
const confirmed = confirm('Are you sure? This action cannot be undone.');
if (!confirmed) return;
const user = await oauthService.getUser();
const httpService = new HttpService(user?.access_token);
const userApi = new UserApi(httpService);
try {
await userApi.deleteUser(user.profile.sub);
// Logout after deletion
await oauthService.signoutRedirect();
} catch (error) {
console.error('Account deletion failed:', error);
}
}