@mvp-factory/holy-auth-firebase
Version:
Firebase Authentication module with Google Sign-In support
128 lines (127 loc) • 6.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_FIREBASE_CONFIG = exports.VERSION = exports.AuthEvent = exports.AuthProvider = exports.createAuthMiddleware = exports.TokenVerifier = exports.createAuthenticatedClient = exports.authenticatedUpload = exports.authenticatedDelete = exports.authenticatedPatch = exports.authenticatedPut = exports.authenticatedPost = exports.authenticatedGet = exports.authenticatedJSON = exports.authenticatedFetch = exports.FirebaseAuthManager = void 0;
exports.initializeFirebaseAuth = initializeFirebaseAuth;
exports.isBrowser = isBrowser;
exports.isNode = isNode;
exports.updateAuthUI = updateAuthUI;
exports.setupAutoSave = setupAutoSave;
exports.getAutoSavedContent = getAutoSavedContent;
exports.clearAutoSavedContent = clearAutoSavedContent;
var FirebaseAuthManager_1 = require("./core/FirebaseAuthManager");
Object.defineProperty(exports, "FirebaseAuthManager", { enumerable: true, get: function () { return FirebaseAuthManager_1.FirebaseAuthManager; } });
const FirebaseAuthManager_2 = require("./core/FirebaseAuthManager");
var AuthenticatedFetch_1 = require("./utils/AuthenticatedFetch");
Object.defineProperty(exports, "authenticatedFetch", { enumerable: true, get: function () { return AuthenticatedFetch_1.authenticatedFetch; } });
Object.defineProperty(exports, "authenticatedJSON", { enumerable: true, get: function () { return AuthenticatedFetch_1.authenticatedJSON; } });
Object.defineProperty(exports, "authenticatedGet", { enumerable: true, get: function () { return AuthenticatedFetch_1.authenticatedGet; } });
Object.defineProperty(exports, "authenticatedPost", { enumerable: true, get: function () { return AuthenticatedFetch_1.authenticatedPost; } });
Object.defineProperty(exports, "authenticatedPut", { enumerable: true, get: function () { return AuthenticatedFetch_1.authenticatedPut; } });
Object.defineProperty(exports, "authenticatedPatch", { enumerable: true, get: function () { return AuthenticatedFetch_1.authenticatedPatch; } });
Object.defineProperty(exports, "authenticatedDelete", { enumerable: true, get: function () { return AuthenticatedFetch_1.authenticatedDelete; } });
Object.defineProperty(exports, "authenticatedUpload", { enumerable: true, get: function () { return AuthenticatedFetch_1.authenticatedUpload; } });
Object.defineProperty(exports, "createAuthenticatedClient", { enumerable: true, get: function () { return AuthenticatedFetch_1.createAuthenticatedClient; } });
var TokenVerifier_1 = require("./utils/TokenVerifier");
Object.defineProperty(exports, "TokenVerifier", { enumerable: true, get: function () { return TokenVerifier_1.TokenVerifier; } });
Object.defineProperty(exports, "createAuthMiddleware", { enumerable: true, get: function () { return TokenVerifier_1.createAuthMiddleware; } });
var FirebaseAuth_1 = require("./types/FirebaseAuth");
Object.defineProperty(exports, "AuthProvider", { enumerable: true, get: function () { return FirebaseAuth_1.AuthProvider; } });
Object.defineProperty(exports, "AuthEvent", { enumerable: true, get: function () { return FirebaseAuth_1.AuthEvent; } });
exports.VERSION = '1.0.0';
exports.DEFAULT_FIREBASE_CONFIG = {
authDomain: 'firebaseapp.com',
storageBucket: 'firebasestorage.app'
};
async function initializeFirebaseAuth(config, options) {
const authManager = FirebaseAuthManager_2.FirebaseAuthManager.getInstance(options);
await authManager.initialize(config);
return authManager;
}
function isBrowser() {
return typeof window !== 'undefined' && typeof window.document !== 'undefined';
}
function isNode() {
return typeof process !== 'undefined' &&
process.versions != null &&
process.versions.node != null;
}
function updateAuthUI(isLoggedIn, user, config = {}) {
if (!isBrowser())
return;
const { loginButtonId = 'firebase-login-btn', logoutButtonId = 'firebase-logout-btn', userInfoId = 'firebase-user-info' } = config;
const loginButton = document.getElementById(loginButtonId);
const logoutButton = document.getElementById(logoutButtonId);
const userInfo = document.getElementById(userInfoId);
if (isLoggedIn && user) {
if (loginButton)
loginButton.style.display = 'none';
if (logoutButton)
logoutButton.style.display = 'inline-block';
if (userInfo) {
userInfo.innerHTML = `<span class="user-email">${user.email || 'User'}</span>`;
userInfo.style.display = 'inline-block';
}
}
else {
if (loginButton)
loginButton.style.display = 'inline-block';
if (logoutButton)
logoutButton.style.display = 'none';
if (userInfo)
userInfo.style.display = 'none';
}
}
function setupAutoSave(getContent, options = {}) {
if (!isBrowser()) {
return () => { };
}
const { interval = 60000, key = 'firebase_auth_autosave', onSave, onError } = options;
const authManager = FirebaseAuthManager_2.FirebaseAuthManager.getInstance();
const saveContent = () => {
try {
const user = authManager.getCurrentUser();
if (!user)
return;
const content = getContent();
if (!content || !content.trim())
return;
const autoSaveData = {
content,
timestamp: new Date().toISOString(),
userEmail: user.email,
userId: user.uid
};
localStorage.setItem(key, JSON.stringify(autoSaveData));
if (onSave) {
onSave(autoSaveData);
}
}
catch (error) {
if (onError) {
onError(error);
}
}
};
const intervalId = setInterval(saveContent, interval);
return () => {
clearInterval(intervalId);
};
}
function getAutoSavedContent(key = 'firebase_auth_autosave') {
if (!isBrowser())
return null;
try {
const saved = localStorage.getItem(key);
if (!saved)
return null;
return JSON.parse(saved);
}
catch {
return null;
}
}
function clearAutoSavedContent(key = 'firebase_auth_autosave') {
if (!isBrowser())
return;
localStorage.removeItem(key);
}