Skip to content

User Authentication

David Waring edited this page Nov 11, 2017 · 3 revisions

User Authentication Example

See the project's README for general information on the User Authentication process.

The following example generates an Auth URL to be given to the User. The User will authenticate themselves on the RTM website and grant access to the API application. Once the User has granted access, an Auth Token will be requested and if successful an RTMUser will be returned, which includes the User's information and an Auth Token. The RTMUser can then be used to make user-authenticated API requests.

const RTM = require('rtm-api');

// Get API credentials from the RTM website
const API_KEY = "xyz";
const API_SECRET = "abc";

// Create an API Client with your API credentials and desired permissions
let client = new RTM(API_KEY, API_SECRET, RTM.PERM_DELETE);

// Get an Auth URL to give to the user
client.auth.getAuthUrl(function(err, url, frob) {

  // Error: could not get Auth URL
  if ( err ) {
    console.error(err.toString());
    process.exit(1);
  }

  // Display the URL and have User open it
  console.log("Please grant access to your RTM account using the following link:");
  console.log(url);

  // Wait for User to grant access
  process.stdin.resume();
  process.stdout.write("Press [enter] when complete:");
  process.stdin.once('data', function(data) {
    process.stdin.pause();

    // Get an Auth Token for the user with the authorized frob
    client.auth.getAuthToken(frob, function(err, user) {
      
      // Error: Could not get Auth Token
      if ( err ) {
        console.error(err.toString());
        process.exit(1);
      }

      // User details
      console.log(user);

      // Optionally verify the AuthToken
      client.auth.verifyAuthToken(user.authToken, function(err, verified) {
        
        // Error: Could not verify Auth Token
        if ( err ) {
          console.error(err.toString());
          process.exit(1);
        }

        console.log("VERIFIED: " + verified);
        process.exit(0);

      });
    });
  });
});
Clone this wiki locally