UNPKG

@sentclose/sentc-light

Version:
153 lines (152 loc) 5.39 kB
/** * @author Jörn Heinemann <joernheinemann@gmx.de> * @since 2023/07/23 */ import { User } from "./User"; import { InitInput } from "sentc_wasm_light"; import { StorageInterface, UserMfaLogin, StorageOptions } from "@sentclose/sentc-common"; import { LoginUser, UserId } from "./Entities"; export declare const enum REFRESH_ENDPOINT { cookie = 0, cookie_fn = 1, api = 2 } export interface RefreshOptions { endpoint_url?: string; endpoint_fn?: (old_jwt: string) => Promise<string>; endpoint: REFRESH_ENDPOINT; } export interface SentcOptions { base_url?: string; app_token: string; file_part_url?: string; refresh?: RefreshOptions; wasm_path?: InitInput | Promise<InitInput>; storage?: StorageOptions; } export declare class Sentc { private static init_client; private static init_storage; static options: SentcOptions; private static storage; static getStore(): Promise<StorageInterface>; static init(options: SentcOptions): Promise<User | undefined>; /** * Do a request to the sentc api to check if the user identifier is still available. * * true => user identifier is free * * @param userIdentifier */ static checkUserIdentifierAvailable(userIdentifier: string): Promise<boolean>; /** * Prepare the server input for the sentc api to check if an identifier is available * * This function won't do a request * * @param userIdentifier */ static prepareCheckUserIdentifierAvailable(userIdentifier: string): string | false; /** * Checks the server output after the request. * * This is only needed when not using @see checkUserIdentifierAvailable * * @param serverOutput */ static doneCheckUserIdentifierAvailable(serverOutput: string): boolean; static generateRegisterData(): string[]; /** * Generates the register input for the api. * * It can be used in an external backend * * @param userIdentifier * @param password */ static prepareRegister(userIdentifier: string, password: string): string; /** * Validates the register output from the api when using prepare register function * * @param serverOutput */ static doneRegister(serverOutput: string): string; /** * Register a new user. * * @param userIdentifier * @param password * @throws Error * - if username exists * - request error */ static register(userIdentifier: string, password: string): Promise<UserId> | false; static doneRegisterDeviceStart(server_output: string): void; static registerDeviceStart(device_identifier: string, password: string): false | Promise<string>; private static buildUserObj; /** * Log the user in * * Store all user data in the storage (e.g. Indexeddb) * * For a refresh token flow -> send the refresh token to your server and save it in a http only strict cookie * Then the user is safe for xss and csrf attacks * * when Either UserMfaLogin is returned, then the user must enter the mfa token. * Use the function Sentc.mfaLogin() to do the totp login or Sentc.mfaRecoveryLogin() to log in with a recover key */ static login(deviceIdentifier: string, password: string): Promise<LoginUser>; /** * Log in the user. * * The same as the other login function but already given the user class back instead of an Either type. * This is helpful if you disabled mfa for every user and just wants to get the user without an extra check. * * This function will throw an exception if the user enables mfa. */ static login(deviceIdentifier: string, password: string, force: true): Promise<User>; static mfaLogin(token: string, login_data: UserMfaLogin): Promise<User>; static mfaRecoveryLogin(recovery_token: string, login_data: UserMfaLogin): Promise<User>; /** * get a new jwt when the old one is expired * * The check is done automatically when making a sentc api request * * It can be refreshed directly with the sdk, a request to another backend with a cookie or with an own function * * @param old_jwt * @param refresh_token */ static refreshJwt(old_jwt: string, refresh_token: string): Promise<string>; /** * Get the actual used user data * * @throws Error * when user is not set */ static getActualUser(): Promise<User>; /** * Get the actual user but with a valid jwt * @param jwt * @throws Error * when user is not set or the jwt refresh failed */ static getActualUser(jwt: true): Promise<User>; /** * Get the actual used user and the username * * @param jwt * @param username * @throws Error * when user not exists in the client */ static getActualUser(jwt: false, username: true): Promise<[User, string]>; /** * Get any user matched by the user identifier. * * The user data is stored in the indexeddb (standard) or in memory * * @param userIdentifier */ static getUser(userIdentifier: string): Promise<User | false>; }