UserSession
import { UserSession } from '@esri/arcgis-rest-auth';
UserSession.beginOAuth2({
// register an app of your own to create a unique clientId
clientId: "abc123",
redirectUri: 'https://yourapp.com/authenticate.html'
})
.then(session)
// or
new UserSession({
username: "jsmith",
password: "123456"
})
// or
UserSession.deserialize(cache)
Used to authenticate both ArcGIS Online and ArcGIS Enterprise users. UserSession
includes helper methods for OAuth 2.0 in both browser and server applications.
Implements
Constructors
Constructor Parameters
Parameter | Type | Default | Notes |
---|---|---|---|
options Required | IUserSessionOptions |
Properties
Property | Type | Notes |
---|---|---|
string |
Client ID being used for authentication if provided in the |
|
string |
The currently authenticated user's password if provided in the |
|
string |
The current portal the user is authenticated with. |
|
AuthenticationProvider |
The authentication provider to use. |
|
string |
A valid redirect URI for this application if provided in the |
|
number |
Duration of new OAuth 2.0 refresh token validity (in minutes). |
|
string |
An unfederated ArcGIS Server instance known to recognize credentials supplied manually.
|
|
boolean |
This value is set to true automatically if the ArcGIS Organization requires that requests be made over https. |
|
number |
Determines how long new tokens requested are valid. |
|
string |
The currently authenticated user if provided in the |
Accessors
Methods
Method | Returns | Notes |
---|---|---|
|
void |
Begins a new server-based OAuth 2.0 sign in. This will redirect the user to the ArcGIS Online or ArcGIS Enterprise authorization page. |
|
Promise<UserSession> | undefined |
Begins a new browser-based OAuth 2.0 sign in. If |
|
UserSession |
Completes a browser-based OAuth 2.0 sign in. If |
|
UserSession | |
|
Promise<UserSession> |
Completes the server-based OAuth 2.0 sign in process by exchanging the |
|
UserSession |
Translates authentication from the format used in the ArcGIS API for JavaScript.
|
|
Promise<any> |
Request session information from the parent application When an application is embedded into another application via an IFrame, the embedded app can
use The ArcGIS API for Javascript has this built into the Identity Manager as of the 4.19 release. Note: The parent application will not respond if the embedded app's origin is not:
|
|
void |
For a "Host" app that has embedded other platform apps via iframes, when the host needs
to transition routes, it should call |
|
any |
For a "Host" app that embeds other platform apps via iframes, after authenticating the user and creating a UserSession, the app can then enable "post message" style authentication by calling this method. Internally this adds an event listener on window for the |
|
RequestCredentials |
Returns the proper [ |
|
Promise<any> |
Returns information about the currently logged in user's portal. Subsequent calls will not result in additional web traffic.
|
|
string |
Determines the root of the ArcGIS Server or Portal for a given URL. |
|
Promise<string> |
Gets an appropriate token for the given URL. If |
|
Promise<IUser> |
Returns information about the currently logged in user. Subsequent calls will not result in additional web traffic.
|
|
Promise<string> |
Returns the username for the currently logged in user. Subsequent calls will not result in additional web traffic. This is also used internally when a username is required for some requests but is not present in the options. session.getUsername() .then(response => { console.log(response); // "casey_jones" })
|
|
Promise<UserSession> |
Manually refreshes the current |
|
string | |
|
ICredential |
Returns authentication in a format useable in the ArcGIS API for JavaScript.
|
|
IUserSessionOptions | |
|
Promise<IAppAccess> |
Get application access information for the current user
see |
beginOAuth2
StaticBegins a new browser-based OAuth 2.0 sign in. If options.popup
is true
the
authentication window will open in a new tab/window and the function will return
Promise<UserSession>. Otherwise, the user will be redirected to the
authorization page in their current tab/window and the function will return undefined
.
Parameters
Parameter | Type | Default | Notes |
---|---|---|---|
options Required | IOAuth2Options | ||
win Optional | any | window |
Returns
completeOAuth2
StaticCompletes a browser-based OAuth 2.0 sign in. If options.popup
is true
the user
will be returned to the previous window. Otherwise a new UserSession
will be returned. You must pass the same values for options.popup
and
options.portal
as you used in beginOAuth2()
.
Parameters
Parameter | Type | Default | Notes |
---|---|---|---|
options Required | IOAuth2Options | ||
win Optional | any | window |
Returns
exchangeAuthorizationCode
StaticCompletes the server-based OAuth 2.0 sign in process by exchanging the authorizationCode
for a access_token
.
Parameters
Parameter | Type | Default | Notes |
---|---|---|---|
options Required | IOAuth2Options | ||
authorizationCode Required | string |
Returns
fromCredential
StaticTranslates authentication from the format used in the ArcGIS API for JavaScript.
Parameters
Parameter | Type | Default | Notes |
---|---|---|---|
credential Required | ICredential |
Returns
UserSession
UserSession.fromCredential({
userId: "jsmith",
token: "secret"
});
fromParent
StaticRequest session information from the parent application
Parameters
Parameter | Type | Default | Notes |
---|---|---|---|
parentOrigin Required | string |
origin of the parent frame. Passed into the embedded application as |
|
win Optional | any |
Returns
When an application is embedded into another application via an IFrame, the embedded app can
use window.postMessage
to request credentials from the host application. This function wraps
that behavior.
The ArcGIS API for Javascript has this built into the Identity Manager as of the 4.19 release.
Note: The parent application will not respond if the embedded app's origin is not:
- the same origin as the parent or *.arcgis.com (JSAPI)
- in the list of valid child origins (REST-JS)
disablePostMessageAuth
For a "Host" app that has embedded other platform apps via iframes, when the host needs
to transition routes, it should call UserSession.disablePostMessageAuth()
to remove
the event listener and prevent memory leaks
Parameters
Returns
enablePostMessageAuth
For a "Host" app that embeds other platform apps via iframes, after authenticating the user and creating a UserSession, the app can then enable "post message" style authentication by calling this method.
Parameters
Parameter | Type | Default | Notes |
---|---|---|---|
validChildOrigins Required | string[] | string |
Array of origins that are allowed to request authentication from the host app |
|
win Optional | any |
Returns
Internally this adds an event listener on window for the message
event
getDomainCredentials
Returns the proper [credentials
] option for fetch
for a given domain.
See trusted server.
Used internally by underlying request methods to add support for specific security considerations.
Parameters
Returns
"include" or "same-origin"
getPortal
Returns information about the currently logged in user's portal. Subsequent calls will not result in additional web traffic.
Parameters
Parameter | Type | Default | Notes |
---|---|---|---|
requestOptions Optional | IRequestOptions |
Options for the request. NOTE: |
Returns
A Promise that will resolve with the data from the response.
session.getPortal()
.then(response => {
console.log(portal.name); // "City of ..."
})
getServerRootUrl
Determines the root of the ArcGIS Server or Portal for a given URL.
Parameters
Returns
getToken
Gets an appropriate token for the given URL. If portal
is ArcGIS Online and
the request is to an ArcGIS Online domain token
will be used. If the request
is to the current portal
the current token
will also be used. However if
the request is to an unknown server we will validate the server with a request
to our current portal
.
Parameters
Parameter | Type | Default | Notes |
---|---|---|---|
url Required | string | ||
requestOptions Optional | ITokenRequestOptions |
Returns
getUser
Returns information about the currently logged in user. Subsequent calls will not result in additional web traffic.
Parameters
Parameter | Type | Default | Notes |
---|---|---|---|
requestOptions Optional | IRequestOptions |
Options for the request. NOTE: |
Returns
A Promise that will resolve with the data from the response.
session.getUser()
.then(response => {
console.log(response.role); // "org_admin"
})
getUsername
Returns the username for the currently logged in user. Subsequent calls will not result in additional web traffic. This is also used internally when a username is required for some requests but is not present in the options.
Returns
session.getUsername() .then(response => { console.log(response); // "casey_jones" })
refreshSession
Manually refreshes the current token
and tokenExpires
.
Parameters
Parameter | Type | Default | Notes |
---|---|---|---|
requestOptions Optional | ITokenRequestOptions |
Returns
toCredential
Returns authentication in a format useable in the ArcGIS API for JavaScript.
Returns
ICredential
esriId.registerToken(session.toCredential());
validateAppAccess
Get application access information for the current user
see validateAppAccess
function for details
Parameters
Returns
Class defined in packages/arcgis-rest-auth/src/UserSession.ts:259
Begins a new server-based OAuth 2.0 sign in. This will redirect the user to the ArcGIS Online or ArcGIS Enterprise authorization page.