Skip to content

Commit

Permalink
Show account identity: #36
Browse files Browse the repository at this point in the history
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
hmis-tools/hmis-api-server#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.
  • Loading branch information
cecilia-donnelly committed Jul 25, 2016
1 parent c287002 commit 45bdc9e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
32 changes: 32 additions & 0 deletions app/controllers/client.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};
1 change: 1 addition & 0 deletions app/routes/client.server.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
7 changes: 5 additions & 2 deletions app/views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions public/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,33 @@ body {
background-position: center;
}

#header {
display: flex;
}

#logo {
position: absolute;
left: 15px;
top: 22px;
left: 15px;
top: 22px;
float: left;
}

#logo img {
width: 80px;
height: 23px;
}

#account {
float: right;
display: block;
padding-left: 15px;
}

#loginInfo {
font-style: italic;
display: block;
text-align: right;
}

.clear {
clear: both;
}
Expand Down
24 changes: 24 additions & 0 deletions public/js/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

1 comment on commit 45bdc9e

@cecilia-donnelly
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See hmis-tools/hmis-api-server@3400ae1, which should have been linked in this commit message.

Please sign in to comment.