ui-for-firebase-authentication
Version:
UI for Firebase Authentication (Firebase UI alternative supporting v9+)
58 lines (57 loc) • 2.9 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { createUserWithEmailAndPassword, fetchSignInMethodsForEmail, sendPasswordResetEmail, signInWithEmailAndPassword, signInWithPopup } from "firebase/auth";
function createFirebaseBindings(auth) {
return {
signInWithEmailPassword: function (email, password) {
return firebaseErrorWrapper(signInWithEmailAndPassword(auth, email, password));
},
createUserWithEmailPassword: function (email, password) {
return firebaseErrorWrapper(createUserWithEmailAndPassword(auth, email, password));
},
sendPasswordResetEmail: function (email) {
return firebaseErrorWrapper(sendPasswordResetEmail(auth, email));
},
fetchSignInMethodsForEmail: function (email) {
return __awaiter(this, void 0, void 0, function* () {
return firebaseErrorWrapper(fetchSignInMethodsForEmail(auth, email));
});
},
signInWithProvider: function (provider) {
return firebaseErrorWrapper(signInWithPopup(auth, provider));
}
};
}
const codeMap = {
//User errors
"auth/user-not-found": "No user found with selected email",
"auth/email-already-in-use": "An account already exists with this email",
"auth/wrong-password": "Incorrect Password",
"auth/weak-password": "Password too weak - must be at least 6 characters",
"auth/popup-closed-by-user": "Oopsies! Looks like you closed the sign in popup!",
//Service Problems
"auth/too-many-requests": "Too many recent requests. You can reset your password or try again later",
"auth/requires-recent-login": "You must sign in again before you can perform this action",
//Configuration issues
"auth/unauthorized-domain": "Configuration Error: Unauthorized Domain",
};
function firebaseErrorWrapper(prom) {
return __awaiter(this, void 0, void 0, function* () {
//Wrap an error to create a user-presentable error message.
return prom.catch((e) => {
if (codeMap[e.code]) {
console.error("Error Cause", e);
throw new Error(codeMap[e.code]);
}
throw e;
});
});
}
export { createFirebaseBindings };