UNPKG

@alpha018/nestjs-firebase-auth

Version:

NestJS Firebase library and Role based guard for authentication with some utils functions

72 lines 2.95 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FirebaseProvider = void 0; const app_1 = require("firebase-admin/app"); const auth_1 = require("firebase-admin/auth"); const common_1 = require("@nestjs/common"); const fa = require("firebase-admin"); const firebase_constant_1 = require("../constant/firebase.constant"); let FirebaseProvider = class FirebaseProvider { get auth() { return (0, auth_1.getAuth)(this._app); } get app() { return this._app; } get rolesKey() { return this.data.auth?.config?.rolesClaimKey ?? firebase_constant_1.FIREBASE_APP_ROLES_DEFAULT_DECORATOR; } constructor(data) { this.data = data; if ((0, app_1.getApps)().length) { this._app = (0, app_1.getApp)(); return; } let appOptions; if (data.base64) { appOptions = { credential: fa.credential.cert(JSON.parse(Buffer.from(data.base64, 'base64').toString('utf-8'))), }; } else if (data.options) { appOptions = data.options; } this._app = (0, app_1.initializeApp)(appOptions); } async getClaimsRoleBase(user, localDecode) { if (localDecode) { return user?.[this.rolesKey]; } const { customClaims } = await this.auth.getUser(user.uid); return customClaims?.[this.rolesKey]; } async setClaimsBase(uid, claims) { const { customClaims } = await this.auth.getUser(uid); return this.auth.setCustomUserClaims(uid, { ...claims, [this.rolesKey]: customClaims?.[this.rolesKey], }); } async setClaimsRoleBase(uid, claims) { const { customClaims } = await this.auth.getUser(uid); return this.auth.setCustomUserClaims(uid, { ...(customClaims || {}), [this.rolesKey]: claims, }); } }; exports.FirebaseProvider = FirebaseProvider; exports.FirebaseProvider = FirebaseProvider = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [Object]) ], FirebaseProvider); //# sourceMappingURL=firebase.provider.js.map