Skip to content

CodeProvider

Reference doc for the `CodeProvider`.

Configures a provider that supports pin code authentication. This is usually paired with the CodeUI.

import { CodeUI } from "@openauthjs/openauth/ui/code"
import { CodeProvider } from "@openauthjs/openauth/provider/code"
export default issuer({
providers: {
code: CodeProvider(
CodeUI({
copy: {
code_info: "We'll send a pin code to your email"
},
sendCode: (claims, code) => console.log(claims.email, code)
})
)
},
// ...
})

You can customize the adapter using.

const ui = CodeUI({
// ...
})
export default issuer({
providers: {
code: CodeProvider(
{ ...ui, length: 4 }
)
},
// ...
})

Behind the scenes, the CodeProvider expects callbacks that implements request handlers that generate the UI for the following.

CodeProvider({
// ...
request: (req, state, form, error) => Promise<Response>
})

This allows you to create your own UI.


Methods

CodeProvider

CodeProvider(config)

Parameters

Returns Provider

CodeProviderConfig

CodeProviderConfig.length?

Type number

Default 6

The length of the pin code.

CodeProviderConfig.request

Type (req: Request<>, state: CodeProviderState, form?: FormData<>, error?: CodeProviderError) => Promise<Response<>>

The request handler to generate the UI for the code flow.

Takes the standard Request and optionally FormData ojects.

Also passes in the current state of the flow and any error that occurred.

Expects the Response object in return.

CodeProviderConfig.sendCode

Type (claims: Record<string, string>, code: string) => Promise<void | CodeProviderError>

Callback to send the pin code to the user.

{
sendCode: async (claims, code) => {
// Send the code through the email or phone number based on the claims
}
}

CodeProviderError

Type { type: invalid_code } | { key: string, type: invalid_claim, value: string }

The errors that can happen on the code flow.

ErrorDescription
invalid_codeThe code is invalid.
invalid_claimThe claim, email or phone number, is invalid.

CodeProviderState

Type { type: start } | { claims: Record<string, string>, code: string, resend: boolean, type: code }

The state of the code flow.

StateDescription
startThe user is asked to enter their email address or phone number to start the flow.
codeThe user needs to enter the pin code to verify their claim.