UNPKG

@lonelyplanet/dotcom-core

Version:

58 lines (57 loc) 2.76 kB
"use strict"; var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var AuthRequestBuilder = (function () { function AuthRequestBuilder(_a) { var host = _a.host, clientId = _a.clientId, redirectUri = _a.redirectUri, scope = _a.scope; this.host = host; this.clientId = clientId; this.redirectUri = redirectUri; this.scope = scope ? AuthRequestBuilder.buildScope(scope) : ""; } AuthRequestBuilder.build = function (options) { var builder = new AuthRequestBuilder(options); return { builder: builder, googleLink: builder.getLoginUrl("google", options.targetLinkUri), twitterLink: builder.getLoginUrl("twitter", options.targetLinkUri), facebookLink: builder.getLoginUrl("facebook", options.targetLinkUri), passwordLink: builder.getLoginUrl("password", options.targetLinkUri), passwordlessLink: function (email) { return builder.getLoginUrl("passwordless", options.targetLinkUri, { "login_hint": email }); } }; }; AuthRequestBuilder.buildScope = function (scope) { if (scope === void 0) { scope = []; } return scope.join("+"); }; AuthRequestBuilder.formatQueryString = function (queryObject) { return Object.keys(queryObject) .map(function (q) { return q + "=" + queryObject[q]; }) .join("&"); }; AuthRequestBuilder.prototype.buildUrl = function (queryObject) { var queryWithDefaults = __assign({ client_id: this.clientId, redirect_uri: this.redirectUri, scope: this.scope }, queryObject); return this.host + "?" + AuthRequestBuilder.formatQueryString(queryWithDefaults); }; AuthRequestBuilder.prototype.getLoginUrl = function (authMethod, redirect, settings) { if (settings === void 0) { settings = {}; } var queryObject = __assign({ auth_method: authMethod, target_link_uri: redirect, response_type: "code", prompt: "login" }, settings); return this.buildUrl(queryObject); }; AuthRequestBuilder.prototype.getRefreshUrl = function (authMethod, redirect, settings) { if (settings === void 0) { settings = {}; } var queryObject = __assign({ auth_method: authMethod, target_link_uri: redirect, response_type: "ajax" }, settings); return this.buildUrl(queryObject); }; return AuthRequestBuilder; }()); exports.default = AuthRequestBuilder;