@accounty/oauth
v1.0.19
Published
<img src="https://cdn.jsdelivr.net/npm/@accounty/oauth/banner.png" alt="Accounty — Many providers. One interface." />
Readme
@accounty/oauth
OAuth authorization and credential refresh for Accounty providers. The default
AccountyOauthProvider export is abstract; use a concrete provider to connect a service.
Installation
yarn add @accounty/oauthCredential lifecycle
- Construct your provider with
clientId,clientSecret, andredirectUriwhere required. Octopus does not require a client secret. - Redirect users to
provider.getAuthorizeURL(state). Generate a random state and validate it in your callback. - Exchange the callback code with
provider.getOauthToken(code)and save the returned credentials securely. - Restore credentials with
provider.setCredentials(credentials)before use. Convert a serializedexpiresAtback to aDate. - Persist any credentials returned by
setCredentials(); tokens may have changed. A return value oftruemeans the supplied credentials remain in use. - Where supported, select an administration with
getAdministrations()and pass it tosetSettings().
import type AccountyOauthProvider from '@accounty/oauth'
import type { OauthCredentials } from '@accounty/oauth'
async function restoreCredentials<Settings>(
provider: AccountyOauthProvider<OauthCredentials, Settings>,
credentials: OauthCredentials,
save: (credentials: OauthCredentials) => Promise<void>
): Promise<void> {
const result = await provider.setCredentials(credentials)
if (typeof result === 'object') await save(result)
}setCredentials() refreshes tokens expiring within about one minute. Refreshing
requires the provider's client configuration and a refresh token; it does not run
in the background. Keep the authorization and token-exchange redirect URIs consistent.
Detected invalid_grant refresh responses throw RevokedException from @accounty/core.
Public types: OauthOptions, OauthCredentials, and OauthAdministration.
