Commits

Tom Morton committed cafa415

work on the login interface

Comments (0)

Files changed (6)

     };
 
     Router.prototype.needLogin = function() {
-      return this.navigate("login");
+      return this.navigate("login", {
+        trigger: true
+      });
     };
 
     Router.prototype.routes = {
-define([],function() {
-  var MW = {
-    api: "http://en.wikipedia.org/w/api.php",
+// Generated by CoffeeScript 1.3.3
+var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
 
-    get: function(data,success,fail)
-    {
-      $.ajax({url:this.api,data:data,success:success})
-    },
+define([], function() {
+  var MW;
+  MW = (function() {
 
-    checkLogin: function(success,fail)
-    {
-      $.ajax({url:this.api,data:{action:"query",meta:"userinfo",format:"json"},success:function(data) { if(data.query.userinfo.id == 0) { fail() } else { success() } },fail:fail})
-    },
+    function MW() {
+      this.loginWithToken = __bind(this.loginWithToken, this);
 
-    loginWithToken: function(user,pass,token)
-    {
-    	$.ajax({
-	      type: 'POST',
-	      url:this.api,
-	      data:{action:"login",lgname:user,lgpassword:pass,lgtoken:token,format:"json"},
-	      success:function(data) {  
-		     console.log(data)
-		}});
-    },
-
-    doLogin: function(user,pass)
-    {
-      $.ajax({
-      	type: 'POST',
-      	url:this.api,
-      	data:{action:"login",lgname:user,lgpassword:"xx",format:"json"},
-      	success: function(data) { MW.loginWithToken(user,pass,data.login.token) } });
-      
     }
 
-    
+    MW.prototype.api = "http://en.wikipedia.org/w/api.php";
 
+    MW.prototype.get = function(data, success, fail) {
+      return $.ajax({
+        url: this.api,
+        data: data,
+        success: success
+      });
+    };
 
-  }
-  return MW
+    MW.prototype.checkLogin = function(success, fail) {
+      var _this = this;
+      return $.ajax({
+        url: this.api,
+        data: {
+          action: "query",
+          meta: "userinfo",
+          format: "json"
+        },
+        success: function(data) {
+          if (data.query.userinfo.id === 0) {
+            return fail();
+          } else {
+            return success();
+          }
+        },
+        fail: fail
+      });
+    };
+
+    MW.prototype.loginWithToken = function(user, pass, token, success, error) {
+      var _this = this;
+      return $.ajax({
+        type: 'POST',
+        url: this.api,
+        data: {
+          action: "login",
+          lgname: user,
+          lgpassword: pass,
+          lgtoken: token,
+          format: "json"
+        },
+        success: function(data) {
+          if (data.login.result === "Success") {
+            return success(data.login);
+          } else {
+            return error(data.login.result);
+          }
+        }
+      });
+    };
+
+    MW.prototype.doLogin = function(user, pass, success, error) {
+      var _this = this;
+      return $.ajax({
+        type: 'POST',
+        url: this.api,
+        data: {
+          action: "login",
+          lgname: user,
+          lgpassword: "xx",
+          format: "json"
+        },
+        success: function(data) {
+          return _this.loginWithToken(user, pass, data.login.token, success, error);
+        },
+        fail: function(data) {
+          return error("Unknown Error");
+        }
+      });
+    };
+
+    return MW;
+
+  })();
+  return new MW;
 });

js/templates/auth/login.html

     <p style="margin-bottom: 25px;" >
       You're not signed into Wikipedia; either sign in below (no login data is stored!) or visit Wikipedia, sign in, then return and refresh this page
     </p>
+    <p class="alert alert-danger hide" >
+      Error logging in: <span id="modalLoginError" ></span>
+    </p>
     <form class="form-horizontal" >
       <div class="control-group">
         <label class="control-label" for="user">Username</label>

js/views/auth/login.js

     __extends(LoginModal, _super);
 
     function LoginModal() {
+      this.completedLogin = __bind(this.completedLogin, this);
+
+      this.showError = __bind(this.showError, this);
+
       this.attemptLogin = __bind(this.attemptLogin, this);
       return LoginModal.__super__.constructor.apply(this, arguments);
     }
 
     LoginModal.prototype.attemptLogin = function(event) {
       event.preventDefault();
-      return MW.doLogin($("#user", this.el).attr("value"), $("#password", this.el).attr("value"));
+      return MW.doLogin($("#user", this.el).attr("value"), $("#password", this.el).attr("value"), this.completedLogin, this.showError);
     };
 
     LoginModal.prototype.render = function() {
       });
     };
 
+    LoginModal.prototype.showError = function(error) {
+      $("#modalLoginError", this.el).html(error);
+      return $(".alert-danger", this.el).fadeIn();
+    };
+
+    LoginModal.prototype.completedLogin = function(user) {
+      Backbone.history.navigate("watchlist", {
+        trigger: true
+      });
+      $(this.el).html("");
+      $(this.el).modal("hide");
+      this.unbind();
+      return this.destroy();
+    };
+
     return LoginModal;
 
   })(Backbone.View);

src/coffee/app.coffee

           console.log("win")
 
         needLogin: =>
-          @navigate("login")
+          @navigate("login",{trigger:true})
 
         routes:
           "watchlist": "watchlist"

src/coffee/views/auth/login.coffee

         "click .login": "attemptLogin"
       attemptLogin: (event) =>
         event.preventDefault()
-        MW.doLogin($("#user",@el).attr("value"),$("#password",@el).attr("value"))
+        MW.doLogin($("#user",@el).attr("value"),$("#password",@el).attr("value"),@completedLogin,@showError)
       render: () ->
         $(@el).html(template)
         $(@el).modal({backdrop:"static"})
+      showError: (error) =>
+        $("#modalLoginError",@el).html(error)
+        $(".alert-danger",@el).fadeIn()
+      completedLogin: (user) =>
+        Backbone.history.navigate("watchlist",{trigger:true})
+        $(@el).html("")
+        $(@el).modal("hide")
+        @unbind()
+        
+
 )