React

This document explains how to integrate the Authentication flow from your React application

This document explains how to integrate the Authentication directly from your React application through our Javascript component to verify your users. This stage might be at the signup page of your application, or at a later point to enrich the profiles of your users. The guide assumes the preliminary steps have already been completed as explained in the initial setup section of the documentation.

We suggest you follow our best practice how to integrate Passbase in your wider system in the overview section.

Install the NPM Package

Start by installing our component and its dependencies in your React project:

npm
yarn
npm
$ npm i --save @passbase/button react-dom styled-components
yarn
$ yarn add @passbase/button react-dom styled-components

Render the Component

Import the React component from the newly installed NPM package. The package supports UMD modules, the ES import method is presented below.

src/App.js
import VerifyButton from "@passbase/button/react";

In order for the button to render correctly, you'll need to pass your previously generated apiKey and a onFinished callback as arguments to the rendering function:

apiKey

The Passbase API Key you obtained from the dashboard

onFinished

error: A string with the reason why the user didn't complete the authentication flow

authKey:The UUID of the completed authentication

additionalAttributes: The Javascript object originally passed to the component

src/App.js
function App() {
return (
<div className="App">
<VerifyButton
apiKey={"YOUR_API_KEY"}
onFinished={(error, authKey, additionalAttributes) => {}}
/>;
</div>
);
}

Make sure you're using a publishable API key for this client side integration

The button should now render as shown here:

Speed Up the Verification Flow

During the verification flow, your users will be asked to provide some information which you might already possess in your application. In this case, you should provide a prefillAttributes object containing the following information:

Value

prefillAttributes

email: Your user's email. This will enable skipping the email step.

country: You user's nationality. This will help to pre-select the language and ID origin of your user.

<VerifyButton
apiKey={"YOUR_API_KEY"}
onFinished={(error, authKey, additionalAttributes) => {}}
prefillAttributes={{
email: "user@email.de",
country: "de"
}}
/>

Customize the Aspect of the iframe

Our component currently supports a set of customization options which will influence the appearance of the modal. The feature set is bound to grow in the future, subscribe to our newsletter to receive immediate updates upon release.

theme

accentColor: The main color of the main UI components in the flow (button, progress bar)

font: The CSS font-family to customize all text fields present in the UI

fontURL: A URL where to fetch the specified font (if necessary)

buttonColorPreset: Theme color of the "Verify me" button (applicable values: "dark-blue", "mint"). Leave empty for default color.

<VerifyButton
apiKey={"YOUR_API_KEY"}
onFinished={(error, authKey, additionalAttributes) => {}}
theme={{
accentColor: "#ff0000",
font: "Fira",
fontURL: "https://fonts.googleapis.com/css?family=Exo:400,500,600,700,750,800",
buttonColorPreset: "dark-blue"
}}
/>

The configuration above would have the following result:

The modal also supports a darkMode attribute, which supersedes the effects of any other theme attribute

theme

darkMode: A boolean value indicating whether or not to enable the dark theme

systemDarkMode: A boolean value indicating whether the dark mode setting of the users OS should be applied (see https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme)

<VerifyButton
apiKey={"YOUR_API_KEY"}
onFinished={(error, authKey, additionalAttributes) => {}}
theme={{
darkMode: true,
systemDarkMode: true
}}
/>;

Reference Your Users

In order for your application to interoperate with Passbase, you need to add a reference to your users once the Authentication is completed. Two options are available:

  1. Keep track of the authKey returned by the onFinished callback and associate it in your backend with your users' id

  2. Pass an additionalAttributes object to the component from your application to retrieve through our API once the Authentication is completed

You are free to choose whichever method suits your application best. Both are described below.

onFinished method

const userId = "SOME_USER_ID"
const onFinished = (error, authKey, additionalAttributes) => {
if(error){
console.error(error)
}else{
fetch(
'/new_authentication', // You have to provide a similar endpoint in your app
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
'authentication_key': authKey,
'user_id': userId
})
}
)
}
}
return <VerifyButton apiKey={apiKey} onFinished={onFinished}/>

additionalAttributes method

<VerifyButton
apiKey={"YOUR_API_KEY"}
onFinished={(error, authKey, additionalAttributes) => {}}
additionalAttributes={{
customer_user_id: "SOME_USER_ID"
}}
/>

Upon completion of the authentication, it will be retrievable through our API and the dashboard:

Choosing the Integration Type

This parameter specifies that you want to complete a full Authentication, meaning you are seeing this user for the first time in your application and want him to complete the full verification flow. Leave this as integrationType={"signup"}. This parameter becomes relevant in the Reauthentication section, if you want to trigger the reauthentication flow. e.g. verify that the person is still the same one, who originally signed up.

Wrap-up & Example

If you've followed this integration guide closely and you've chosen to fully customize your Passbase button, your code should resemble this code snippet:

import React from 'react';
import VerifyButton from "@passbase/button/react";
function App() {
return (
<div className="App">
<VerifyButton
apiKey={"YOUR_PUBLISHABLE_API_KEY"}
onFinished={(error, authKey, additionalAttributes) => {}}
integrationType={"signup"}
additionalAttributes={{
customer_user_id: "SOME_USER_ID"
}}
prefillAttributes={{
email: "",
country: "en"
}}
theme={{
accentColor: "",
font: "Arial"
}}
/>;
</div>
);
}
export default App;

You can find this code inside a fully working React application in our Github repositories.

You have successfully completed the Passbase React Integration! 🎉