applicaster-oauth
Version:
this modules is a express.js middle to integrate with Applicaster's oauth authentication system accounts.applicaster.com
118 lines (97 loc) • 4.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _axios = require('axios');
var _axios2 = _interopRequireDefault(_axios);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var endpoints = {
authUrl: 'https://accounts.applicaster.com/oauth/authorize?response_type=token',
userData: 'https://accounts.applicaster.com/api/v1/users/current.json',
accountData: 'https://accounts.applicaster.com/api/v1/accounts.json'
};
var ApplicasterOauth = function () {
function ApplicasterOauth(app, options) {
var _this = this;
_classCallCheck(this, ApplicasterOauth);
if (!options.client_id) {
throw 'ApplicasterOauth : no client_id provided';
}
if (!options.redirect_url) {
throw 'ApplicasterOauth : no redirect_uri provided';
}
this.client_id = options.client_id;
this.redirect_url = options.redirect_url;
app.get('/auth/applicaster/callback', function (req, res) {
res.sendFile(__dirname + '/auth.html');
});
options.user_route && app.get('/applicaster/user', function (req, res) {
var token = req.cookies.appliAuth_token;
_this.getUserData(token).then(function (user) {
return res.json(user);
}).catch(function (error) {
return res.status(500);
});
});
options.accounts_route && app.get('/applicaster/accounts', function (req, res) {
var token = req.cookies.appliAuth_token;
_this.getAccountInfo(token).then(function (accounts) {
return res.json(accounts);
}).catch(function (error) {
return res.status(500);
});
});
}
_createClass(ApplicasterOauth, [{
key: 'authenticate',
value: function authenticate() {
var _this2 = this;
return function (req, res, next) {
if (req.cookies && req.cookies.appliAuth_token) {
_this2.getUserData(req.cookies.appliAuth_token).catch(function (error) {
return console.log('error %s', error);
}).then(function () {
return next();
});
} else {
if (req.query.access_token) {
_this2.getUserData(req.query.access_token).then(function () {
res.cookie('appliAuth_token', req.query.access_token, {});
next();
}).catch(function () {
res.send(500);
});
} else {
res.redirect(endpoints.authUrl + '&client_id=' + _this2.client_id + '&redirect_uri=' + _this2.redirect_url);
}
}
};
}
}, {
key: 'getUserData',
value: function getUserData(token) {
return new Promise(function (resolve, reject) {
return token ? _axios2.default.get(endpoints.userData + '?access_token=' + token).catch(function () {
return reject('invalid token');
}).then(function (response) {
return resolve(response.data);
}) : reject('invalid token');
});
}
}, {
key: 'getAccountInfo',
value: function getAccountInfo(token) {
return new Promise(function (resolve, reject) {
return token ? _axios2.default.get(endpoints.accountData + '?access_token=' + token).catch(function () {
return reject('invalid token');
}).then(function (response) {
return resolve(response.data);
}) : reject('invalid token');
});
}
}]);
return ApplicasterOauth;
}();
exports.default = ApplicasterOauth;