UNPKG

@marplex/flarebase-auth

Version:

Firebase/Admin auth SDK for Cloudflare Workers

93 lines (92 loc) 3.59 kB
import { Cache } from './cache/cache'; import { DecodedIdToken, User } from './models'; export declare type FlarebaseConfig = { readonly projectId: string; readonly apiKey: string; readonly serviceAccountEmail: string; readonly privateKey: string; readonly cache?: Cache; }; /** * Interact with Firebase REST Api and Google Identity Toolkit Api. * Made to work with Cloudflare Workers */ export declare class FlarebaseAuth { readonly config: FlarebaseConfig; private BASE_URL; constructor(config: FlarebaseConfig); /** * Cache the result of an async function * @param action Function with result to be stored * @param key Where to find/store the value from/to the cache * @param expiration Cache expiration in seconds * @returns Cached result */ private withCache; /** * Send a post request to the identity toolkit api * @param formData POST form data * @param endpoint endpoint of the identity toolkit googleapis * @returns HTTP Response */ private sendFirebaseAuthPostRequest; /** * Retrieve user info from a Firebase ID token * @param idToken A valid Firebase ID token * @returns User info linked to this ID token */ lookupUser(idToken: any): Promise<User>; /** * Sign in Firebase user with email and password * @param email Email of the Firebase user * @param password Password of the Firebase user * @returns The decoded JWT token payload and the signed in user info */ signInWithEmailAndPassword(email: string, password: string): Promise<{ token: DecodedIdToken; user: User; }>; /** * Change a user's password * @param idToken A Firebase Auth ID token for the user. * @param newPassword User's new password. * @returns The decoded JWT token payload */ changePassword(idToken: string, newPassword: string): Promise<DecodedIdToken>; /** * Delete a current user * @param idToken A Firebase Auth ID token for the user. */ deleteAccount(idToken: string): Promise<void>; /** * Sign up Firebase user with email and password * @param email Email of the Firebase user * @param password Password of the Firebase user * @returns The decoded JWT token payload and the signed in user info */ signUpWithEmailAndPassword(email: string, password: string): Promise<{ token: DecodedIdToken; user: User; }>; /** * Creates a session cookie for the given Identity Platform ID token. * The session cookie is used by the client to preserve the user's login state. * @param idToken A valid Identity Platform ID token * @param expiresIn The number of seconds until the session cookie expires. * Specify a duration in seconds, between five minutes and fourteen days, inclusively. * @returns The session cookie that has been created */ createSessionCookie(idToken: string, expiresIn?: number): Promise<string>; /** * Verify if the provided session cookie is valid. * @param sessionCookie JWT session cookie generated from createSessionCookie * @returns The decoded JWT payload */ verifySessionCookie(sessionCookie: string): Promise<DecodedIdToken>; /** * Verifies a Firebase ID token (JWT). * If the token is valid, the promise is fulfilled with the token's decoded claims; otherwise, the promise is rejected. * @param idToken An Identity Platform ID token */ verifyIdToken(idToken: string): Promise<DecodedIdToken>; }