1.0.2 • Published 1 year ago
@opengovsg/gov-login-client v1.0.2
gov-login-client
The official TypeScript/JavaScript client for gov-login
This SDK is a fork of the sgID SDK.
CHANGELOG
See Releases for CHANGELOG and breaking changes.
Installation
npm i @opengovsg/gov-login-client
Usage
Initialization
import GovLoginClient from '@opengovsg/gov-login-client'
const client = new GovLoginClient({
clientId: 'CLIENT-ID',
clientSecret: 'cLiEnTsEcReT',
redirectUri: 'http://localhost:3000/callback',
})
Get Authorization URL
client.authorizationUrl(state, scope, [nonce], [redirectUri])
const { url } = client.authorizationUrl(
'state',
['openid', 'myinfo.nric_number'], // or space-concatenated string
null, // defaults to randomly generated nonce if unspecified
'http://localhost:3000/other_callback', // overrides redirect uri
)
Token exchange
async client.callback(code, [nonce], [redirectUri])
const { sub, accessToken } = await client.callback(
'code', // auth code reuturned from redirect_url
null,
'http://localhost:3000/other_callback', // optional, unless overridden
)
User info
async client.userinfo(accessToken)
const { sub } = await client.userinfo('access_token')
Supported Runtime and Environment
This library depends on jose npm package which currently supports these Node.js versions.