@denz93/vendure-plugin-3rd-party-auth
Version:
A set of auth strategies to work with 3rd party such as Google, Facebook, etc
92 lines (91 loc) • 4.03 kB
JavaScript
;
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); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FacebookAuthService = void 0;
const common_1 = require("@nestjs/common");
const constants_1 = require("./constants");
const path_1 = __importDefault(require("path"));
let FacebookAuthService = class FacebookAuthService {
constructor(httpService, options) {
this.httpService = httpService;
this.options = options;
this.endpoint = 'https://graph.facebook.com';
this.version = 'v16.0';
this.optionDetail = options.facebook;
}
/**
* The flow to verify a token will be:
*
* 1. Generate app_token using client_id, and client_secret
* 2. Inspect the token below using app_token
* 3. Call /me endpoint on behalf of user to get user's email
*
* @param token the token is retrieved from Facebook dialog in front-end
*/
async verify(token) {
const appToken = await this.generateAppToken();
const tokenDebugInfo = await this.inspectToken(token, appToken);
if (!tokenDebugInfo.is_valid) {
throw new Error('Token is invalid');
}
if (!tokenDebugInfo.scopes.includes('email')) {
throw new Error('User not grant "email" permission');
}
const userInfo = await this.getUserInfo(token);
return userInfo;
}
async getUserInfo(token) {
const url = this.constructUrl('me');
const res = await this.httpService.get(url, {
params: {
fields: ['id', 'email', 'first_name', 'last_name'].join(),
},
headers: {
"Authorization": `Bearer ${token}`
}
}).toPromise();
return res.data;
}
async inspectToken(token, appToken) {
const url = this.constructUrl('debug_token', false);
const res = await this.httpService.get(url, { params: {
input_token: token,
access_token: appToken
} }).toPromise();
return res.data.data;
}
async generateAppToken() {
const url = this.constructUrl('oauth/access_token', false);
const res = await this.httpService.get(url, { params: {
client_id: this.optionDetail.clientId,
client_secret: this.optionDetail.clientSecret,
grant_type: 'client_credentials'
} }).toPromise();
return res.data.access_token;
}
constructUrl(edge, withVersion = true) {
const url = path_1.default.join(this.endpoint, withVersion ? this.version : '', edge);
return url.toString();
}
};
FacebookAuthService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(common_1.HttpService)),
__param(1, (0, common_1.Inject)(constants_1.THIRD_PARTY_OPTIONS_PROVIDER)),
__metadata("design:paramtypes", [common_1.HttpService, Object])
], FacebookAuthService);
exports.FacebookAuthService = FacebookAuthService;