UNPKG

@tfarras/nestjs-firebase-auth

Version:

NestJS Passport Strategy for Firebase Auth using Firebase Admin SDK

53 lines 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FirebaseAuthStrategy = void 0; const common_1 = require("@nestjs/common"); const passport_strategy_1 = require("passport-strategy"); const constants_1 = require("./constants"); const admin = require("firebase-admin"); class FirebaseAuthStrategy extends passport_strategy_1.Strategy { constructor(options, extractor, logger = new common_1.Logger(FirebaseAuthStrategy.name)) { super(); this.extractor = extractor; this.logger = logger; this.name = constants_1.FIREBASE_AUTH; this.checkRevoked = false; if (!options.extractor) { throw new Error('\n Extractor is not a function. You should provide an extractor. \n Read the docs: https://github.com/tfarras/nestjs-firebase-auth#readme'); } this.extractor = options.extractor; this.checkRevoked = options.checkRevoked; } async validate(payload) { return payload; } authenticate(req) { const idToken = this.extractor(req); if (!idToken) { this.fail(constants_1.UNAUTHORIZED, 401); return; } try { admin .auth() .verifyIdToken(idToken, this.checkRevoked) .then((res) => this.validateDecodedIdToken(res)) .catch((err) => { this.fail({ err }, 401); }); } catch (e) { this.logger.error(e); this.fail(e, 401); } } async validateDecodedIdToken(decodedIdToken) { const result = await this.validate(decodedIdToken); if (result) { this.success(result); } this.fail(constants_1.UNAUTHORIZED, 401); } } exports.FirebaseAuthStrategy = FirebaseAuthStrategy; //# sourceMappingURL=passport-firebase.strategy.js.map