From 818d1ab38f021d8ff84d3c2eb8789bc54863be45 Mon Sep 17 00:00:00 2001
From: Derek Lieu
Date: Tue, 21 Feb 2017 14:22:00 +0000
Subject: [PATCH] Don't use regex to parse url
---
app/models/user.js | 23 +++++++++++++----------
1 file changed, 13 insertions(+), 10 deletions(-)
diff --git a/app/models/user.js b/app/models/user.js
index fa055a5e8..bf00c7803 100644
--- a/app/models/user.js
+++ b/app/models/user.js
@@ -1,5 +1,6 @@
var $ = require('jquery-browserify');
var _ = require('underscore');
+var url = require('url');
var Backbone = require('backbone');
var Repos = require('../collections/repos');
@@ -19,21 +20,23 @@ module.exports = Backbone.Model.extend({
},
authenticate: function(options) {
- var match;
-
if (cookie.get('oauth-token')) {
if (_.isFunction(options.success)) options.success();
} else {
- match = window.location.href.match(/\?code=([a-z0-9]*)/);
-
- if (match) {
- var ajax = $.ajax(auth.url + '/authenticate/' + match[1], {
+ var parsed = url.parse(window.location.href, true);
+ var code = parsed.query && parsed.query.code;
+ if (code) {
+ var ajax = $.ajax(auth.url + '/authenticate/' + code, {
success: function(data) {
cookie.set('oauth-token', data.token);
-
- var regex = new RegExp("(?:\\/)?\\?code=" + match[1]);
- window.location.href = window.location.href.replace(regex, '');
-
+ var newHref = url.format({
+ protocol: parsed.protocol,
+ slashes: parsed.slashes,
+ host: parsed.host,
+ pathname: parsed.pathname,
+ hash: parsed.hash
+ });
+ window.location.href = newHref;
if (_.isFunction(options.success)) options.success();
}
});