The TypeScript Auth SDK enables you to authenticate users without requiring a particular frontend framework.

Getting Started

1. Install the SDK

Install the SDK with your preferred package manager.

npm install @microapp-io/auth

2. Import and Initialize the Auth SDK

To use the Auth SDK in your code, import it in any file where you need to use it.

import { Auth } from '@microapp-io/auth';

const auth = new Auth();

For local development, we recommend using the sandbox feature on the Auth class to return mocked users:

import { Auth } from '@microapp-io/auth';

const auth = new Auth({
  sandbox: {
    enabled: process.env.NODE_ENV !== 'production',
    user: { id: '1', email: '[email protected]', pictureUrl: 'https://example.com/avatar.png' },
  },
});

The sandbox prop should only be used for local development. Do not use it in production.

3. Check if the User is Authenticated

The isAuthenticated method returns the user’s authentication status: true if authenticated and false if unauthenticated.

import { Auth } from '@microapp-io/auth';

const isAuthenticated = await auth.isAuthenticated();

if (isAuthenticated) {
  console.log('User is authenticated');
} else {
  console.log('User is not authenticated');
}

4. Get the User’s Profile Information

The getUser method returns the user’s profile information.

import { Auth } from '@microapp-io/auth';

const user = await auth.getUser();

console.log('User Profile:', user);

3. Prompt the User to Log In

If the user is not authenticated, you can prompt them to authenticate by calling the requestLogin method.

import { Auth } from '@microapp-io/auth';

await auth.requestLogin();

5. Get Notified when the User is Authenticated

You can get notified when the user authenticates using onUserAuthenticated method.

import { Auth } from '@microapp-io/auth';

const unsubscribeCallback = auth.onUserAuthenticated((user) => {
  console.log(`User just authenticated`, user);
});

await auth.requestLogin();

// Unsubscribe the callback
unsubscribeCallback();