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

@hiforce/auth-sdk

v1.2.0

Published

基于OIDC协议的与用户中心对接的前端sdk。

Downloads

1

Readme

auth-sdk

基于OIDC协议的与用户中心对接的前端sdk。

功能

  • [x] 强制、非强制认证
  • [x] 自动刷新token
  • [x] 认证失败自动跳登录
  • [x] 退登
  • [x] 主动到登录、注册(系统可以免登访问的场景)

接入

安装依赖

yarn add @hiforce/auth-sdk
npm i @hiforce/auth-sdk

项目配置

创建一个应用全局认证client

// 必填参数
export const authClient = new AuthenticationClient({
  client_id: 'AlarmCenter',
  client_secret: '123456',
  auth_center_host: 'http://auth_center_host',
});

属性说明

| 属性key | 说明 | 必填 | |--------------------|-----------------------------------------------------------------------------------|-------| | client_id | idaas里的id,建应用的时候会生成。 | true | | client_secret | idaas里的secret,建应用的时候会生成。 | true | | auth_center_host | 使用哪个环境的idaas. 注意是公网地址, e.g. http://idaas-onlinetest.quadtalent.com | true | | redirect_uri | 认证时跳转的地址,如果不填,则认证成功后回到当前页。如果填了,认证成功后始终回到改地址,一般用于每次登录都回到首页(window.location.origin) | false | | appId | 当前应用的id,用于在idaas登录页展示定制化的登录页。(前提是该应用在idaas应该定制了登录页) | false |

认证

在需要认证的地方调用 doAuthorization 方法。一般情况下认证失败,sdk会直接将页面重定向到idaas登录页,如果你有不登录也能访问系统的需求,那么可以给这个方法传true,代表跳过认证失败的结果。

// 这是一个异步的过程,
const authResult:boolean = await client.doAuthorization(skipWhenInvalid);

最佳实践

认证需要在应用入口处,认证未结束,不要渲染UI

  const App = () => {
    const [authing, setAuthing] = useState<boolean>(true);
    useEffect(() => {
      client.doAuthorization().then((authResult:boolean)=>{
        // 如果你的应用是必须登录才能访问的,那么只有认证成功才渲染UI,sdk会自动调到登录页
        if(authResult){
          setAuthing(false)
        }

      })
    }, [])
    
    if(authing){
      return null;
    }
    return 'hello page';
  }

获取用户信息

认证成功后,可直接通过 getCurrentUser 获取用户信息,推荐将用户信息放到状态管理里

  import { getCurrentUser} from '@hiforce/auth-sdk';
  // 认证
  await authClient.doAuthorization();
  // 获取用户信息
  const user = getCurrentUser();
  // 放到redux里
  dispatch(setUserInfo(user));

用户信息变化事件

       authClient.onAuthInfoChange = (userInfo) => {
          //
    };

authClient.onAuthFail = ()=>{
  
}

认证流程

认证流程