UNPKG

@trap_stevo/legendarybuilderpronodejs-utilities

Version:

The legendary computational utility API that makes your application a legendary application. ~ Created by Steven Compton

190 lines (146 loc) 4.55 kB
const admin = require("firebase-admin"); class HUDFirebaseManager { constructor() { this.initializedApps = {}; this.services = {}; } initializeHUDFirebaseApp(config) { const appName = config.projectId; if (this.initializedApps[appName]) { return this.initializedApps[appName]; } const app = admin.initializeApp({ credential : admin.credential.cert(config.serviceAccount), databaseURL : config.databaseURL }, appName); this.initializedApps[appName] = app; return app; } setUpHUDFirebaseServices(app, options) { if (options.firestore) { this.services.firestore = options.firestore.reduce((account, config) => { account[config.label] = admin.firestore(app); return account; }, {}); } if (options.database) { this.services.database = options.database.reduce((account, config) => { account[config.label] = admin.database(app); return account; }, {}); } if (options.storage) { this.services.storage = options.storage.reduce((account, config) => { account[config.label] = admin.storage(app); return account; }, {}); } if (options.auth) { this.services.auth = admin.auth(app); } return this.services; } getHUDFirebaseApp(appName) { return this.initializedApps[appName] || null; } async generatePasswordResetEmailLink(email, auth = this.services.auth) { try { return await auth.generatePasswordResetLink(email); } catch (error) { console.error("Did not generate password reset email link: ", error); return null; } } async createUser(email, password, auth = this.services.auth) { try { return await auth.createUser({ email, password }); } catch (error) { console.error("Did not create user: ", error); return null; } } async deleteUser(uid, auth = this.services.auth) { try { await auth.deleteUser(uid); return true; } catch (error) { console.error("Error deleting user: ", error); return false; } } async getUserByEmail(email, auth = this.services.auth) { try { return await auth.getUserByEmail(email); } catch (error) { console.error("Did not get user by email: ", error); return null; } } async getUserByUID(uid, auth = this.services.auth) { try { return await auth.getUser(uid); } catch (error) { console.error("Error getting user by UID: ", error); return null; } } async createCustomToken(uid, additionalClaims, auth = this.services.auth) { try { return await auth.createCustomToken(uid, additionalClaims); } catch (error) { console.error("Did not create custom token: ", error); return null; } } async verifyIDToken(idToken, auth = this.services.auth) { try { return await auth.verifyIdToken(idToken); } catch (error) { console.error("Did not verify ID token: ", error); return false; } } } const hudFirebaseManager = new HUDFirebaseManager(); module.exports = { hudFirebaseManager };