Skip to content

Commit

Permalink
Merge pull request #1014 from prose/feature/1013-url-query-parsing
Browse files Browse the repository at this point in the history
Don't use regex to parse url
  • Loading branch information
dereklieu committed Feb 21, 2017
2 parents 9835d51 + 818d1ab commit 0f18425
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions 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');
Expand All @@ -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();
}
});
Expand Down

0 comments on commit 0f18425

Please sign in to comment.