import { BoraPortalConnectRequest, FaceLoginResponse, LoginProviderType, LoginWithIdTokenRequest, ProviderRpcError, } from '@haechi-labs/face-types'; import { getChainId, isEthlikeNetwork } from '@haechi-labs/shared'; import { ethers } from 'ethers'; import { Internal } from './Internal'; import eventEmitter from './utils/Events'; /** * The interface about authentication. * * @category API */ export class Auth { private readonly internal: Internal; constructor(internal: Internal) { this.internal = internal; } private emitLoginEventsOnlyEthlike() { if (isEthlikeNetwork(this.internal.getNetwork())) { const chainId = ethers.utils.hexlify(getChainId(this.internal.getNetwork())); eventEmitter.emit('connect', { chainId }); } } private emitLogoutEventsOnlyEthlike() { if (isEthlikeNetwork(this.internal.getNetwork())) { const error: ProviderRpcError = { name: 'disconnect', code: 4900, message: 'facewallet logout', }; eventEmitter.emit('disconnect', error); } } /** * Sign-up(if new user) or login function. Need to initialize face with environment, blockchain and api key first. * * You can choose three options, Google, Facebook, and Apple login. * * @method * @returns {Promise} FaceLoginResponse. Unique user ID using on Face server and wallet address. */ async login(): Promise { const res = await this.internal.loginWithCredential(); this.emitLoginEventsOnlyEthlike(); return res; } /** * Directly sign-up(if new user) or login using social login. Need to initialize face with environment, blockchain and api key first. * * Pass the desired login provider to parameter. * * @method * @param {LoginProviderType} provider - 'google.com' | 'facebook.com' | 'apple.com' * @returns {Promise} FaceLoginResponse. Unique user ID using on Face server and wallet address. */ async directSocialLogin(provider: LoginProviderType): Promise { const res = this.internal.directSocialLogin(provider); this.emitLoginEventsOnlyEthlike(); return res; } /** * Login using the idToken published from social providers and Signature. Need to initialize face with environment, blockchain and api key first. * You can make a Signature using Facewallet SDK API Secret. * * Pass the the idToken and Signature to parameters. * * @method * @param {LoginWithIdTokenRequest} loginWithIdTokenRequest * @returns {Promise} FaceLoginResponse. Unique user ID using on Face server and wallet address. */ async loginWithIdToken( loginWithIdTokenRequest: LoginWithIdTokenRequest ): Promise { const res = this.internal.loginWithIdToken(loginWithIdTokenRequest); this.emitLoginEventsOnlyEthlike(); return res; } async boraLogin( connectRequest: BoraPortalConnectRequest, providers?: LoginProviderType[] ): Promise { const res = await this.internal.boraLogin(connectRequest, providers); this.emitLoginEventsOnlyEthlike(); return res; } async boraDirectSocialLogin( connectRequest: BoraPortalConnectRequest, provider: LoginProviderType ): Promise { const res = await this.internal.boraDirectSocialLogin(connectRequest, provider); this.emitLoginEventsOnlyEthlike(); return res; } async boraLoginWithIdToken( connectRequest: BoraPortalConnectRequest, loginWithIdTokenRequest: LoginWithIdTokenRequest ): Promise { const res = await this.internal.boraLoginWithIdToken(connectRequest, loginWithIdTokenRequest); this.emitLoginEventsOnlyEthlike(); return res; } /** * Log out Facewallet. * * It will be resolved after successful logout. * * @method * @returns {Promise} */ async logout(): Promise { await this.internal.logout(); this.emitLogoutEventsOnlyEthlike(); } /** * Return user information. * * @method * @returns {Promise} */ async getCurrentUser(): Promise { return await this.internal.getCurrentUser(); } /** * Check if user is already logged in. * * @returns {Promise} */ async isLoggedIn(): Promise { return await this.internal.isLoggedIn(); } }