@kyberswap/oauth2
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Sdk authentication

  • Support sign in with google, email, ethereum, anonymous
  • Provide functions (KyberOauth2Api) to call API, auto attach access token, refresh token if expired

How to install local

  • Create file .npmrc in root folder, with content:
@kybernetwork:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=your_token

Install via git workflow

  • Add registry-url,scope and NODE_AUTH_TOKEN
   ...
   - name: Set up Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
          registry-url: 'https://npm.pkg.github.com'
          scope: '@kybernetwork'
   ...

   - name: Install dependencies
      uses: bahmutov/npm-install@HEAD
      env:
         NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} ### authen npm
   ...

How to use

Initialize SDK

  • Make sure call these lines once
import KyberOauth2 from '@kybernetwork/oauth2'

const clientConfig = {
  clientId:'b6f36b76-77b3-4f67-b756-1e49df71fda3', // register with our admin to have an id
  redirectUri: `http://127.0.0.1:5004/callback` // redirect to this url after successful sign-in. (optional)
}

KyberOauth2.initialize(clientConfig)

try {
  const data = await KyberOauth2.getSession()
  console.log(data.userInfo)  // user already login
} catch (error) {
  KyberOauth2.authenticate() // it will redirect you to login gateway to sign in with google/ethereum/email/...

  // or sign in anonymous
  const data = await KyberOauth2.loginAnonymous()
  console.log(data.userInfo) // this is anonymous user info
}

Call protected API with SDK

  • Make sure to Initialize SDK first
  • Use KyberOauth2Api.get(...), KyberOauth2Api.post(...) and KyberOauth2Api.call(...) instead of cal axios or fetch
  • KyberOauth2Api will attach access token and it will auto refresh token if token expired.

How to publish this package

Development Mode

  • KyberOauth2.initialize({ mode: DEVELOPMENT_MODE, ... })
  • Please set mode to development or staging if you want to test, if not provided, mode will be production

Readme

Keywords

none

Package Sidebar

Install

npm i @kyberswap/oauth2

Weekly Downloads

2

Version

1.0.2

License

MIT

Unpacked Size

628 kB

Total Files

20

Last publish

Collaborators

  • viet-nv