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

@xbasesdk/auth

v1.0.37-beta

Published

auth apis for xbase

Downloads

6

Readme

xbase-sdk

提供 oauth2 的登录相关功能。

主要模块

OAuth2Client oauth2 基础模块

Auth 登录相关模块

使用示例

初始化对象

import { OAuth2Client, Auth } from '@xbasesdk/auth';
// 组装需要使用的模块,下面的例子是组装 oAuth2Client(HTTP请求) 和 auth 两个
class XbaseClient {
    public oAuth2Client: OAuth2Client;
    public auth: Auth;
    constructor(initOptions: any) {
        this.oAuth2Client = new OAuth2Client(initOptions);
        this.auth = new Auth({credentialsClient: this.oAuth2Client, ...initOptions});
    }
}
const config = {
    apiOrigin: 'https://your_api_base_url',
    clientId: 'your_api_client',
};
const client = new XbaseClient(config);
// 调用API
const loginState = await client.auth.hasLoginState()

// 获取 accessToken
const accessToken =  await client.oAuth2Client.getAccessToken();

// 用 OIDC Token 用自己的API
export interface ExampleData {
    result?: string;
}
const data = await client.oAuth2Client.request<ExampleData>("https://example.com", {withCredentials:true})
console.log(data.result)

账号登录

auth.signIn({
  username: 'test',
  password: 'test',
});

手机验证码登录/注册

const phoneNumber = '10012341234';
// 短信验证码
const verificationCode = '******';
const verification = await auth.getVerification({
  phone_number: '+86 ' + phoneNumber,
  target: 'ANY',
});

let verifyResult = await auth.verify({
  verification_code: verificationCode,
  verification_id: verification ? verification.verification_id : '',
});
let isUser = verification ? verification.is_user : false;

let result;
if (isUser) {
  result = await auth.signIn({
    username: '+86 ' + phoneNumber,
    verification_code: verificationCode,
    verification_token: verifyResult.verification_token,
  });
} else {
  result = await auth.signUp({
    phone_number: '+86 ' + phoneNumber,
    password: '*******',
    verification_code: verificationCode,
    verification_token: verifyResult.verification_token,
    local: 'zh-cn',
    name: '100****1234',
  });
}

错误处理

try {
  await auth.verify({
    verification_code: verificationCode,
    verification_id: verification ? verification.verification_id : '',
  });
} catch (error) {
  if (error && error.error_uri === '/v1/auth/verification/verify') {
    switch (error.error) {
      case 'not_found': {
        result = i18n.translate('error', 'User Not Found');
        break;
      }
      case 'invalid_password': {
        if (error.details !== undefined && error.details.length > 0) {
          result =
            i18n.translate('error', 'Certification failed, you still have') +
            error.details[0].limit_remaining +
            i18n.translate('error', 'chances. You can also') +
            '' +
            i18n.translate('error', 'Reset Login Password') +
            '</a>';
        } else {
          result = i18n.translate('error', 'Password does not match');
        }
        break;
      }
      case 'user_pending': {
        result = i18n.translate('error', 'The account is not activated');
        break;
      }
      case 'user_blocked': {
        result = i18n.translate('error', 'Account has been disabled');
        break;
      }
      case 'invalid_status': {
        if (error.details !== undefined && error.details.length > 0) {
          let s = error.details[0].retry_delay;
          s = s.substring(0, s.length - 1);
          s = parseInt(s);
          result =
            i18n.translate(
              'error',
              'The password input error has reached the upper limit and the account will be locked ',
            ) +
            (s < 3600
              ? s / 60 + i18n.translate('error', 'minute')
              : s / 3600 + i18n.translate('error', 'hour'));
        } else {
          result = i18n.translate(
            'error',
            'Account has been temporarily locked',
          );
        }
        break;
      }
      case 'invalid_two_factor': {
        result = i18n.translate(
          'error',
          'Secondary authentication code does not match or has expired',
        );
        break;
      }
      case 'invalid_two_factor_recovery': {
        result = i18n.translate(
          'error',
          'Recovery code does not match or has expired',
        );
        break;
      }
      default: {
        result = i18n.translate('error', 'unknown');
        break;
      }
    }
  }
}

更新日志