UNPKG

ui-for-firebase-authentication

Version:

UI for Firebase Authentication (Firebase UI alternative supporting v9+)

50 lines (49 loc) 2.16 kB
import { signInWithPopup } from "firebase/auth"; import { createButtonForProvider } from "./createProviderButton"; import { EmailLoginInterface } from "./EmailLoginInterface"; import { handleFirebasePromise } from "../firebasePromiseResults"; /** * Initializes the sign in UI. This will render the sign in options into the target element. * * @param auth The Firebase Auth instance to use for authentication. * @param signInOptions The sign in options to display to the user. * @param targetElement The element to render the UI into. This element will be emptied before rendering. * */ function Initialize_UI(auth, signInOptions, targetElement) { //@ts-ignore window.signInWithPopup = signInWithPopup; //@ts-ignore window.auth = auth; //@ts-ignore window.signInOptions = signInOptions; //Empty the target element. while (targetElement.lastChild) { targetElement.lastChild.remove(); } if (signInOptions.length === 0) { throw new Error("No sign in options provided. "); } let buttonContainer = document.createElement("div"); buttonContainer.classList.add("loginProviderButtonContainer"); targetElement.appendChild(buttonContainer); //Create the buttons for sign in. for (let signInOption of signInOptions) { let button = createButtonForProvider(signInOption); buttonContainer.appendChild(button); button.addEventListener("click", function () { if (signInOption.provider !== "email") { handleFirebasePromise(signInWithPopup(auth, signInOption.provider)); } else { //We will pass this element over to the email login interface. //After the email login interface closes, we will recurse and regenerate the UI let emailLoginInterface = new EmailLoginInterface(auth); buttonContainer.appendChild(emailLoginInterface.container); emailLoginInterface.onClose = function () { Initialize_UI(auth, signInOptions, targetElement); }; } }); } } export { Initialize_UI };