Skip to content

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).

ts
import { OAuth2 } from '@aspro-cloud/miniapp-jssdk'

Properties

accessToken

Current OAuth2 access token

ts
get accessToken(): string

events

EventCallbacks<OAuth2EventName> object with event handlers

ts
get events(): EventCallbacks
set events(value: EventCallbacks)

expiresAt

Access token expiration date and time

ts
get expiresAt(): Date | null

refreshToken

Current OAuth2 refresh token

ts
get refreshToken(): string

Methods

initialize

Initializes the singleton instance of OAuth2. If an instance already exists — updates event handlers from the provided parameters

ts
static initialize(params?: OAuth2Params): OAuth2
ParameterTypeDescription
paramsOAuth2ParamsInitialization parameters

getInstance

Returns the current singleton instance of OAuth2. Call only after initialize()

ts
static getInstance(): OAuth2

Errors

The method will throw an error if OAuth2 has not been initialized yet.

destroy

Removes subscriptions and releases resources

ts
destroy(): void

refresh

Forcefully refreshes OAuth2 tokens via the platform and returns OAuth2RefreshResult with new token values

ts
async refresh(): Promise<OAuth2RefreshResult>

Method events refresh():

EventParameterTypeDescription
'onRefresh'Tokens successfully refreshed
dataOAuth2RefreshResultObject with refreshed tokens

Example

ts
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()
js
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();

Published under the MIT license.