├── public ├── robots.txt ├── favicon.ico ├── logo192.png ├── logo512.png ├── manifest.json └── index.html ├── src ├── setupTests.js ├── App.test.js ├── index.css ├── reportWebVitals.js ├── components │ ├── Logout.js │ ├── UserInfo.js │ └── Login.js ├── index.js ├── App.css ├── App.js ├── authConfig.js └── logo.svg ├── .gitignore ├── package.json └── README.md /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descope-sample-apps/descope-azure-b2c-react-sample/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descope-sample-apps/descope-azure-b2c-react-sample/HEAD/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/descope-sample-apps/descope-azure-b2c-react-sample/HEAD/public/logo512.png -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /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/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/components/Logout.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { useMsal } from '@azure/msal-react'; 3 | import { Button } from 'react-bootstrap'; 4 | 5 | const Logout = () => { 6 | const { instance } = useMsal(); 7 | 8 | const handleLogout = () => { 9 | instance.logoutRedirect().catch(e => { 10 | console.error(e); 11 | }); 12 | }; 13 | 14 | return ( 15 | 18 | ); 19 | }; 20 | 21 | export default Logout; -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom/client'; 3 | import './index.css'; 4 | import App from './App'; 5 | import reportWebVitals from './reportWebVitals'; 6 | import 'bootstrap/dist/css/bootstrap.min.css'; 7 | 8 | const root = ReactDOM.createRoot(document.getElementById('root')); 9 | root.render( 10 | 11 | 12 | 13 | ); 14 | 15 | // If you want to start measuring performance in your app, pass a function 16 | // to log results (for example: reportWebVitals(console.log)) 17 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 18 | reportWebVitals(); 19 | -------------------------------------------------------------------------------- /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 React from 'react'; 2 | import Login from './components/Login'; 3 | import UserInfo from './components/UserInfo'; 4 | import { Container } from 'react-bootstrap'; 5 | import { MsalProvider } from "@azure/msal-react"; 6 | import { PublicClientApplication } from "@azure/msal-browser"; 7 | import { msalConfig } from "./authConfig"; 8 | 9 | const pca = new PublicClientApplication(msalConfig); 10 | 11 | function App() { 12 | return ( 13 | 14 | 15 |

Azure AD B2C + Descope Authentication

16 | 17 | 18 |
19 |
20 | ); 21 | } 22 | 23 | export default App; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "descope-azure-b2c-react-sample", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@azure/msal-browser": "^3.6.0", 7 | "@azure/msal-react": "^2.0.8", 8 | "@testing-library/jest-dom": "^5.17.0", 9 | "@testing-library/react": "^13.4.0", 10 | "@testing-library/user-event": "^13.5.0", 11 | "bootstrap": "^5.3.2", 12 | "react": "^18.2.0", 13 | "react-bootstrap": "^2.9.2", 14 | "react-dom": "^18.2.0", 15 | "react-scripts": "5.0.1", 16 | "web-vitals": "^2.1.4" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject" 23 | }, 24 | "eslintConfig": { 25 | "extends": [ 26 | "react-app", 27 | "react-app/jest" 28 | ] 29 | }, 30 | "browserslist": { 31 | "production": [ 32 | ">0.2%", 33 | "not dead", 34 | "not op_mini all" 35 | ], 36 | "development": [ 37 | "last 1 chrome version", 38 | "last 1 firefox version", 39 | "last 1 safari version" 40 | ] 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/components/UserInfo.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { useMsal } from '@azure/msal-react'; 3 | import { Card, Button } from 'react-bootstrap'; 4 | 5 | const UserInfo = () => { 6 | const { accounts, instance } = useMsal(); 7 | const account = accounts[0] || null; 8 | 9 | console.log(account) 10 | 11 | const handleLogout = () => { 12 | instance.logoutPopup().catch(e => { 13 | console.error(e); 14 | }); 15 | }; 16 | 17 | return ( 18 | 19 | 20 | {account ? ( 21 | <> 22 | Welcome, {account.idTokenClaims.name} 23 | Email: {account.username} 24 | 27 | 28 | ) : ( 29 | No user is signed in. 30 | )} 31 | 32 | 33 | ); 34 | }; 35 | 36 | export default UserInfo; -------------------------------------------------------------------------------- /src/components/Login.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { useMsal } from '@azure/msal-react'; 3 | import { loginRequest } from '../authConfig'; 4 | import { Button } from 'react-bootstrap'; 5 | 6 | const Login = () => { 7 | const { instance, accounts } = useMsal(); 8 | 9 | const handleLogin = (loginType) => { 10 | const request = { 11 | ...loginRequest, 12 | prompt: loginType === 'popup' ? 'select_account' : 'login', 13 | }; 14 | 15 | if (loginType === 'popup') { 16 | instance.loginPopup(request).catch(e => { 17 | console.error(e); 18 | }); 19 | } else { 20 | instance.loginRedirect(request).catch(e => { 21 | console.error(e); 22 | }); 23 | } 24 | }; 25 | 26 | // If user is not logged in 27 | if (accounts.length > 0) { 28 | return null; 29 | } 30 | 31 | return ( 32 |
33 | 36 | 39 |
40 | ); 41 | }; 42 | 43 | export default Login; -------------------------------------------------------------------------------- /src/authConfig.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Configuration object to be passed to MSAL instance on creation. 3 | * For a full list of MSAL.js configuration parameters, visit: 4 | * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/configuration.md 5 | */ 6 | export const msalConfig = { 7 | auth: { 8 | clientId: "YOUR_AZURE_CLIENT_ID", // REQUIRED: You will need to supply this to make the sample app work 9 | authority: "https://YOUR_TENANT_DOMAIN.b2clogin.com/YOUR_TENANT_DOMAIN.onmicrosoft.com/AZURE_USER_FLOW", // REQUIRED: You will need to supply this to make the sample app work 10 | knownAuthorities: ['YOUR_TENANT_DOMAIN.b2clogin.com'], // Mark your B2C tenant's domain as trusted. 11 | redirectUri: 'http://localhost:3000/', // You must register this URI on Azure Portal/App Registration. Defaults to window.location.origin 12 | postLogoutRedirectUri: 'http://localhost:3000/', // Indicates the page to navigate after logout. 13 | navigateToLoginRequestUrl: false, // If "true", will navigate back to the original request location before processing the auth code response. 14 | }, 15 | cache: { 16 | cacheLocation: 'sessionStorage', // Configures cache location. "sessionStorage" is more secure, but "localStorage" gives you SSO between tabs. 17 | storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge 18 | }, 19 | }; 20 | 21 | export const loginRequest = { 22 | scopes: ["openid", "profile"], 23 | }; -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Azure B2C and Descope Banner](https://github.com/descope-sample-apps/descope-azure-b2c-react-sample/assets/32936811/425ac418-b8d6-4031-bafd-808bfc3e51bb) 2 | 3 | --- 4 | 5 | # Azure AD B2C + Descope React Sample App 6 | 7 | This open-source sample app, built with React, demonstrates integration with Azure AD B2C and Descope for authentication. The application showcases the use of Azure AD B2C for user authentication in a React application, leveraging MSAL React hooks. 8 | 9 | Before running the app, you'll need to configure Azure AD B2C and Descope as per the instructions provided [here](https://docs.descope.com/knowledgebase/sso/azureoidc/). 10 | 11 | ## Table of Contents 📝 12 | 13 | 1. [Features](#features) 14 | 2. [Installation](#installation) 15 | 3. [Running the Application](#running-the-application) 16 | 4. [Configuring Azure AD B2C and Descope](#configuring-azure-ad-b2c-and-descope) 17 | 5. [Issue Reporting](#issue-reporting) 18 | 6. [License](#license) 19 | 20 | ## Features ✨ 21 | 22 | - **Azure AD B2C Integration**: Seamless integration with Azure AD B2C for user authentication. 23 | - **Descope Authentication**: Leverage Descope as a federated identity provider. 24 | - **React MSAL Usage**: Utilization of MSAL React hooks for handling authentication flows. 25 | 26 | ## Installation 💿 27 | 28 | Clone the repository: 29 | 30 | ```bash 31 | git clone https://github.com/descope-sample-apps/azure-b2c-react-sample-app.git 32 | ``` 33 | 34 | Install dependencies: 35 | 36 | ```bash 37 | npm install 38 | # or 39 | yarn install 40 | ``` 41 | 42 | ## Configuring Authentication Settings 43 | 44 | Open `src/authConfig.js` and replace the placeholder values with your actual Azure AD B2C settings: 45 | 46 | - **clientId**: Your Azure AD B2C application client ID. 47 | - **authority**: Your Azure AD B2C authority URL. 48 | - **knownAuthorities**: Your Azure AD B2C tenant domain. 49 | 50 | ## Running the Application 🚀 51 | 52 | To start the application: 53 | 54 | ```bash 55 | npm start 56 | # or 57 | yarn start 58 | ``` 59 | 60 | Navigate to `http://localhost:3000/` in your browser. 61 | 62 | ## Configuring Azure AD B2C and Descope 63 | 64 | Ensure you have correctly configured Azure AD B2C and Descope. Follow the detailed guide [here](https://docs.descope.com/knowledgebase/sso/azureoidc/) for instructions on setting up Azure AD B2C Tenant, registering the application, and configuring Descope as an Identity Provider. 65 | 66 | ## Issue Reporting ⚠️ 67 | 68 | For any issues or suggestions, please [open an issue](https://github.com/descope-sample-apps/azure-b2c-react-sample-app/issues) on GitHub. 69 | 70 | ## License 📜 71 | 72 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 73 | --------------------------------------------------------------------------------