Skip to content

Commit 9e52f6c

Browse files
authored
Revert "Close #15, send GitHub access token via headers"
1 parent 4128486 commit 9e52f6c

File tree

2 files changed

+13
-67
lines changed

2 files changed

+13
-67
lines changed

Diff for: public/local.js

+11-40
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,21 @@ var currentGistView = document.getElementById('currentgist');
5454
GITHUB AUTHENTICATION
5555
---------------------------------------------------- */
5656

57-
// If GitHub tempcode is available as a parameter, get access_token from server and log in!
58-
if (getAllUrlParams().tempcode) {
59-
60-
let tempCode = getAllUrlParams().tempcode;
61-
62-
// Remove parameter from URL, updating this entry in the client's browser history
63-
history.replaceState(null, '', '/');
57+
// If GitHub access_token is available as a parameter, log in!
58+
// TODO: pass the token as a header instead? can client access it that way?
59+
if (getAllUrlParams().access_token) {
60+
console.log('*********** AUTHENTICATED!!! **********');
61+
console.log('access_token from URL params: ' + getAllUrlParams().access_token);
6462

6563
// TODO: show loading animation while waiting???
66-
// TODO: refactor getAllUrlParams(), don't need it, just need ONE param!
6764

68-
// Send tempCode to server in exchange for GitHub access token sent via headers
69-
getTokenFromServer(tempCode)
70-
.then(function(access_token){
71-
72-
// Save the access token as a global variable for now
73-
currentAccessToken = access_token;
74-
75-
// Authenticate with GitHub!
76-
getJSON('https://api.github.com/user?access_token=' + currentAccessToken)
77-
.then(loginUser).catch(handleError);
65+
// TODO: refactor getAllUrlParams(), don't need it, just need ONE param!
66+
67+
// For now, save the access token as a global variable (I'm sure this is SUPER wrong though!)
68+
currentAccessToken = getAllUrlParams().access_token;
7869

79-
}, handleError).catch(handleError);
70+
getJSON('https://api.github.com/user?access_token=' + currentAccessToken)
71+
.then(loginUser).catch(handleError);
8072

8173
// Otherwise, if user has not yet started the login process,
8274
} else {
@@ -609,27 +601,6 @@ function get(url) {
609601
});
610602
}
611603

612-
function getTokenFromServer(tempCode) {
613-
return new Promise(function(succeed, fail) {
614-
var req = new XMLHttpRequest();
615-
req.open("GET", '/github-token', true);
616-
617-
// Set header:
618-
req.setRequestHeader('GitHub-Temp-Code', tempCode);
619-
620-
req.addEventListener("load", function() {
621-
if (req.status < 400)
622-
succeed(req.getResponseHeader('GitHub-Token'));
623-
else
624-
fail(new Error("Request failed: " + req.statusText));
625-
});
626-
req.addEventListener("error", function() {
627-
fail(new Error("Network error"));
628-
});
629-
req.send(null);
630-
});
631-
}
632-
633604
// Returns a promise for a POST request, similar to get() above
634605
function postWithGitHubToken(url, postDataObject) {
635606
return new Promise(function(succeed, fail) {

Diff for: server.js

+2-27
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ var port = process.env.PORT || 8000; // Set the default port number to 8000, or
1111
// Use Express to serve everything in the "public" folder as static files
1212
app.use(express.static('public'));
1313

14-
// Save table of temp codes and access tokens, for sending access tokens to the corresponding clients via headers
15-
let clientTokens = {};
16-
1714
// Pass GITHUB_CLIENT_ID to client when requested (using AJAX for now)
1815
// TODO (later): mess around with templating engines and Express .render()?
1916
app.get('/github-client', function (req, res) {
@@ -51,13 +48,8 @@ function authenticateUser (req, res) {
5148

5249
// TODO (later): check the scopes, because users can authorize less than what my app requested!
5350

54-
// Save received access token to clientTokens to keep it associated with this client
55-
clientTokens[req.query.code] = JSON.parse(githubResponseBody).access_token;
56-
57-
// Redirect to home page again, with the temp code as a URL param
58-
// TODO (later): can I use server-side rendering to accomplish this also???
59-
res.redirect('/?tempcode=' + req.query.code);
60-
51+
// Redirect to home page again but now with the access token!
52+
res.redirect('/?access_token=' + JSON.parse(githubResponseBody).access_token);
6153
});
6254
});
6355

@@ -66,23 +58,6 @@ function authenticateUser (req, res) {
6658

6759
}
6860

69-
// Pass GitHub access token to corresponding client, if it matches client's temp code
70-
app.get('/github-token', function (req, res) {
71-
72-
let tempCode = req.header('GitHub-Temp-Code');
73-
74-
console.log('Request received for /github-token route for temp code: ' + tempCode);
75-
76-
if ( clientTokens.hasOwnProperty(tempCode) ) {
77-
console.log('\t Temp code MATCHES! Sending access token in response header!');
78-
res.header('GitHub-Token', clientTokens[tempCode]);
79-
}
80-
res.end(); // Double check: can I use res.end() with no body?
81-
82-
console.log("\nclientTokens:\n");
83-
console.log(clientTokens);
84-
});
85-
8661
// Activate the server and listen on our specified port number
8762
server.listen(port, function() {
8863
// Display this message in the server console once the server is active

0 commit comments

Comments
 (0)