var _ = require('./utils.js');

module.exports = {
  // Return a promise that resolves to a user ID
  // if authorization was successful
  login: function(username, pw) {
    return _.ajax({
      verb: 'POST',
      url: '/login',
      data: {
        username: username,
        password: pw
      }
    }).then(function(id) {
      document.cookie = 'user=' + id;
      return id;
    });
  },

  // Register a new user on the server
  signup: function(username, pw) {
    return _.ajax({
      verb: 'POST',
      url: '/signup',
      data: {
        username: username,
        password: pw
      }
    }).then(function(id) {
      document.cookie = 'user=' + id;
      return id;
    });
  },

  logout: function() {
    _.ajax({
      verb: 'POST',
      url: '/signup'
    });
    clearCookie();
  },

  // Return the ID of the current user
  getUserId: function() {
    return document && document.cookie.replace(/(?:(?:^|.*;\s*)user\s*\=\s*([^;]*).*$)|^.*$/, "$1");
  }
};

var clearCookie = function() {
  document.cookie = "user=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
};

@@ -722,16 +722,42 @@
 ) %7B%0A    
+clearCookie();%0A    return 
 _.ajax(%7B
@@ -790,22 +790,22 @@
  url: '/
-signup
+logout
 '%0A    %7D)
@@ -806,35 +806,16 @@
     %7D);%0A
-    clearCookie();%0A
   %7D,%0A%0A  

Fix logout