@esri/arcgis-rest-auth
Version:
Authentication helpers for @esri/arcgis-rest-js.
59 lines • 2.38 kB
JavaScript
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import { __assign } from "tslib";
import { fetchToken } from "./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 = __assign({ params: {
client_id: this.clientId,
client_secret: this.clientSecret,
grant_type: "client_credentials",
expiration: this.duration
} }, requestOptions);
return 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;
}());
export { ApplicationSession };
//# sourceMappingURL=ApplicationSession.js.map