Client
Reference doc for the OpenAuth client.
Use the OpenAuth client kick off your OAuth flows, exchange tokens, refresh tokens, and verify tokens.
First, create a client.
Kick off the OAuth flow by calling authorize
.
When the user completes the flow, exchange
the code for tokens.
And verify
the tokens.
Methods
createClient
Client
An instance of the OpenAuth client contains the following methods.
authorize
Type (redirectURI:
string
, response: “
code
”
|
“
token
”
, opts?: AuthorizeOptions
) => Promise
<
AuthorizeResult
>
Start the autorization flow. For example, in SSR sites.
This takes a redirect URI and the type of flow you want to use. The redirect URI is the location where the user will be redirected to after the flow is complete.
Supports both the code and token flows. We recommend using the code flow as it’s more secure.
This returns a URL to the auth server. You can redirect the user to the URL to start the OAuth flow.
For SPA apps, we recommend using the PKCE flow.
This returns a redirect URL and a challenge that you need to use later to verify the code.
exchange
Type (code:
string
, redirectURI: string
, verifier?: string
) => Promise
<
ExchangeSuccess
|
ExchangeError
>
Exchange the code for access and refresh tokens.
You call this after the user has been redirected back to your app after the OAuth flow.
So the code comes from the query parameter in the redirect URI. The redirect URI here is
the one that you passed in to the authorize
call when starting the flow.
If you used the PKCE flow for an SPA app, the code is returned as a part of the redirect URL hash.
You also need to pass in the previously stored challenge verifier.
This method returns the access and refresh tokens. Or if it fails, it returns an error that you can handle depending on the error.
refresh
Type (refresh:
string
, opts?: RefreshOptions
) => Promise
<
RefreshSuccess
|
RefreshError
>
Refreshes the tokens if they have expired. This is used in an SPA app to maintain the session, without logging the user out.
Can optionally take the access token as well. If passed in, this will skip the refresh if the access token is still valid.
This returns the refreshed tokens only if they’ve been refreshed.
Or if it fails, it returns an error that you can handle depending on the error.
verify
Type (subjects:
SubjectSchema
, token: string
, options?: VerifyOptions
) => Promise
<
VerifyError
|
VerifyResult
>
Verify the token in the incoming request.
This is typically used for SSR sites where the token is stored in an HTTP only cookie. And is passed to the server on every request.
This takes the subjects that you had previously defined when creating the issuer.
This can optionally take the refresh token as well. If passed in, it’ll automatically refresh the access token if it has expired.
This returns the decoded subjects from the access token. And the tokens if they’ve been refreshed.
Or if it fails, it returns an error that you can handle depending on the error.
AuthorizeOptions
AuthorizeOptions.pkce?
Type boolean
Default false
Enable the PKCE flow. This is for SPA apps.
AuthorizeOptions.provider?
Type string
The provider you want to use for the OAuth flow.
If no provider is specified, the user is directed to a page where they can select from the list of configured providers.
If there’s only one provider configured, the user will be redirected to that.
AuthorizeResult
AuthorizeResult.challenge
Type Challenge
The challenge that you can use to verify the code. This is for the PKCE flow for SPA apps.
This is an object that you stringify and store it in session storage.
AuthorizeResult.url
Type string
The URL to redirect the user to. This starts the OAuth flow.
For example, for SPA apps.
Challenge
Type Object
The challenge that you can use to verify the code.
ClientInput
ClientInput.clientID
Type string
The client ID. This is just a string to identify your app.
If you have a web app and a mobile app, you want to use different client IDs both.
ClientInput.fetch?
Type FetchLike
Optionally, override the internally used fetch function.
This is useful if you are using a polyfilled fetch function in your application and you want the client to use it too.
ClientInput.issuer?
Type string
The URL of your OpenAuth server.
ExchangeError
Returned when the exchange fails.
ExchangeError.err
The type of error that occurred. You can handle this by checking the type.
ExchangeSuccess
ExchangeSuccess.err
Type false
This is always false
when the exchange is successful.
ExchangeSuccess.tokens
Type Tokens
The access and refresh tokens.
RefreshError
Returned when the refresh fails.
RefreshError.err
The type of error that occurred. You can handle this by checking the type.
RefreshOptions
-
access?
string
RefreshOptions.access?
Type string
Optionally, pass in the access token.
RefreshSuccess
RefreshSuccess.err
Type false
This is always false
when the refresh is successful.
RefreshSuccess.tokens?
Type Tokens
Returns the refreshed tokens only if they’ve been refreshed.
If they are still valid, this will be undefined
.
Tokens
Tokens.access
Type string
The access token.
Tokens.refresh
Type string
The refresh token.
VerifyError
Returned when the verify call fails.
VerifyError.err
The type of error that occurred. You can handle this by checking the type.
VerifyOptions
VerifyOptions.fetch?
Type FetchLike
Optionally, override the internally used fetch function.
This is useful if you are using a polyfilled fetch function in your application and you want the client to use it too.
VerifyOptions.refresh?
Type string
Optionally, pass in the refresh token.
If passed in, this will automatically refresh the access token if it has expired.
VerifyResult
VerifyResult.err?
Type undefined
This is always undefined
when the verify is successful.
VerifyResult.subject
Type Subject
The decoded subjects from the access token.
Has the same shape as the subjects you defined when creating the issuer.
VerifyResult.tokens?
Type Tokens
Returns the refreshed tokens only if they’ve been refreshed.
If they are still valid, this will be undefined.