Skip to content
/ XeroLogin Public

Sample javascript for an auth popup to connect to Xero's OAuth API

Notifications You must be signed in to change notification settings

nikz/XeroLogin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Xero Login

This is some sample JS to help you along the path of creating a script to show the Authorization screen for Xero’s API in a popup window.

It’s similar to the one in use in MinuteDock.

Usage

First, set xero_login_url. This should point to a URL on your system where you generate a request token, and then redirect the user to the Request Token’s authorize url.

In MinuteDock (a Rails app), our action looks kind of like this:


  def new
    xero_gateway = Xero::Gateway.new(OAUTH_CONSUMER_TOKEN, OAUTH_CONSUMER_SECRET)
    
    # saving these so we can use them to authorize when the user returns
    session[:request_token]  = xero_gateway.request_token.token
    session[:request_secret] = xero_gateway.request_token.secret
  
    # now that Xero supports it, you can also specify a :callback_url here.
    redirect_to xero_gateway.request_token.authorize_url
  end
   

(We’re using Tim Connor’s most excellent Xero Gateway Plugin)

Your Authorize Callback should then run the following Javascript:


  window.opener.xero_login.logged_in();
  window.close();
   

(This will close the popup window and tell XeroLogin it’s no longer needed)

You can invoke a XeroLogin by simply instantiating the class:


  new XeroLogin(false, function() {

     // this is the callback that's executed once .logged_in() is called
     alert("Connected successfully!");
   
  });
   

You can specify a token expiry time for the first parameter (Xero OAuth tokens expire after 30 minutes). If you do that, then the popup will only
be invoked if the user’s token would have expired since they’ve loaded the page.

Problems?

Questions, comments & suggestions can be directed to nik@codetocustomer.com.

About

Sample javascript for an auth popup to connect to Xero's OAuth API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published