UNPKG

@bit-ui-libs/common

Version:
88 lines (78 loc) 2.79 kB
import jwtDecode from 'jwt-decode'; import { LoggerType } from '../common'; import { BiometricsModeEnum, BiometricsStorageKeyEnum } from './enums'; import { IIncodeService, IStorage, IncodeInitializeOptions } from './interfaces'; export type BiometricsServiceOptions = IncodeInitializeOptions & { /** * Web and Mobile have different implementations of IncodeSdk. * * Based on the platform being used, * the appropriate service class needs to be passed here. */ incodeService: IIncodeService; /** * Web and Mobile have different libraries / services * for storing persistent data in a key/value store. * * Based on the platform being used, * the asessionTokenppropriate storage instance needs to be passed here. */ storage: IStorage; /** Optional `LoggerType` object to be able to see our debug / info logs */ logger?: LoggerType; } export interface SignInOptions { /** * Session Token from BIT Authentication flow. * * If this is passed, we try the following: * - If user's `email` matches email persisted from the previous session, * - */ sessionToken?: string; } export class BiometricsService { /** Platform-specific Incode service class */ public incode: IIncodeService; /** Platform-specific storage (e.g. `localStorage`, `EncryptedStorage`) */ private storage?: IStorage; // Local variables private mode: BiometricsModeEnum | null = null; // Incode-related variables private sessionToken?: string; private logger?: LoggerType; constructor(private options: BiometricsServiceOptions) { this.logger = options.logger; this.incode = options.incodeService; this.storage = options.storage; } /** * Two reasons why this method needs to be called separately and is `async` * * Web - needs to call `warmup()` which is `async` * * Mobile - `IncodeSdk.initialize(...)` is `async` */ async initialize() { const initOptions = { apiKey: this.options.apiKey, apiUrl: this.options.apiUrl, isMobileEmulator: this.options.isMobileEmulator, waitForTutorials: this.options.waitForTutorials }; this.logger?.debug('Initializing Incode SDK:', JSON.stringify(initOptions)); await this.incode.initialize(initOptions); } async signIn() { this.mode = BiometricsModeEnum.SignIn; } async signUp(options?: SignInOptions) { if (this.sessionToken) { this.logger?.debug('Decoding session token:', jwtDecode(this.sessionToken)); const email = (jwtDecode(this.sessionToken) as any)['email']; const prevSessionEmail = await this.storage?.getItem(BiometricsStorageKeyEnum.AuthEmails) ?? ''; // if (email !== prevSessionEmail) setInterviewId(null); // if (email) setSignUpEmail(email); } } }