├── .gitignore ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt └── src ├── App.css ├── App.js ├── App.test.js ├── Home.js ├── Profile.js ├── index.css ├── index.js ├── logo.svg ├── reportWebVitals.js └── setupTests.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | .env 21 | 22 | npm-debug.log* 23 | yarn-debug.log* 24 | yarn-error.log* 25 | 26 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Corbado GmbH 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Passkey-First Authentication Example with Corbado 2 | 3 | This is a sample implementation of the Corbado web component being integrated into a web application built with React. 4 | 5 | Please see the [full blog post](https://www.corbado.com/blog/react-passkeys) to understand all the required steps to integrate passkeys into React apps. 6 | 7 | ## File structure 8 | 9 | - `src/App.js`: routing for the React web app 10 | - `src/Home.js`: component for the sign up / login screen 11 | - `src/Profile.js`: :component for the user profile information that is shown after successful authentication 12 | - `.env`: add Corbado project id as environment variables that you can obtain 13 | from [Corbado developer panel](https://app.corbado.com/signin#register) 14 | 15 | ## Setup 16 | 17 | ### Prerequisites 18 | 19 | Please follow the steps in [Getting started](https://docs.corbado.com/overview/getting-started) to create and configure 20 | a project in the [Corbado developer panel](https://app.corbado.com/signin#register). 21 | 22 | You need to have [Node](https://nodejs.org/en/download) and `npm` installed to run it. 23 | 24 | ## Usage 25 | 26 | Run 27 | 28 | ```bash 29 | npm i 30 | ``` 31 | 32 | to install all dependencies. 33 | 34 | Finally, you can run the project locally with 35 | 36 | ```bash 37 | export REACT_APP_CORBADO_PROJECT_ID= 38 | npm start 39 | ``` 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "passkeys-demo-react", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@corbado/react": "^2.17.0", 7 | "@testing-library/jest-dom": "^5.17.0", 8 | "@testing-library/react": "^13.4.0", 9 | "@testing-library/user-event": "^13.5.0", 10 | "react": "^18.2.0", 11 | "react-dom": "^18.2.0", 12 | "react-router-dom": "^6.14.2", 13 | "react-scripts": "5.0.1", 14 | "web-vitals": "^2.1.4" 15 | }, 16 | "scripts": { 17 | "start": "react-scripts start", 18 | "build": "react-scripts build", 19 | "test": "react-scripts test", 20 | "eject": "react-scripts eject" 21 | }, 22 | "eslintConfig": { 23 | "extends": [ 24 | "react-app", 25 | "react-app/jest" 26 | ] 27 | }, 28 | "browserslist": { 29 | "production": [ 30 | ">0.2%", 31 | "not dead", 32 | "not op_mini all" 33 | ], 34 | "development": [ 35 | "last 1 chrome version", 36 | "last 1 firefox version", 37 | "last 1 safari version" 38 | ] 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/corbado/example-passkeys-react/ecef9e46921b461226575b07ba3df1f4c5bd3702/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/corbado/example-passkeys-react/ecef9e46921b461226575b07ba3df1f4c5bd3702/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/corbado/example-passkeys-react/ecef9e46921b461226575b07ba3df1f4c5bd3702/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-spin infinite 20s linear; 13 | } 14 | } 15 | 16 | .App-header { 17 | background-color: #282c34; 18 | min-height: 100vh; 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | justify-content: center; 23 | font-size: calc(10px + 2vmin); 24 | color: white; 25 | } 26 | 27 | .App-link { 28 | color: #61dafb; 29 | } 30 | 31 | @keyframes App-logo-spin { 32 | from { 33 | transform: rotate(0deg); 34 | } 35 | to { 36 | transform: rotate(360deg); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import "./App.css" 2 | import { CorbadoProvider } from "@corbado/react" 3 | import { createBrowserRouter, RouterProvider } from "react-router-dom" 4 | import Home from "./Home" 5 | import Profile from "./Profile" 6 | 7 | const CORBADO_PROJECT_ID = process.env.REACT_APP_CORBADO_PROJECT_ID; 8 | 9 | const RouteProvider = () => { 10 | const routes = [ 11 | { 12 | path: '/', 13 | element: , 14 | }, 15 | { 16 | path: '/profile', 17 | element: , 18 | }, 19 | ]; 20 | 21 | return ; 22 | }; 23 | 24 | function App() { 25 | return ( 26 |
27 | 28 | 29 | 30 |
31 | ) 32 | } 33 | 34 | export default App 35 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from '@testing-library/react'; 2 | import App from './App'; 3 | 4 | test('renders learn react link', () => { 5 | render(); 6 | const linkElement = screen.getByText(/learn react/i); 7 | expect(linkElement).toBeInTheDocument(); 8 | }); 9 | -------------------------------------------------------------------------------- /src/Home.js: -------------------------------------------------------------------------------- 1 | import "./App.css" 2 | import React from "react" 3 | import { CorbadoAuth } from "@corbado/react" 4 | import { useNavigate } from "react-router-dom" 5 | 6 | export default function Home() { 7 | const navigate = useNavigate() 8 | const onLoggedIn = () => { 9 | navigate('/profile') 10 | } 11 | 12 | return ( 13 |
14 | 17 |
18 | ) 19 | } -------------------------------------------------------------------------------- /src/Profile.js: -------------------------------------------------------------------------------- 1 | import {useNavigate} from "react-router-dom" 2 | import {useCorbadoSession, useCorbado} from "@corbado/react" 3 | 4 | export default function Profile() { 5 | const navigate = useNavigate() 6 | const {isAuthenticated, user, logout} = useCorbado(); 7 | 8 | const redirectToHome = () => { 9 | navigate("/") 10 | } 11 | const handleLogout = () => { 12 | logout() 13 | redirectToHome() 14 | } 15 | 16 | if (!isAuthenticated || !user) { 17 | return ( 18 |
19 |

You're not logged in.

20 |

21 | Please go back to{" "} 22 | 23 | home 24 | {" "} 25 | to log in. 26 |

27 |
28 | ) 29 | } else { 30 | return ( 31 |
32 |

Profile Page

33 |

34 | User-ID: {user.sub} 35 |
36 | Email: {user.email} 37 |

38 | 39 |
40 | 41 | ) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react" 2 | import ReactDOM from "react-dom/client" 3 | import App from './App.js' 4 | import "./index.css" 5 | import reportWebVitals from './reportWebVitals' 6 | 7 | const root = ReactDOM.createRoot(document.getElementById("root")) 8 | root.render() 9 | 10 | // If you want to start measuring performance in your app, pass a function 11 | // to log results (for example: reportWebVitals(console.log)) 12 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 13 | reportWebVitals() -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | --------------------------------------------------------------------------------