@kyberswap/oauth2
v1.0.2
Published
🛠 An SDK for authentication with Oauth2
Downloads
92
Readme
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
- Visit https://github.com/settings/tokens to create Classic Token if you don't have (at least has role
read:packages
) - Ping admin to add to whitelist https://github.com/orgs/KyberNetwork/packages/npm/oauth2/settings
- Run
npm install @kybernetwork/oauth2
Install via git workflow
- Add
registry-url
,scope
andNODE_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
...
- Add your repo to
Manage Actions access
in this link https://github.com/orgs/KyberNetwork/packages/npm/oauth2/settings
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(...)
andKyberOauth2Api.call(...)
instead of calaxios
orfetch
- KyberOauth2Api will attach access token and it will auto refresh token if token expired.
How to publish this package
- Create file
.npmrc
in root folder, with content same as above - Update version in package.json
- Run
npm publish
- Check here https://github.com/KyberNetwork/kyber-oauth2-sdk/pkgs/npm/oauth2
Development Mode
KyberOauth2.initialize({ mode: DEVELOPMENT_MODE, ... })
- Please set
mode
todevelopment
orstaging
if you want to test, if not provided, mode will beproduction