esri-fetch-utils
Utility functions for interacting with ArcGIS Server and Portal/AGOL REST APIs
Install
$ npm install esri-fetch-utils
# or
$ yarn add esri-fetch-utils
Usage
session: Auth via OAuth or Token
session
provides utility methods for managing OAuth login flow and accessing
the user object. For use in the browser.
// manage OAuth session in the browser
import { session as browserSession } from 'esri-fetch-utils';
// init session
const config = {
clientId: 'string', // registered app client id
redirectUri: 'string', // callback url
portalUrl: 'string', // optional - defaults to https://www.arcgis.com
tokenDuration: 'number', // optional - defaults to 20160
state: 'string', // optional
};
const session = browserSession.create(config);
// generate url to direct user to agol/portal login
const oAuthUrl = session.oAuthUrl();
console.log(oAuthUrl);
// https://www.arcgis.com/sharing/rest/oauth2/authorize?client_id=1234&response_type=token...
// parse hash when user redirected back to your app on successful login
const user = session.onRedirect();
console.log(user);
// {
// username: 'grill',
// portalUrl: 'https://www.arcgis.com',
// clientId: '1234',
// token: '2YotnFZFEjr1zCsicMWpAA',
// tokenDuration: 20160,
// tokenExpires: moment('2020-04-17T13:54:30.248'),
// state: 'qyxmpg9e5uWUPbxw',
// };
// check is token is expired - will return true if expired
const expired = session.tokenExpired(moment('2020-04-17T13:54:30.248'));
console.log(expired);
// true
genToken
TODO - will generate a token via username/password. For use in
scripts/server.