### Installing Casdoor React SDK Source: https://github.com/casdoor/casdoor-react-sdk/blob/master/README.md This snippet provides commands for installing the `casdoor-react-sdk` using both npm and yarn package managers. It's the first step to integrate the SDK into a React project. ```Shell #NPM npm i casdoor-react-sdk #YARN yarn add casdoor-react-sdk ``` -------------------------------- ### Initializing Casdoor SDK in React Source: https://github.com/casdoor/casdoor-react-sdk/blob/master/README.md This snippet demonstrates how to initialize the Casdoor SDK with necessary configuration parameters such as `serverUrl`, `clientId`, `appName`, `organizationName`, `redirectPath`, and `signinPath`. This configuration is crucial for the SDK to communicate with the Casdoor server. ```TypeScript import Sdk from "casdoor-js-sdk"; export const ServerUrl = "http://localhost:7023"; const sdkConfig = { serverUrl: "http://localhost:8000", clientId: "d79fd3c4e09a309fb3f5123", appName: "application_hnpzib", organizationName: "organization_4emn5k", redirectPath: "/callback", signinPath: "/api/signin" }; export const CasdoorSDK = new Sdk(sdkConfig); ``` -------------------------------- ### Implementing Silent Sign-in with Casdoor in React Source: https://github.com/casdoor/casdoor-react-sdk/blob/master/README.md This snippet demonstrates how to use the `SilentSignin` component on a home page to perform a silent login if required. It checks if silent sign-in is needed and provides callbacks for success (`handleReceivedSilentSigninSuccessEvent`) and failure (`handleReceivedSilentSigninFailureEvent`) events. ```TSX import * as Setting from "./Setting"; import { SilentSignin, isSilentSigninRequired } from "casdoor-react-sdk"; function HomePage() { const isLoggedIn = () => { return localStorage.getItem("token") !== null; }; if (isSilentSigninRequired()) { // if the `silentSignin` parameter exists, perform silent login processing return ( { // jump to the home page here and clear silentSignin parameter window.location.href = "/"; }} handleReceivedSilentSigninFailureEvent={() => { // prompt the user to log in failed here alert("login failed"); }} /> ); } const renderContent = () => { if (isLoggedIn()) { return
Hello!
; } else { return
you are not logged in
; } }; return renderContent(); } ``` -------------------------------- ### Handling Casdoor Authentication Callback in React Source: https://github.com/casdoor/casdoor-react-sdk/blob/master/README.md This snippet shows how to use the `AuthCallback` component to intercept the `/callback` path. It handles the server interaction logic for obtaining a token after a successful authentication. It requires implementing `saveTokenFromResponse` to store the token and `isGetTokenSuccessful` to validate the server's response. ```TSX import { Route, BrowserRouter, Routes } from "react-router-dom"; import { AuthCallback } from "casdoor-react-sdk"; import * as Setting from "./Setting"; import HomePage from "./HomePage"; function App() { const authCallback = ( { // @ts-ignore // save token localStorage.setItem("token", res.data.accessToken); }} isGetTokenSuccessful={(res) => { // @ts-ignore // according to the data returned by the server, // determine whether the `token` is successfully obtained through `code` and `state`. return res.success === true; }} /> ); return ( } /> ); } export default App; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.