0.3.0 • Published 1 year ago

oauth2js-client v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

OAuth2 javascript library for web apps

A Javascript Library for integrate Authorization Server to Web Application.

Note: If your want to integrate with your Backend API or Server Side Rendering Web App, this library is not for you.

Getting started

import OAuth2Client from 'oauth2js-client'

const App = props => {
  if (OAuth2Client.user.isLoggedIn())
    return <HomePageView userInfo={OAuth2Client.user.getUserInfo()}/>
  return <LoginView/>
}

const LoginView = props => (
  <p onClick={() => OAuth2Client.user.login()}>
    Login with OAuth2
  </p>
)

Installation

# With yarn
yarn add oauth2js-client

# With npm
npm install oauth2js-client

Then you can initiate the client using OAuth2Client:

import OAuth2Client from 'oauth2js-client'

OAuth2Client.init({
  clientId: 'your-client-id',
  redirectUri: 'your-redirect-uri',
  scopes: ['your', 'scopes'],
  oauthDomain: 'https://makai.com'
}).then(() => {
  // The initial phase is finish.
  // Now you can do your logic.
  const rootElement = document.getElementById('root')
  ReactDOM.render(<App/>, rootElement)
})

which returns a promise and resolve after finish initiating.

For advance usage, please check examples folder.

NOTE: You can also include JS SDK oauth2js-client.min.js in your source code, then access the lib using window.OAuth2Client.

Usage

async OAuth2Client.init(configs)

configs keyTypeDefault valueRequired
clientIdstringY
oauthDomainstringhttps://maikai.com
redirectUristringprotocol//hostname:port
postLogoutRedirectUristringprotocol//hostname:port
scopes[string][]
silentbooleantrue
monitorSessionbooleanfalse
checkTokenRevokedbooleanfalse
  • clientId: The ID of your client which you registered in the Authorization Server.
  • redirectUri: Your client will redirect to this uri with authorization code value after user successfully logged in. This value should match exact with one of the redirect_uris you registered in the IAM system.
  • postLogoutRedirectUri: Your client will redirect to this uri after user successfully logged out. This value should match exact with one of the post_logout_redirect_uris you registered in the IAM system.
  • scopes: The scopes of your client.
  • oauthDomain: The Authorization Server domain to use SDK.
  • silent: If the SDK auto renew access token silently when the current one is expired. Set false to turn off this feature (so user must re-login if access token is expired).
  • monitorSession: Set to true to able to listen to changes in user session.
  • checkTokenRevoked: Set to true to call oauthDomain/userinfo endpoint to ensure the current cached token is not revoked.
0.3.0

1 year ago

0.2.0

1 year ago

0.1.0

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago