UNPKG

facebook-auth-nestjs

Version:
81 lines (80 loc) 4.09 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); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FacebookAuthService = void 0; const axios_1 = require("@nestjs/axios"); const config_constants_1 = require("./config/config.constants"); const common_1 = require("@nestjs/common"); const invalid_user_exception_1 = require("./exceptions/invalid-user.exception"); const app_token_exception_1 = require("./exceptions/app-token.exception"); const get_user_fields_exception_1 = require("./exceptions/get-user-fields.exception"); const user_validate_exception_1 = require("./exceptions/user-validate.exception"); let FacebookAuthService = exports.FacebookAuthService = class FacebookAuthService { constructor(configModule, httpService) { this.configModule = configModule; this.httpService = httpService; } async getUser(accessToken, ...fields) { const appToken = await this.getAppToken(); const user = (await this.userValidate(accessToken, appToken)); return await this.getUserFields(user.user_id, accessToken, fields); } async getUserFields(userId, accessToken, fields) { const result = await this.httpService.get(`${config_constants_1.FACEBOOK_AUTH_GRAPH_URL}${userId}`, { params: { fields: fields.join(', '), access_token: accessToken, } }) .toPromise(); if (result.status !== common_1.HttpStatus.OK) { throw new get_user_fields_exception_1.GetUserFieldsException(); } return result === null || result === void 0 ? void 0 : result.data; } async userValidate(accessToken, appToken) { var _a; const result = await this.httpService.get(config_constants_1.FACEBOOK_AUTH_DEBUG_TOKEN_URL, { params: { input_token: accessToken, access_token: appToken, } }) .toPromise(); if (result.status !== common_1.HttpStatus.OK) { throw new user_validate_exception_1.UserValidateException(); } const userData = (_a = result.data) === null || _a === void 0 ? void 0 : _a.data; if (!userData || !userData.user_id || !userData.is_valid || userData.app_id != this.configModule.clientId) { throw new invalid_user_exception_1.InvalidUserException(); } return userData; } async getAppToken() { const result = await this.httpService.get(config_constants_1.FACEBOOK_AUTH_ACCESS_TOKEN_URL, { params: { grant_type: 'client_credentials', client_id: this.configModule.clientId, client_secret: this.configModule.clientSecret, } }) .toPromise(); if (result.status !== common_1.HttpStatus.OK) { throw new app_token_exception_1.AppTokenException(); } return result.data.access_token; } }; exports.FacebookAuthService = FacebookAuthService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(config_constants_1.FACEBOOK_AUTH_CONFIG_MODULE_TOKEN)), __metadata("design:paramtypes", [Object, axios_1.HttpService]) ], FacebookAuthService);