Using createOIDCClient does a couple of things automatically:
It creates an instance of OIDCClient.
It calls silentLogin to refresh the user session.
It suppresses all errors from silentLogin.
Create callback page
OpenID Connect / OAuth2 authorization flows require a redirect uri to return the authorization result back. Create a
page and register its url to your client's allowed redirect uris. In your page initialize OIDCClient and all you
need to do is call loginCallback method.
In the click event handler of button you created, call login method for redirecting user to provider's login page
. Make sure redirect_uri is registered on the provider, and you have created a callback handler as defined in above
.
Generally, access tokens have a short lifetime, so it is common to renew the access token before its expiration.
This feature is enabled by default, but you can disable it by passing autoSilentRenew: false to client options.
newOIDCClient({autoSilentRenew:false,...// other options})
Use different callback page for silent renew
In silent renew the library performs the flow in a hidden iframe. When you are developing a single page application,
assuming your callback page is handled by the app itself, the iframe will load your whole application after the
oauth2 redirection.
You can prevent this overhead by creating a different page which will handle silent renew only. To accomplish this you
should pass silent_redirect_uri to client options which should have your silent redirect handler page uri. If you don't use
silent_redirect_uri, redirect_uri will be used instead. Don't forget to include it to your providers redirect uri whitelist.
Don't forget to include offline_access in your scope for retrieving refresh tokens. If there is not any refresh
token stored locally, the library will fallback to using silent authorization request.
Login with popup
Create a button to trigger login.
<buttonid="loginWithPopup">Login</button>
Attach event listener and call loginWithPopup method of your initialized oidc client.
Most browsers block popups if they are not happened as a result of user actions. In order to display
login popup you must call `loginWithPopup` in an event handler listening for a user action like button click.
Additional methods
You can access user, access token, refresh token, id token and scopes with followings. Using getter methods are always the
safe bet as they will read from store. Direct access of those variables may result unexpectedly if you modify them in your app.
Direct variables are created by listening the user_login and user_logout events.
Get User
const user =await oidcClient.getUser();// orconst user = oidcClient.user