UNPKG

domotz-remote-pawn

Version:

Domotz Agent

66 lines (55 loc) 2.6 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. **/ /** * * Created by Iacopo Papalini <ipapalini@domotz.com> on 08/09/15. */ module.exports.get = function (resourceLocator, onException, onSuccess) { var domotzUser = resourceLocator.entities.user; var webUtils = resourceLocator.utils.web; return function (req, res) { var apiMinorVersion = req.headers['x-api-minor-version']; console.info('API - (%s): %s', req.method, req.url); if (apiMinorVersion === undefined || apiMinorVersion < 1) { onException(res, {message: {error: 'Sorry, this page needs to be refreshed before proceeding.<br/><div class="dmt-buttons-block"><div class="dmt-popup-buttons"><a class="dmt-action-button" href="/"><b>REFRESH PAGE</b></a></div></div><b>If you have just created an account, it is ready and waiting for you.</b><br/>Please click the above "Refresh" button and login with your credentials.'}}); return; } domotzUser.get(webUtils.getUsername(req), webUtils.getPassword(req)).then(function (data) { onSuccess(res, data); }).catch(function (err) { onException(res, err); }); }; }; module.exports.post = function (resourceLocator, onException, onSuccess) { var domotzUser = resourceLocator.entities.user; return function (req, res) { console.info('API - (%s): %s', req.method, req.url); var user = req.query.username; var country = req.query.country; var password = req.headers['x-forward-password']; var promise = domotzUser.create(user, password, country); promise.then(function (data) { onSuccess(res, data); }).catch(function (err) { console.error(err); onException(res, err); } ); }; };