Trying the DaVinci module sample app for JavaScript
PingOne JavaScript
This tutorial walks you through updating the provided reactjs-todo-davinci sample app so that it connects to a PingOne tenant to authenticate a user using the PingOne sign-on with sessions DaVinci flow.
This flow allows users to register, authenticate, and verify their email address with PingOne.
What functionality does the reactjs-todo-davinci sample demonstrate?
| Feature | Supported by sample |
|---|---|
Orchestration |
|
Implementation |
|
Language |
|
DaVinci Fields |
|
DaVinci Buttons |
|
DaVinci Toolbox |
|
The reactjs-todo-davinci application is a "To-Do List" single-page application (SPA) built with ReactJS.
Its primary purpose is to demonstrate how to secure a React application by embedding a PingOne DaVinci flow directly into the user experience.
Instead of a static login form, the application initiates a DaVinci flow and dynamically renders the necessary UI components based on the collectors it receives from the server. Once a user is authenticated, they can access the protected To-Do list page, showcasing a complete, secure workflow from login to application usage.
|
This sample optionally uses the |
The application showcases several modern, best-practice patterns for integrating the DaVinci module for JavaScript within a React project:
1. Global State Management with React Context
The application uses React’s built-in Context API for managing shared state, which is a lightweight and effective alternative to larger libraries like Redux for this use case.
-
Location:
javascript/reactjs-todo-davinci/client/global-state.js -
Function: It defines a custom hook,
useGlobalStateMgmt, which encapsulates all the logic for managing shared state likeisAuthenticated,username, andtheme. TheAppContextthen makes this state and its setter functions available to any component wrapped in its provider.The
setAuthenticationWrapperfunction couples the application’s state with the DaVinci module’s functionality. When a component sets the authentication state tofalse, this wrapper interprets it as a logout action and triggerslogout(), ensuring the user’s session is properly terminated PingOne.
2. Dynamic, View-Based Routing
The application uses react-router-dom to manage navigation, with a structure that separates public and private views.
-
File:
javascript/reactjs-todo-davinci/client/router.js -
Function: The
Routercomponent defines all application routes. It makes use of a customProtectedRoutecomponent to guard access to the/todospage.This guard checks the
isAuthenticatedflag from the global context and redirects unauthenticated users to the/loginpage.
3. Component-Driven DaVinci Flow
The login experience is not hardcoded but is driven by the DaVinci flow itself.
-
File:
javascript/reactjs-todo-davinci/client/views/login.js -
Function: The
Loginview acts as a container. It renders a dedicated<Form />component (refer tojavascript/reactjs-todo-davinci/client/components/davinci-client/form.js).This
Formcomponent is responsible for initiating the DaVinci flow, listening for collectors, and dynamically rendering the appropriate input fields, such as a username and password. This architecture makes the login UI incredibly flexible, as the entire user journey can be modified in the DaVinci flow builder without changing the ReactJS code.