Skip to content
This repository was archived by the owner on Jul 19, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions examples/client_auth/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
# Podio Password Auth
# Podio Client Auth

A simple example app for client side authentication against the Platform API.

# Installation

```
npm install
$ npm install
```

# Configuration

Make sure to add your clientId in the [config file](https://github.com/podio/podio-js/blob/master/examples/client_auth/public/javascript/config.js).

# Server
Also, go to the root of this repository and compile PodioJS by running
```
$ npm run bundle
```


# Server
```
npm start
$ npm start
```

The app is accessible through http://localhost:3000
22 changes: 11 additions & 11 deletions examples/client_auth/public/javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
function onStartAuthClick(e) {
var elmBody = document.body;

if (!platform.isAuthenticated()) {
// methods are registered globally for the popup to call on this main window
platform.isAuthenticated().catch(function (err) {
// methods are registered globally for the popup to call on this main window
window.onAuthCompleted = function() {
// the platform SDK instance from the popup has
// received a new auth token and saved it to the store.
Expand All @@ -61,7 +61,7 @@
};

openPopup();
}
});
}

/***
Expand All @@ -72,14 +72,14 @@
function onRequestUserClick(e) {
var elmBody = document.body;

if (platform.isAuthenticated()) {
platform.isAuthenticated().then(function () {
platform.request('get', '/user/status')
.then(function(responseData) {
elmBody.innerHTML = compiledUser({ profile: responseData.profile });
});
} else {
.then(function(responseData) {
elmBody.innerHTML = compiledUser({ profile: responseData.profile });
});
}).catch(function () {
elmBody.innerHTML = compiledError();
}
});
}

// Use event delegation
Expand All @@ -98,8 +98,8 @@
// replace the content with a success template
// if we had authenticated previously and auth tokens
// are available in the store
if (platform.isAuthenticated()) {
platform.isAuthenticated().then(function () {
document.body.innerHTML = compiledSuccess();
}
});

})(PodioJS, SessionStore, PlatformConfig, _);
2 changes: 1 addition & 1 deletion examples/client_auth/public/javascript/sessionStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
var podioOAuth = localStorage.getItem('podioOAuth');
if (podioOAuth) {
podioOAuth = JSON.parse(podioOAuth);
callback(podioOAuth);
}
callback(podioOAuth || {});
},
set: function(podioOAuth, authType) {
localStorage.setItem('podioOAuth', JSON.stringify(podioOAuth));
Expand Down
3 changes: 2 additions & 1 deletion examples/password_auth/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/**/*
node_modules/**/*
config.json
10 changes: 9 additions & 1 deletion examples/password_auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ A simple example app for password authentication against the Platform API.
npm install
```

Make sure to add your credentials to the [routes file](https://github.com/podio/podio-js/blob/master/examples/password_auth/routes/index.js#L6-L9).
Make sure to add your client ID, client secret, username and password in a new file ```./config.json```. It should have the following format:
```
{
"clientId": "<Your client ID>",
"clientSecret": "<Your client secret>",
"username": "<Your username>",
"password": "<Your password"
}
```

# Server

Expand Down
34 changes: 21 additions & 13 deletions examples/password_auth/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ var domain = require('domain');
var router = express.Router();
var PodioJS = require('../../../lib/podio-js');
var sessionStore = require('../sessionStore');
var fs = require('fs');

var clientId = ''; // your clientId here
var clientSecret = ''; // your clientSecret here;
var username = ''; // your username here;
var password = ''; // your password here
var config = JSON.parse(fs.readFileSync('./config.json'));

var clientId = config.clientId;
var clientSecret = config.clientSecret;
var username = config.username;
var password = config.password;
var podio = new PodioJS({ authType: 'password', clientId: clientId, clientSecret: clientSecret }, { sessionStore: sessionStore });

/* GET home page. */
router.get('/', function(req, res) {
if (podio.isAuthenticated()) {

podio.isAuthenticated().then(function () {
// ready to make API calls
res.render('success');
} else {
}).catch(function (err) {

var reqdomain = domain.create();

reqdomain.on('error', function(e) {
Expand All @@ -33,17 +38,20 @@ router.get('/', function(req, res) {
res.render('success');
});
});
}
});
});

router.get('/user', function(req, res) {
if (podio.isAuthenticated()) {
podio.request('get', '/user/status', null, function(responseData) {
res.render('user', { profile: responseData.profile });
});
} else {

podio.isAuthenticated().then(function () {
return podio.request('get', '/user/status');
})
.then(function(responseData) {
res.render('user', { profile: responseData.profile });
})
.catch(function () {
res.send(401);
}
});
});

module.exports = router;
10 changes: 6 additions & 4 deletions examples/password_auth/sessionStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ var path = require('path');
function get(authType, callback) {
var fileName = path.join(__dirname, 'tmp/' + authType + '.json');
var podioOAuth = fs.readFile(fileName, 'utf8', function(err, data) {
if (err) {
if (err.errno !== 2) { // skip file not found errors
throw new Error('Reading from the sessionStore failed');
}

// Throw errors unless it's file-not-found
if (err && err.errno !== 2) {
throw new Error('Reading from the sessionStore failed');
} else if (data.length > 0) {
callback(JSON.parse(data));
} else {
callback();
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion examples/server_auth/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/**/*
node_modules/**/*
config.json
8 changes: 7 additions & 1 deletion examples/server_auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ A simple example app for server-side authentication against the Platform API.
npm install
```

Make sure to add your clientId and clientSecret to the [routes file](https://github.com/podio/podio-js/blob/master/examples/server_auth/routes/index.js#L9-L10).
Make sure to add your client ID and client secret in a new file ```./config.json```. It should have the following format:
```
{
"clientId": "<Your client ID>",
"clientSecret": "<Your client secret>"
}
```

# Server

Expand Down
77 changes: 49 additions & 28 deletions examples/server_auth/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ var Busboy = require("busboy");
var temp = require('temp');
var fs = require('fs');

var clientId = ''; // your clientId here
var clientSecret = '' // your clientSecret here;
// Remember to place a file in this folder called 'config.json',
// with the contents formatted like so:
// {
// "clientId": "<Your client ID>",
// "clientSecret": "<Your client secret>""
// }
var config = JSON.parse(fs.readFileSync('./config.json'));

var clientId = config.clientId;
var clientSecret = config.clientSecret;

var podio = new PodioJS({ authType: 'server', clientId: clientId, clientSecret: clientSecret }, { sessionStore: sessionStore });

function getFullURL(req) {
Expand All @@ -20,12 +29,14 @@ router.get('/', function(req, res) {
var errorCode = req.query.error;
var redirectURL = getFullURL(req);

if (podio.isAuthenticated()) {
podio.isAuthenticated()
.then(function () {
// ready to make API calls
res.render('success');
} else {
}).catch(function () {

if (typeof authCode !== 'undefined') {
podio.getAccessToken(authCode, redirectURL, function () {
podio.getAccessToken(authCode, redirectURL, function (err) {
// we are ready to make API calls
res.render('success');
});
Expand All @@ -36,17 +47,21 @@ router.get('/', function(req, res) {
// we have neither an authCode nor have we authenticated before
res.render('index', { authUrl: podio.getAuthorizationURL(redirectURL) });
}
}
});
});

router.get('/user', function(req, res) {
if (podio.isAuthenticated()) {
podio.request('get', '/user/status', null, function(responseData) {
res.render('user', { profile: responseData.profile });
});
} else {

podio.isAuthenticated()
.then(function() {
return podio.request('get', '/user/status');
})
.then(function(responseData) {
res.render('user', { profile: responseData.profile });
})
.catch(function(err) {
res.send(401);
}
});
});

router.get('/upload', function(req, res) {
Expand All @@ -56,29 +71,35 @@ router.get('/upload', function(req, res) {
router.post('/upload', function(req, res) {
var busboy = new Busboy({ headers: req.headers });

if (!podio.isAuthenticated()) {
res.send(401);
return;
}
podio.isAuthenticated()
.then(function() {

busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
var dir = temp.mkdirSync();
var filePath = dir + '/' + filename;
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {

fs.writeFileSync(filePath, '');
var dir = temp.mkdirSync();
var filePath = dir + '/' + filename;

file.on('data', function(data) {
fs.appendFileSync(filePath, data);
});
fs.writeFileSync(filePath, '');

file.on('data', function(data) {
fs.appendFileSync(filePath, data);
});

file.on('end', function() {
podio.uploadFile(filePath, filename, function(body, response) {
res.render('upload_success', { fileId: body.file_id })
file.on('end', function() {
podio.uploadFile(filePath, filename)
.then(function(body, response) {
res.render('upload_success', { fileId: body.file_id })
})
.catch(function (err) {
res.end(String(err));
});
});
});
req.pipe(busboy);
})
.catch(function () {
res.send(401);
});

req.pipe(busboy);
});

module.exports = router;
10 changes: 6 additions & 4 deletions examples/server_auth/sessionStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ var path = require('path');
function get(authType, callback) {
var fileName = path.join(__dirname, 'tmp/' + authType + '.json');
var podioOAuth = fs.readFile(fileName, 'utf8', function(err, data) {
if (err) {
if (err.errno !== 2) { // skip file not found errors

// Throw error, unless it's file-not-found
if (err && err.errno !== 2) {
throw new Error('Reading from the sessionStore failed');
}
} else if (data.length > 0) {
callback(JSON.parse(data));
} else {
callback();
}
});
}
Expand All @@ -20,7 +22,7 @@ function set(podioOAuth, authType, callback) {
if (/server|client|password/.test(authType) === false) {
throw new Error('Invalid authType');
}

fs.writeFile(fileName, JSON.stringify(podioOAuth), 'utf8', function(err) {
if (err) {
throw new Error('Writing in the sessionStore failed');
Expand Down
Empty file modified examples/server_auth/tmp/server.json
100644 → 100755
Empty file.
Loading