import { LoginAttemptResponse } from './AWSCognitoClient'; import { MiscTypes } from '@oneblink/types'; interface CognitoServiceData { oAuthClientId: string; loginDomain: string; region: string; redirectUri: string; logoutUri: string; } declare function init(cognitoServiceData: CognitoServiceData): void; /** * Register a listener function that will be call when authentication tokens are * updated or removed. * * #### Example * * ```js * const listener = async () => { * // Check if the user is logged in still * const isLoggedIn = authService.isLoggedIn() * } * const deregister = await authService.registerAuthListener(listener) * * // When no longer needed, remember to deregister the listener * deregister() * ``` * * @param listener * @returns */ declare function registerAuthListener(listener: () => unknown): () => void; /** * Create a session for a user by entering a username and password. If the user * requires a password reset, the "resetPasswordCallback" property will be * returned. This function should be called with the new password once entered * by the user. If the user requires an MFA token, the "mfaCodeCallback" * property will be returned. This function should be called with a one-time * token generated from an authenticator app. The functions returned are * recursive and the result from each of them is the same result from the * loginUsernamePassword() function. Each time the response includes a callback, * you will need to begin the process again until all callbacks are handled. * * #### Example * * ```js * async function handleLoginAttemptResponse({ * resetPasswordCallback, * mfaCodeCallback, * }) { * // "resetPasswordCallback" will be undefined if a password reset was not required. * if (resetPasswordCallback) { * // Prompt the user to enter a new password * const newPassword = prompt( * 'The password you entered was only temporary, and must be reset for security purposes. Please enter your new password below to continue.', * ) * const resetPasswordResponse = * await resetPasswordCallback(newPassword) * return await handleLoginAttemptResponse(resetPasswordResponse) * } * * // "mfaCodeCallback" will be undefined if MFA is not setup. * if (mfaCodeCallback) { * // Prompt the user to enter an MFA code * const code = prompt( * 'Please enter a one-time code from your MFA app.', * ) * const mfaCodeResponse = await mfaCodeCallback(code) * return await handleLoginAttemptResponse(mfaCodeResponse) * } * } * * const username = 'user@email.io' * const password = 'P@$5w0rd' * * const loginAttemptResponse = await authService.loginUsernamePassword( * username, * password, * ) * * await handleLoginAttemptResponse(loginAttemptResponse) * ``` * * @param username * @param password * @returns */ declare function loginUsernamePassword(username: string, password: string): Promise; /** * Redirect the user to the login screen. Passing an `identityProvider` is * optionally, it will allow users to skip the login page and be directed * straight to that providers login page * * #### Example * * ```js * // OPtionally pass a * const identityProvider = 'Google' * await authService.loginHostedUI(identityProvider) * // User will be redirected to login page or promise will resolve * ``` * * @param identityProviderName * @returns */ declare function loginHostedUI(identityProviderName?: string): Promise; /** * This function should be called when the user is redirected back to your app * after a login attempt. It will use the query string add the redirect URL to * create a session for the current user. It will return a URL as a `string` * that should be redirected to within your app. * * #### Example * * ```js * try { * const continueTo = await authService.handleAuthentication() * // Redirect the user back to where they were before attempting to login * window.location.href = continueTo * } catch (error) { * // handle failed login attempts here. * } * ``` * * @returns */ declare function handleAuthentication(): Promise; /** * Allow the currently logged in user to change their password by passing their * existing password and a new password. * * #### Example * * ```js * const currentPassword = 'P@$5w0rd' * const newPassword = 'P@$5w0rD' * await authService.changePassword(currentPassword, newPassword) * ``` * * @param existingPassword * @param newPassword * @returns */ declare function changePassword(existingPassword: string, newPassword: string): Promise; /** * Allow a user to start the forgot password process. The user will be emailed a * temporary code that must be passed with a new password to the function * returned. * * #### Example * * ```js * const username = 'user@email.io' * const finishForgotPassword = await authService.forgotPassword(username) * * // Prompt the user to enter the code and a new password * const code = prompt( * 'You have been emailed a verification code, please enter it here.', * ) * const newPassword = prompt('Please enter a new password to continue.') * await finishForgotPassword(code, newPassword) * ``` * * @param username * @param formsAppId Used to give the resulting email sent to the user * associated forms app branding and sending address * @returns */ declare function forgotPassword(username: string, /** * Used to give the resulting email sent to the user associated forms app * branding and sending address */ formsAppId?: number): Promise<(code: string, password: string) => Promise>; /** * Redirect the user to the logout screen to clear the users session on the * hosted login page. User will then be redirected to `/logout`. After being * redirected back to the application, the `logout()` function should be called * to clear the session data from browser storage. * * #### Example * * ```js * authService.logoutHostedUI() * ``` */ declare function logoutHostedUI(): void; declare function logout(): Promise; /** * Check if the user is currently logged in * * #### Example * * ```js * const isLoggedIn = authService.isLoggedIn() * // handle user being logged in or not * ``` * * @returns */ declare function isLoggedIn(): boolean; declare function getCognitoIdToken(): Promise; /** * Get current users profile based on there Id Token payload. This will return * `null` if the the current user is not logged in. * * #### Example * * ```js * const profile = authService.getUserProfile() * if (profile) { * // Use profile here * } * ``` * * @returns */ declare function getUserProfile(): MiscTypes.UserProfile | null; export declare function getUsername(): string | undefined; /** * A friendly `string` that represents the current user. Uses first name, last * name, full name and username. This will return `null` the current user is not * logged in. * * #### Example * * ```js * const name = authService.getUserFriendlyName() * if (name) { * // Display current user's name * } * ``` * * @returns */ declare function getUserFriendlyName(): string | undefined; /** * Generate a QR code link to display to a user after they have initiated MFA * setup. * * #### Example * * ```js * const mfaSetupQrCodeUrl = authService.generateMfaQrCodeUrl() * if (mfaSetupQrCodeUrl) { * // use mfaSetupQrCodeUrl to display QR code to user * } * ``` * * @returns */ declare function generateMfaQrCodeUrl(mfaSetupConfiguration: Awaited>): string | undefined; /** * Check if MFA is enabled for this current user. * * #### Example * * ```js * const isMfaEnabled = await authService.checkIsMfaEnabled() * if (isMfaEnabled) { * // Allow disabling MFA * } else { * // Allow enabling MFA * } * ``` * * @returns */ declare function checkIsMfaEnabled(): Promise; /** * Disable MFA for the current user. * * #### Example * * ```js * await authService.disableMfa() * ``` * * @returns */ declare function disableMfa(): Promise; /** * Setup MFA for the current user. The result will include a callback that * should be called with the valid TOTP from an authenticator app. * * #### Example * * ```js * const { secretCode, mfaCodeCallback } = await authService.setupMfa() * // Prompt the user to enter an MFA code * const code = prompt( * `Please enter a one-time code from your MFA app after creating a new entry with secret: ${secretCode}.`, * ) * await mfaCodeCallback(code) * ``` * * @returns */ declare function setupMfa(): Promise<{ secretCode: string | undefined; mfaCodeCallback: (code: string) => Promise; } | undefined>; export { init, registerAuthListener, loginUsernamePassword, loginHostedUI, handleAuthentication, changePassword, forgotPassword, logoutHostedUI, logout, isLoggedIn, getCognitoIdToken, getUserProfile, getUserFriendlyName, LoginAttemptResponse, checkIsMfaEnabled, disableMfa, setupMfa, generateMfaQrCodeUrl, };