import { DEFAULT_MFA_SETTINGS, LoginAttemptResponse, LoggedInMfaMethod, MfaMethod, MfaSettings } 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 "mfa" property will be * returned. Its "codeCallback" should be called with the one-time token. 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, * mfa, * }) { * // "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) * } * * // "mfa" will be undefined if MFA is not setup. * if (mfa) { * // Prompt the user to enter an MFA code * const code = prompt( * mfa.method === 'email' * ? 'Please enter the one-time code sent to your email.' * : 'Please enter a one-time code from your MFA app.', * ) * const mfaCodeResponse = await mfa.codeCallback(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 * authenticator app MFA setup. * * #### Example * * ```js * const mfaAuthenticatorAppSetupQrCodeUrl = * mfaService.generateMfaAuthenticatorAppQrCodeUrl() * if (mfaAuthenticatorAppSetupQrCodeUrl) { * // use mfaAuthenticatorAppSetupQrCodeUrl to display QR code to user * } * ``` * * @param mfaAuthenticatorAppSetup * @param authenticatorAppDisplayName Name shown in the authenticator app. When * omitted, the product name is used. Console sign-in omits this. * @returns */ declare function generateMfaAuthenticatorAppQrCodeUrl(mfaAuthenticatorAppSetup: Awaited>, authenticatorAppDisplayName?: string): string | undefined; declare function getMfaSettings(abortSignal?: AbortSignal): Promise; declare function updateUserPhoneNumber(phoneNumber: string): Promise<{ isPhoneNumberVerified: boolean; }>; declare function removeUserPhoneNumber(): Promise; declare function verifyUserPhoneNumber(code: string): Promise; declare function setupSmsMfa(options?: { preferred?: boolean; }): Promise; declare function disableMfaMethod(method: MfaMethod): Promise; declare function setPreferredMfaMethod(method: MfaMethod): Promise; /** * Setup authenticator app 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 mfaService.setupMfaAuthenticatorApp() * // 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 setupMfaAuthenticatorApp(options?: { preferred?: boolean; }): Promise<{ secretCode: string | undefined; mfaCodeCallback: (code: string) => Promise; } | undefined>; export { init, registerAuthListener, loginUsernamePassword, loginHostedUI, handleAuthentication, changePassword, forgotPassword, logoutHostedUI, logout, isLoggedIn, getCognitoIdToken, getUserProfile, getUserFriendlyName, getMfaSettings, updateUserPhoneNumber, removeUserPhoneNumber, verifyUserPhoneNumber, disableMfaMethod, setPreferredMfaMethod, setupSmsMfa, setupMfaAuthenticatorApp, generateMfaAuthenticatorAppQrCodeUrl, DEFAULT_MFA_SETTINGS, }; export type { LoginAttemptResponse, LoggedInMfaMethod, MfaMethod, MfaSettings };