
var housing = housing || {};

housing.auth = housing.auth || {};

housing.auth.clientId = '180343920180-41f9qsqdcf9it1poolqtqages644lgs3.apps.googleusercontent.com';

housing.auth.scopes = 'https://www.googleapis.com/auth/userinfo.email';

housing.auth.hostname = 'https://rithonorshousing.appspot.com';

housing.auth.init = function() {
    var apisToLoad;
    var loadCallback = function() {
        if(--apisToLoad == 0) {
            housing.auth.check();
        }
    };

    apisToLoad = 2;
    gapi.client.load('housing', 'v1', loadCallback, housing.auth.hostname+"/_ah/api");
    gapi.client.load('oauth2', 'v2', loadCallback);
};

housing.auth.check = function() {
    gapi.auth.authorize({
        client_id: housing.auth.clientId,
        scope: housing.auth.scopes,
        immediate: true,
        hd: "g.rit.edu",
    }, housing.auth.result);
};

housing.auth.result = function(result) {
    if(housing.app) {
        // Start the housing app with a boolean
        // indicating whether or not the user is signed in
        housing.app((result && !result.error));
    } else {
        housing.client.displayError("The application code is missing. Please contact the developers");
    }
    gapi.client.oauth2.userinfo.get().then(function(resp){
        d3.select(".username").html(null)
            .append("a")
            .on("click", housing.auth.click)
            .attr("title","Click to change accounts")
            .text(resp.result.email);
    },function(resp){
        // I don't really know what to do if we can't get the user's email...
    });
}


housing.auth.click = function(evt) {
    gapi.auth.authorize({
        client_id: housing.auth.clientId,
        scope: housing.auth.scopes,
        immediate: false,
        authuser: "",
    }, housing.auth.result);
    return false;
}

@@ -1191,24 +1191,53 @@
 ers%22);%0A    %7D
+%0A%0A    // Get the user's email
 %0A    gapi.cl
@@ -1276,32 +1276,128 @@
 function(resp)%7B%0A
+        // Put the email in the top bar so users can see which account they are logged in under%0A
         d3.selec
@@ -1618,77 +1618,185 @@
     
-// I don't really know what to do if we can't get the user's email...
+if(window.console && console.log)%7B%0A            console.log(resp.result.error);%0A        %7D%0A        // Restart app in unauthenticated mode.%0A        setTimeout(housing.app,0,false);
 %0A   

Handle OAuth2 getuserinfo failure