教程:在 React 单页应用中添加登录和注销功能组件
功能组件是 React 应用的构建基块。 本教程演示如何使用功能组件在 React 单页应用 (SPA) 中构建登录和注销体验。 useMsal
挂钩用于检索访问令牌以允许用户登录。
本教程的内容:
- 将组件添加到应用程序
- 创建显示用户配置文件信息的方法
- 创建显示登录和注销体验的布局
- 添加登录和注销体验
先决条件
- 完成教程:准备用于身份验证的应用程序中的先决条件和步骤。
添加页面布局组件
打开 PageLayout.jsx 并添加以下代码以呈现页面布局。 useIsAuthenticated 挂钩返回用户当前是否已登录。
/* * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ import React from "react"; import Navbar from "react-bootstrap/Navbar"; import { useIsAuthenticated } from "@azure/msal-react"; import { SignInButton } from "./SignInButton"; import { SignOutButton } from "./SignOutButton"; /** * Renders the navbar component with a sign in or sign out button depending on whether or not a user is authenticated * @param props */ export const PageLayout = (props) => { const isAuthenticated = useIsAuthenticated(); return ( <> <Navbar bg="primary" variant="dark" className="navbarStyle"> <a className="navbar-brand" href="/"> Microsoft Identity Platform </a> <div className="collapse navbar-collapse justify-content-end"> {isAuthenticated ? <SignOutButton /> : <SignInButton />} </div> </Navbar> <br /> <br /> <h5> <center> Welcome to the Microsoft Authentication Library For JavaScript - React SPA Tutorial </center> </h5> <br /> <br /> {props.children} </> ); };
保存文件。
显示配置文件信息
打开 ProfileData.jsx 并添加以下代码,该代码将创建用于显示用户配置文件信息的组件:
import React from "react"; /** * Renders information about the user obtained from MS Graph * @param props */ export const ProfileData = (props) => { return ( <div id="profile-div"> <p> <strong>First Name: </strong> {props.graphData.givenName} </p> <p> <strong>Last Name: </strong> {props.graphData.surname} </p> <p> <strong>Email: </strong> {props.graphData.userPrincipalName} </p> <p> <strong>Id: </strong> {props.graphData.id} </p> </div> ); };
保存文件。
添加登录体验
打开 SignInButton.jsx 并添加以下代码,它会创建一个使用弹出窗口或重定向来登录用户的按钮。
import React from "react"; import { useMsal } from "@azure/msal-react"; import { loginRequest } from "../authConfig"; import DropdownButton from "react-bootstrap/DropdownButton"; import Dropdown from "react-bootstrap/Dropdown"; /** * Renders a drop down button with child buttons for logging in with a popup or redirect * Note the [useMsal] package */ export const SignInButton = () => { const { instance } = useMsal(); const handleLogin = (loginType) => { if (loginType === "popup") { instance.loginPopup(loginRequest).catch((e) => { console.log(e); }); } else if (loginType === "redirect") { instance.loginRedirect(loginRequest).catch((e) => { console.log(e); }); } }; return ( <DropdownButton variant="secondary" className="ml-auto" drop="start" title="Sign In" > <Dropdown.Item as="button" onClick={() => handleLogin("popup")}> Sign in using Popup </Dropdown.Item> <Dropdown.Item as="button" onClick={() => handleLogin("redirect")}> Sign in using Redirect </Dropdown.Item> </DropdownButton> ); };
保存文件。
添加注销体验
打开 SignOutButton.jsx 并添加以下代码,该代码将创建一个使用弹出窗口或重定向来注销用户的按钮。
import React from "react"; import { useMsal } from "@azure/msal-react"; import DropdownButton from "react-bootstrap/DropdownButton"; import Dropdown from "react-bootstrap/Dropdown"; /** * Renders a sign out button */ export const SignOutButton = () => { const { instance } = useMsal(); const handleLogout = (logoutType) => { if (logoutType === "popup") { instance.logoutPopup({ postLogoutRedirectUri: "/", mainWindowRedirectUri: "/", }); } else if (logoutType === "redirect") { instance.logoutRedirect({ postLogoutRedirectUri: "/", }); } }; return ( <DropdownButton variant="secondary" className="ml-auto" drop="start" title="Sign Out" > <Dropdown.Item as="button" onClick={() => handleLogout("popup")}> Sign out using Popup </Dropdown.Item> <Dropdown.Item as="button" onClick={() => handleLogout("redirect")}> Sign out using Redirect </Dropdown.Item> </DropdownButton> ); };
保存文件。