Code Monkey home page Code Monkey logo

force.oauth.js's Introduction

JS Library for apps wishing to authenticate with force.com using OAuth 2.0 User-Agent flow.

Usage

<script src='jquery.js' type='text/javascript'></script>
<script src='force.oauth.js' type='text/javascript'></script>
<script type='text/javascript'>
  $(function(){
    var config = {
      //required
      clientId: 'replace_me_with_value_from_force_com_remote_access_screen',
      authorized: function(token){
        //do something with token!
      }
      //optional (default values shown)
      scope:                      'id',
      loginUrl:                   'https://login.salesforce.com',
      popup:                      true,
      cacheTokenInSessionStorage: false,
      callbackPath:               undefined,
      unauthorized:               undefined,
      error:                      undefined
    };
    force.oauth.ready(config); 
  });
</script>

View Demo & Unit Tests

https://force-oauth-js.herokuapp.com/

Configuration Properties

  • clientId (required)

    Values must be generated via salesforce.com at Setup > App Setup > Develop > Remote Access. Only one remote access application is needed for production, sandbox, or pre-release; separate entries are not necessary for My Domain.

  • authorized (required)

    Function to be called once user is authorized.

  • popup (optional)

    Do OAuth handshake in popup window.

    Default: true

  • unauthorized (optional)

    Function to be called if authorization shouldn't happen immediately. For example, if you'd like wait until the user clicks a button before authorizing:

    Example:

      unauthorized: function(){
        var signInButton = $('<button></button>').html('Authenticate');
        signInButton.click(function(){force.oauth.authorize();});
        $('#mydiv').append(signInButton);
      }

    Default: undefined

  • cacheTokenInSessionStorage (optional)

    Enabling this will persist the OAuth token in HTML5 sessionStorage. Please, please only enable this if you're positive your app has zero XSS/CSRF risks. The benefit, of course, is DOM reloads won't re-initiate OAuth hand-shake.

    Default: false

  • scope (optional)

    See Scope Parameter Values

    Default: 'id'

  • loginUrl (optional)

    If you're testing against a sandbox, change this to https://test.salesforce.com

    Default: 'https://login.salesforce.com'

  • callbackPath (optional)

    If you'd like one path in your app to handle all callbacks, set this to that path. Prevents the need for multiple clientIds for a single app.

    Default: undefined

  • error (optional)

    Function to receive callback errors if they occur; otherwise, errors will simply be thrown.

    Default: undefined

Scenarios

Scenario Popup or Same window? Cache Token in sessionStorage Fire on page load or user action
A Popup No page load
B Popup No user action
C Popup Yes page load
D Popup Yes user action
E Same Yes page load
F Same Yes user action

NOTE: two scenarios - what would be G & H - are not shown in the table above because they are invalid. To be more specific, to effectively use window.location.replace(), caching token in sessionStorage cannot be disabled.

The following screenshots diagram the scenarios enabled by this JS library.

A: Popup window, without sessionStorage, fire on page load

ScreenShot

B: Popup window, without sessionStorage, fire on user action

ScreenShot

C: Popup window, with sessionStorage, fire on page load

ScreenShot

D: Popup window, with sessionStorage, fire on user action

ScreenShot

E: Same window, with sessionStorage, fire on page load

ScreenShot

F: Same window, with sessionStorage, fire on user action

ScreenShot

force.oauth.js's People

Contributors

richardvanhook avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.