OAuth2
OAuth2 — a class for managing OAuth2 authorization tokens. Singleton instance is available via the rest.oauth2 property after Frame initialization.
Automatically adds the Authorization header to every request and refreshes tokens upon receiving an authorization error (HTTP 401).
import { OAuth2 } from '@aspro-cloud/miniapp-jssdk'Properties
accessToken
Current OAuth2 access token
get accessToken(): stringevents
EventCallbacks<OAuth2EventName> object with event handlers
get events(): EventCallbacks
set events(value: EventCallbacks)expiresAt
Access token expiration date and time
get expiresAt(): Date | nullrefreshToken
Current OAuth2 refresh token
get refreshToken(): stringMethods
initialize
Initializes the singleton instance of OAuth2. If an instance already exists — updates event handlers from the provided parameters
static initialize(params?: OAuth2Params): OAuth2| Parameter | Type | Description |
|---|---|---|
params | OAuth2Params | Initialization parameters |
getInstance
Returns the current singleton instance of OAuth2. Call only after initialize()
static getInstance(): OAuth2Errors
The method will throw an error if OAuth2 has not been initialized yet.
destroy
Removes subscriptions and releases resources
destroy(): voidrefresh
Forcefully refreshes OAuth2 tokens via the platform and returns OAuth2RefreshResult with new token values
async refresh(): Promise<OAuth2RefreshResult>Method events refresh():
| Event | Parameter | Type | Description |
|---|---|---|---|
'onRefresh' | Tokens successfully refreshed | ||
data | OAuth2RefreshResult | Object with refreshed tokens |
Example
import { App, OAuth2 } from '@aspro-cloud/miniapp-jssdk'
await App.initializeFrame()
const oauth2 = OAuth2.getInstance()
// Subscribe to token refresh event
oauth2.events = {
onRefresh: (data) => {
console.log('Tokens refreshed:', data)
}
}
// Force token refresh
const result = await oauth2.refresh()const App = window.ACloudMiniApp
const frame = await App.initializeFrame()
// Subscribe to token refresh event
frame.rest.oauth2.events = {
onRefresh: (data) => {
console.log('Tokens refreshed:', data);
}
};
// Force token refresh
const result = await frame.rest.oauth2.refresh();