0.3.0 • Published 1 year ago
oauth2js-client v0.3.0
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 key | Type | Default value | Required |
---|---|---|---|
clientId | string | Y | |
oauthDomain | string | https://maikai.com | |
redirectUri | string | protocol//hostname:port | |
postLogoutRedirectUri | string | protocol//hostname:port | |
scopes | [string] | [] | |
silent | boolean | true | |
monitorSession | boolean | false | |
checkTokenRevoked | boolean | false |
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 theredirect_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 thepost_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. Setfalse
to turn off this feature (so user must re-login if access token is expired).monitorSession
: Set totrue
to able to listen to changes in user session.checkTokenRevoked
: Set totrue
to calloauthDomain/userinfo
endpoint to ensure the current cached token is not revoked.