npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

casdoor-react-sdk

v1.2.0

Published

React client SDK for Casdoor

Downloads

403

Readme

casdoor-react-sdk

NPM version NPM download Github Actions Coverage Status Release Gitter

It can help you quickly build a silent sign-in application based on Casdoor.

We have an implemented example: casdoor-spring-security-react-example.

To use this sdk just follow the steps below.

Installation

#NPM
npm i casdoor-react-sdk

#YARN
yarn add casdoor-react-sdk

Use in React

Let's take a look at the following example of a silent sign-in.

First, you need to initialize the Casdoor SDK.

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);

Then, intercept the /callback path in your application, the AuthCallback component will help you automatically handle the logic of interacting with the server, you just need to make sure that your server ServerUrl implements the ${ServerUrl}/api/signin api, and takes two parameters code and state, and returns token.

Note: Here you need to implement the saveTokenFromResponse and isGetTokenSuccessful functions.

  • isGetTokenSuccessful:you need to judge from the response data whether the request is processed successfully by the server.
  • saveTokenFromResponse:when your ${ServerUrl}/api/signin api successfully returns the token, you need to save the token.
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 = (
    <AuthCallback
      sdk={Setting.CasdoorSDK}
      serverUrl={Setting.ServerUrl}
      saveTokenFromResponse={(res) => {
        // @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 (
    <BrowserRouter>
      <Routes>
        <Route path="/callback" element={authCallback} />
        <Route path="/" element={<HomePage />} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;

In your HomePage to determine whether you need to log in silently, if you need to log in silently, return the SilentSignin component, it will help you initiate a login request, and after the login is successful, it will call the handleReceivedSilentSigninSuccessEvent function, and when the login fails, it will also Call the handleReceivedSilentSigninFailureEvent function.

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 (
      <SilentSignin
        sdk={Setting.CasdoorSDK}
        isLoggedIn={isLoggedIn}
        handleReceivedSilentSigninSuccessEvent={() => {
          // 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 <div>Hello!</div>;
    } else {
      return <div>you are not logged in</div>;
    }
  };

  return renderContent();
}

export default HomePage;