Class
Used to authenticate methods in ArcGIS REST JS with an API keys. The instance of ApiKeyManager
can be passed to IRequestOptions.authentication
to authenticate requests.
import { ApiKeyManager } from '@esri/arcgis-rest-request';
const apiKey = new ApiKeyManager.fromKey("...");
In most cases however the API key can be passed directly to the IRequestOptions.authentication
.
Implements
Constructors
constructor
Class Constructornew ApiKeyManager(options: IApiKeyOptions): ApiKeyManager
Parameters
Parameter | Type |
---|---|
options | IApiKeyOptions |
Returns
ApiKeyManager
Properties
Property | Type | Notes |
---|---|---|
portal | string | The current portal the user is authenticated with. |
Accessors
Methods
Method | Returns | Notes |
---|---|---|
clearCachedUserInfo() inherited | void | Clear the cached user infornation. Usefull to ensure that the most recent user information from |
getToken(url) | Promise<string> | Gets the current access token (the API Key). |
getUser(requestOptions?) inherited | Promise<IUser> | Returns information about the currently logged in user. Subsequent calls will not result in additional web traffic. |
getUsername() inherited | 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. |
serialize() | string | Serializes the ApiKeyManager instance to a JSON string. |
toJSON() | { portal: string; token: string; type: string; username: string } | Converts the |
deserialize(serialized) | ApiKeyManager | Deserializes a JSON string previously created with |
fromKey(apiKey) | ApiKeyManager | The preferred method for creating an instance of |
clearCachedUserInfo
clearCachedUserInfo(): void
Clear the cached user infornation. Usefull to ensure that the most recent user information from AuthenticationManagerBase.getUser
is used.
Returns
void
getToken
Class MethodgetToken(url: string): Promise<string>
Gets the current access token (the API Key).
Parameters
Parameter | Type |
---|---|
url | string |
Returns
Promise<string>
getUser
getUser(requestOptions?: IRequestOptions): Promise<IUser>
Returns information about the currently logged in user. Subsequent calls will not result in additional web traffic.
manager.getUser()
.then(response => {
console.log(response.role); // "org_admin"
})
Parameters
Parameter | Type | Notes |
---|---|---|
request | IRequestOptions | Options for the request. NOTE: |
Returns
Promise<IUser>
A Promise that will resolve with the data from the response.
getUsername
getUsername(): 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.
manager.getUsername()
.then(response => {
console.log(response); // "casey_jones"
})
Returns
Promise<string>
serialize
Class Methodserialize(): string
Serializes the ApiKeyManager instance to a JSON string.
import { ApiKeyManager } from '@esri/arcgis-rest-request';
const apiKey = new ApiKeyManager.fromKey("...")
localStorage.setItem("apiKey", apiKey.serialize());
Returns
string
The serialized JSON string.
toJSON
Class MethodtoJSON(): { portal: string; token: string; type: string; username: string }
Converts the ApiKeyManager
instance to a JSON object. This is called when the instance is serialized to JSON with JSON.stringify()
.
import { ApiKeyManager } from '@esri/arcgis-rest-request';
const apiKey = new ApiKeyManager.fromKey("...")
const json = JSON.stringify(session);
Returns
{ portal: string; token: string; type: string; username: string }
A plain object representation of the instance.
deserialize
deserialize(serialized: string): ApiKeyManager
Deserializes a JSON string previously created with ApiKeyManager.deserialize
to an ApiKeyManager
instance.
import { ApiKeyManager } from '@esri/arcgis-rest-request';
const apiKey = ApiKeyManager.deserialize(localStorage.getItem("apiKey"));
Parameters
Parameter | Type | Notes |
---|---|---|
serialized | string | The serialized JSON string. |
Returns
ApiKeyManager
The deserialized ApiKeyManager instance.
fromKey
fromKey(apiKey: string | IApiKeyOptions): ApiKeyManager
The preferred method for creating an instance of ApiKeyManager
.
Parameters
Parameter | Type |
---|---|
api | string | IApiKeyOptions |
Returns
ApiKeyManager