Skip to content

sevenLee/oauth2-jssdk

Repository files navigation

OM Client Oauth2 SDK

om.sdk.ClientOauth2 handles to get omapi access token in client application with Oauth2 standard flow.

The service will save omapi access token to local storage.

Quickly Start SDK Demo Example

$ npm install                   # Install project dependencies
$ npm start                     # Compile and launch

Serve example app at localhost:8080.

It should prompt a fake authorize page, then please click fake login button. Then It will back to client page.

You will get omapi access token in client application localStorage. Please Check console.log and the localStorage name OM_ACCESS_TOKEN

Additional Scripts

npm run <script> Description
start Serves example app at localhost:3000. HMR will be enabled in development.
start:dev Same as npm start, but enables nodemon for the server as well.
build compiles sdk lib to dist folder.
build:prod compiles and uglify sdk lib to dist folder. it will generate .min.js file.
test run with karma test runner

Prepare Redirect Page

According to redirectUri, it should should point to an html document in your client side. it should be added the following script:

<script>
if (window.opener && typeof window.opener.oauth2Callback === "function") {
    window.opener.oauth2Callback(window.location.hash);
}
</script>

The function will trigger global function oauth2Callback with window.location.hash.

You can check the example example/oauthRedirect.html.

SDK Example

var service = new om.sdk.ClientOauth2({
    clientId: '12345',
    redirectUri: 'http://localhost:8080/redirect',
    authEndpoint: 'http://localhost:8080/authorize',
    scope: 'test_scope',
    connection: 'facebook'
});

service.authorize(function (token) {
    console.log('get token success!:', token);
}, function (error) {
    console.error('console get access token error:', error);
});

API

om.sdk.ClientOauth2(config)

Create a new instance of ClientOauth2

@constructor

@param {object} config the configs for the ClientOauth2 instance

The object includes the following properties:

clientId - {string} - client id assigned to om.oauth2-provider

redirectUri - {string} - the url for the provider to redirect users back to your application

authEndpoint - {string} - the url for the provider redirect users to Authorization Server

scope - {string|array} - the scope specifies the level of access that the client application is requesting. you can use array for multi scopes.

connection - {string} - specifies which Authorization Server want to connect

om.sdk.ClientOauth2.authorize(success, error)

Open popup that use authorize uri, it will get access token from redirect Uri query string

@param {Function} success The callback function to handle success

@param {Function} error The callback function to handle error

you can directly get accessToken string from success callback parameter:

  service.authorize(function(accessToken){
      console.log(accessToken);
  })

you can also get error message from error callback:

  service.authorize(successCallback, function(error){
      new Error("get access token error: " + error);
  });

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published