@esri/arcgis-rest-auth
Version:
Authentication helpers for @esri/arcgis-rest-js.
62 lines • 2.53 kB
JavaScript
"use strict";
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplicationSession = void 0;
var tslib_1 = require("tslib");
var fetch_token_1 = require("./fetch-token");
/**
* ```js
* import { ApplicationSession } from '@esri/arcgis-rest-auth';
* const session = new ApplicationSession({
* clientId: "abc123",
* clientSecret: "sshhhhhh"
* })
* // visit https://developers.arcgis.com to generate your own clientid and secret
* ```
* You can use [App Login](/arcgis-rest-js/guides/node/) to access premium content and services in ArcGIS Online.
*
*/
var ApplicationSession = /** @class */ (function () {
function ApplicationSession(options) {
this.clientId = options.clientId;
this.clientSecret = options.clientSecret;
this.token = options.token;
this.expires = options.expires;
this.portal = options.portal || "https://www.arcgis.com/sharing/rest";
this.duration = options.duration || 7200;
}
// URL is not actually read or passed through.
ApplicationSession.prototype.getToken = function (url, requestOptions) {
if (this.token && this.expires && this.expires.getTime() > Date.now()) {
return Promise.resolve(this.token);
}
if (this._pendingTokenRequest) {
return this._pendingTokenRequest;
}
this._pendingTokenRequest = this.refreshToken(requestOptions);
return this._pendingTokenRequest;
};
ApplicationSession.prototype.refreshToken = function (requestOptions) {
var _this = this;
var options = tslib_1.__assign({ params: {
client_id: this.clientId,
client_secret: this.clientSecret,
grant_type: "client_credentials",
expiration: this.duration
} }, requestOptions);
return fetch_token_1.fetchToken(this.portal + "/oauth2/token/", options).then(function (response) {
_this._pendingTokenRequest = null;
_this.token = response.token;
_this.expires = response.expires;
return response.token;
});
};
ApplicationSession.prototype.refreshSession = function () {
var _this = this;
return this.refreshToken().then(function () { return _this; });
};
return ApplicationSession;
}());
exports.ApplicationSession = ApplicationSession;
//# sourceMappingURL=ApplicationSession.js.map