kdp

1.1.5 • Public • Published

KDP Node SDK

This Node SDK allows you to use KDP as an authentication mechanism for your application or API.

Pre requisites

You'll need a few things to get started, firstly you'll have to create a new app on the KDP dev site, go to the URL below and create your app..

http://dev.researchplatform.tnsglobal.com/#/RegisterApp

Make sure you add the following for Url and Redirect URL, then you will be taken to your App Management page.

URL: http://localhost:8080/ Redirect URL: http://localhost:8080/TokenHandler

And go!

Initialise a new Node project, follow the onscreen instructions.

npm init

Install the kdp package and the express package

npm install kdp --save
npm install express --save

Then create an index.js file and code add the following code, remember to replace the APP ID and APP SECRET from your App Management pages on the KDP site.

// require the needed express and kdp modules
var express = require('express');
var kdpMod = require('kdp');
 
// set up your KDP config, you can grab these details from app management on the KDP site.
var kdpConfig = {
    https: false,
    url: "dev.researchplatform.tnsglobal.com",
    app_id: "---INSERT YOUR APP ID HERE---",
    app_secret: "---INSERT YOUR APP SECRET HERE---",
    app_name: "My Node app"
}
 
// create a new instance of the KDP module helpers
var kdp = kdpMod(kdpConfig);
 
// set up your express app
app = express();
 
// bind KDP in to the app, this will do all of the redirection and authentication require
// it basically creates the cookies we need to identify a user
kdp.init(app);
 
// Ok, lets set up an endpoint, this will just be route on your express app with some KDP code inside
//
//   Requirements:
//      The user must be authenticated
//      The user must have BasicAccess permission
//
app.get('/', function (req, res) {
 
    // use KDP to authorize and check permission
    kdp.authorize(req, res, "BasicAccess", function(user){
 
        // log the fact the user has logged in to KDP
        kdp.log(user.first_name + ' ' + user.last_name + ' just logged in', user.user_id);
 
        // send the response and say 'Hello' to your user
        res.send('Hello ' + user.first_name + ' ' + user.last_name + '!');
    });
});
 
// finally start up the server
app.listen(8080, function () {
    console.log('Example KDP app listening on port 8080!');
});

Finally, try out the magic!

node index.js

What next?

Try the Node.js KDP API Template here...

https://github.com/TNSGlobal/kdp-node-api-template

Readme

Keywords

none

Package Sidebar

Install

npm i kdp

Weekly Downloads

5

Version

1.1.5

License

ISC

Last publish

Collaborators

  • daviddykeuk