@@ -55,28 +55,22 @@ var currentGistView = document.getElementById('currentgist');
55
55
---------------------------------------------------- */
56
56
57
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 ;
58
+ if ( window . location . href . match ( / \? c o d e = ( . * ) / ) ) {
59
+ // Code for matching URL param from https://github.com/prose/gatekeeper
60
+ let tempCode = window . location . href . match ( / \? c o d e = ( . * ) / ) [ 1 ] ;
61
61
62
62
// Remove parameter from URL, updating this entry in the client's browser history
63
63
history . replaceState ( null , '' , '/' ) ;
64
64
65
65
// TODO: show loading animation while waiting???
66
- // TODO: refactor getAllUrlParams(), don't need it, just need ONE param!
67
66
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
67
+ // Send tempCode to server in exchange for GitHub access token
68
+ get ( '/github-auth?code=' + tempCode ) . then ( function ( access_token ) {
69
+ // Save to local state
73
70
currentAccessToken = access_token ;
74
-
75
- // Authenticate with GitHub!
76
- getJSON ( 'https://api.github.com/user?access_token=' + currentAccessToken )
77
- . then ( loginUser ) . catch ( handleError ) ;
78
-
79
- } , handleError ) . catch ( handleError ) ;
71
+ // Get user data
72
+ return getJSON ( 'https://api.github.com/user?access_token=' + currentAccessToken ) ;
73
+ } ) . then ( loginUser ) . catch ( handleError ) ;
80
74
81
75
// Otherwise, if user has not yet started the login process,
82
76
} else {
@@ -609,27 +603,6 @@ function get(url) {
609
603
} ) ;
610
604
}
611
605
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
-
633
606
// Returns a promise for a POST request, similar to get() above
634
607
function postWithGitHubToken ( url , postDataObject ) {
635
608
return new Promise ( function ( succeed , fail ) {
@@ -666,70 +639,6 @@ function handleError(error) {
666
639
console . log ( "Error: " + error ) ;
667
640
} ;
668
641
669
- // Returns an object containing URL parameters
670
- // via https://www.sitepoint.com/get-url-parameters-with-javascript/
671
- function getAllUrlParams ( url ) {
672
-
673
- // get query string from url (optional) or window
674
- var queryString = url ? url . split ( '?' ) [ 1 ] : window . location . search . slice ( 1 ) ;
675
-
676
- // we'll store the parameters here
677
- var obj = { } ;
678
-
679
- // if query string exists
680
- if ( queryString ) {
681
-
682
- // stuff after # is not part of query string, so get rid of it
683
- queryString = queryString . split ( '#' ) [ 0 ] ;
684
-
685
- // split our query string into its component parts
686
- var arr = queryString . split ( '&' ) ;
687
-
688
- for ( var i = 0 ; i < arr . length ; i ++ ) {
689
- // separate the keys and the values
690
- var a = arr [ i ] . split ( '=' ) ;
691
-
692
- // in case params look like: list[]=thing1&list[]=thing2
693
- var paramNum = undefined ;
694
- var paramName = a [ 0 ] . replace ( / \[ \d * \] / , function ( v ) {
695
- paramNum = v . slice ( 1 , - 1 ) ;
696
- return '' ;
697
- } ) ;
698
-
699
- // set parameter value (use 'true' if empty)
700
- var paramValue = typeof ( a [ 1 ] ) === 'undefined' ? true : a [ 1 ] ;
701
-
702
- // (optional) keep case consistent
703
- paramName = paramName . toLowerCase ( ) ;
704
- paramValue = paramValue . toLowerCase ( ) ;
705
-
706
- // if parameter name already exists
707
- if ( obj [ paramName ] ) {
708
- // convert value to array (if still string)
709
- if ( typeof obj [ paramName ] === 'string' ) {
710
- obj [ paramName ] = [ obj [ paramName ] ] ;
711
- }
712
- // if no array index number specified...
713
- if ( typeof paramNum === 'undefined' ) {
714
- // put the value on the end of the array
715
- obj [ paramName ] . push ( paramValue ) ;
716
- }
717
- // if array index number specified...
718
- else {
719
- // put the value at that index number
720
- obj [ paramName ] [ paramNum ] = paramValue ;
721
- }
722
- }
723
- // if param name doesn't exist yet, set it
724
- else {
725
- obj [ paramName ] = paramValue ;
726
- }
727
- }
728
- }
729
-
730
- return obj ;
731
- }
732
-
733
642
function changeTurn ( ) {
734
643
gameState . turnIndex = ( gameState . turnIndex + 1 ) % gameState . players . length ;
735
644
}
0 commit comments