1
1
var $ = require ( 'jquery-browserify' ) ;
2
2
var _ = require ( 'underscore' ) ;
3
+ var url = require ( 'url' ) ;
3
4
4
5
var Backbone = require ( 'backbone' ) ;
5
6
var Repos = require ( '../collections/repos' ) ;
@@ -19,21 +20,23 @@ module.exports = Backbone.Model.extend({
19
20
} ,
20
21
21
22
authenticate : function ( options ) {
22
- var match ;
23
-
24
23
if ( cookie . get ( 'oauth-token' ) ) {
25
24
if ( _ . isFunction ( options . success ) ) options . success ( ) ;
26
25
} else {
27
- match = window . location . href . match ( / \? c o d e = ( [ a - z 0 - 9 ] * ) / ) ;
28
-
29
- if ( match ) {
30
- var ajax = $ . ajax ( auth . url + '/authenticate/' + match [ 1 ] , {
26
+ var parsed = url . parse ( window . location . href , true ) ;
27
+ var code = parsed . query && parsed . query . code ;
28
+ if ( code ) {
29
+ var ajax = $ . ajax ( auth . url + '/authenticate/' + code , {
31
30
success : function ( data ) {
32
31
cookie . set ( 'oauth-token' , data . token ) ;
33
-
34
- var regex = new RegExp ( "(?:\\/)?\\?code=" + match [ 1 ] ) ;
35
- window . location . href = window . location . href . replace ( regex , '' ) ;
36
-
32
+ var newHref = url . format ( {
33
+ protocol : parsed . protocol ,
34
+ slashes : parsed . slashes ,
35
+ host : parsed . host ,
36
+ pathname : parsed . pathname ,
37
+ hash : parsed . hash
38
+ } ) ;
39
+ window . location . href = newHref ;
37
40
if ( _ . isFunction ( options . success ) ) options . success ( ) ;
38
41
}
39
42
} ) ;
0 commit comments