Skip to content

Commit 0f18425

Browse files
authoredFeb 21, 2017
Merge pull request prose#1014 from prose/feature/1013-url-query-parsing
Don't use regex to parse url
2 parents 9835d51 + 818d1ab commit 0f18425

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed
 

‎app/models/user.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var $ = require('jquery-browserify');
22
var _ = require('underscore');
3+
var url = require('url');
34

45
var Backbone = require('backbone');
56
var Repos = require('../collections/repos');
@@ -19,21 +20,23 @@ module.exports = Backbone.Model.extend({
1920
},
2021

2122
authenticate: function(options) {
22-
var match;
23-
2423
if (cookie.get('oauth-token')) {
2524
if (_.isFunction(options.success)) options.success();
2625
} else {
27-
match = window.location.href.match(/\?code=([a-z0-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, {
3130
success: function(data) {
3231
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;
3740
if (_.isFunction(options.success)) options.success();
3841
}
3942
});

0 commit comments

Comments
 (0)
Please sign in to comment.