From 45bdc9e8bf0391fa45e0444cfa9c99457a880bc4 Mon Sep 17 00:00:00 2001 From: cecilia-donnelly Date: Mon, 25 Jul 2016 12:25:41 -0500 Subject: [PATCH] Show account identity: #36 Show the Google id associated with the logged-in user in the header of the client app. This relies on a new endpoint in the HMIS server which is coming soon, so bear with me (see https://github.com/PCNI/OpenHMIS/issues/68). I'll link back to this commit when that's done. Note, too, that the clients controller as currently written is *extremely* redundant, as noted by @kfogel at some point, and needs to be made more DRY. --- app/controllers/client.server.controller.js | 32 +++++++++++++++++++++ app/routes/client.server.routes.js | 1 + app/views/index.jade | 7 +++-- public/css/main.css | 22 ++++++++++++-- public/js/login.js | 24 ++++++++++++++++ 5 files changed, 81 insertions(+), 5 deletions(-) diff --git a/app/controllers/client.server.controller.js b/app/controllers/client.server.controller.js index a415c33..314ada2 100644 --- a/app/controllers/client.server.controller.js +++ b/app/controllers/client.server.controller.js @@ -297,3 +297,35 @@ exports.authenticateUser = function(req, res) { post_req.end() }; + +exports.getIdentity = function (req, res) { + var post_data = req.body.token; + // An object of options to indicate where to post to + var post_options = { + host: config.api.host, + port: config.api.port, + path: '/openhmis/api/v3/authenticate/externalId/', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': post_data.length + } + }; + + // Set up the request + var post_req = http.request(post_options, function(res_post) { + res_post.setEncoding('utf8'); + var data = [] + res_post.on('data', function (chunk) { + console.log('DEBUG: Response: ' + chunk); + data.push(chunk); + }); + res_post.on('end', function() { + res.send(data); + }); + }); + + // post the data + post_req.write(post_data); + post_req.end() +}; diff --git a/app/routes/client.server.routes.js b/app/routes/client.server.routes.js index e15c6cf..25b1fb7 100644 --- a/app/routes/client.server.routes.js +++ b/app/routes/client.server.routes.js @@ -10,4 +10,5 @@ module.exports = function(app) { app.route('/clients/:id').put(client.editClient); app.route('/client_id').get(client.getClientId); app.route('/authenticate').post(client.authenticateUser); + app.route('/identify').post(client.getIdentity); }; diff --git a/app/views/index.jade b/app/views/index.jade index ef1051a..1e3c849 100644 --- a/app/views/index.jade +++ b/app/views/index.jade @@ -26,8 +26,11 @@ html(lang="en") body .container - div#logo - img(src='img/OpenHMIS-logo.jpg') + div#header + div#logo + img(src='img/OpenHMIS-logo.jpg') + div#account + span#loginInfo div#login h1 Login div#warningtext diff --git a/public/css/main.css b/public/css/main.css index 75ee45c..4c8c87e 100644 --- a/public/css/main.css +++ b/public/css/main.css @@ -5,10 +5,14 @@ body { background-position: center; } +#header { + display: flex; +} + #logo { - position: absolute; - left: 15px; - top: 22px; + left: 15px; + top: 22px; + float: left; } #logo img { @@ -16,6 +20,18 @@ body { height: 23px; } +#account { + float: right; + display: block; + padding-left: 15px; +} + +#loginInfo { + font-style: italic; + display: block; + text-align: right; +} + .clear { clear: both; } diff --git a/public/js/login.js b/public/js/login.js index f78f462..1204193 100644 --- a/public/js/login.js +++ b/public/js/login.js @@ -69,11 +69,35 @@ function switchToSearch(keepResults) { $("#searchForm #addNewClient").prop("disabled", true); } $("#search").css("display", "block"); + // fill in account that was used to log in + var id_token = getIdCookie(); + getLoginInfo(id_token); $("#intake").css("display", "none"); $("#login").css("display", "none"); $("#warning").css("display", "none"); }; +/* + * Takes the id token (received from Google) and displays the + * human-readable id associated with it (usually a Google email + * address). +*/ +function getLoginInfo(token) { + var token_wrapper = {"token": token} + $.ajax({ + type: 'POST', + url: '/identify/', + data: token_wrapper, + success: function (response) { + $("#loginInfo").text("Welcome, " + response); + }, + error: function (error) { + console.log(error); + $("#loginInfo").text("Sorry, there was an error finding your account: " + error); + } + }); +} + function switchToLogin(msg) { var warningMessage = "Sorry, you are not authorized to access this content. Please log in again."; $("#search").css("display", "none");