Index: Dev/trunk/src/client/index.html
===================================================================
--- Dev/trunk/src/client/index.html	(revision 467)
+++ Dev/trunk/src/client/index.html	(revision 468)
@@ -30,5 +30,5 @@
         </div>
         <div id="toaster" data-dojo-type="qed-client/app/Notifications"></div>
-        <div id="loginDialog" data-dojo-type="qed-client/ui/LoginDialog"></div>
+        <div data-dojo-type="qed-client/ui/LoginDialogWrapper"></div>
     </body>
 </html>
Index: Dev/trunk/src/client/qed-client/index.js
===================================================================
--- Dev/trunk/src/client/qed-client/index.js	(revision 467)
+++ Dev/trunk/src/client/qed-client/index.js	(revision 468)
@@ -7,4 +7,5 @@
     "./store",
     "./store/request",
+    "./ui/LoginDialogWrapper",
     "./ui/MainMenu",
     "dojo/_base/array",
@@ -12,5 +13,6 @@
     "./stddeps",
     "dojo/domReady!"
-], function(Content, Page, Router, routes, session, store, request, MainMenu, array, parser) {
+], function(Content, Page, Router, routes, session, store,
+            request, LoginDialogWrapper, MainMenu, array, parser) {
 
     parser.parse();
@@ -21,8 +23,7 @@
         Router.register(route);
     });
+    Router.startup();
 
-    session.restore().then(function(){
-        Router.startup();
-    });
-    
+    session.restore();
+
 });
Index: Dev/trunk/src/client/qed-client/request.js
===================================================================
--- Dev/trunk/src/client/qed-client/request.js	(revision 468)
+++ Dev/trunk/src/client/qed-client/request.js	(revision 468)
@@ -0,0 +1,68 @@
+define([
+    "./session",
+    "dojo/Deferred",
+    "dojo/on",
+    "dojo/request"
+], function(session, Deferred, on, request) {
+
+    var user = session.get();
+    var queue = [];
+    
+    on(session, 'change', function(newUser) {
+        user = newUser;
+        if ( user ) {
+            retry();
+        }
+    });
+    
+    function retry() {
+        if (queue.length > 0) {
+            var item = queue.shift();
+            console.log("Retry",item.url);
+            real_request(item);
+        }
+    }
+    
+    function real_request(item) {
+        var req = request(item.url,item.options);
+
+        // forward successfull response
+        req.then(function(body){
+            item.dfd.resolve(body);
+        });
+
+        // handle unauthenticated and queued requests
+        req.response.then(function(response){
+            retry();
+        }, function(error) {
+            if ( error.response.status === 401 ) {
+                queue.unshift(item);
+                session.restore();
+            } else {
+                item.dfd.reject(error); // this should be error body
+                                        // not, the request?
+                retry();
+            }
+        });
+    }
+    
+    var _request = function(url, options) {
+        var item = {
+            url: url,
+            options: options,
+            dfd: new Deferred()
+        };
+        // only do the request directly if we are authenticated and
+        // there are no earlier requests queued.
+        if ( user && queue.length === 0 ) {
+            console.log("Request",url);
+            real_request(item);
+        } else {
+            console.log("Push",url);
+            queue.push(item);
+        }
+        return item.dfd.promise;
+    };
+
+    return _request;
+});
Index: Dev/trunk/src/client/qed-client/session.coffee
===================================================================
--- Dev/trunk/src/client/qed-client/session.coffee	(revision 467)
+++ Dev/trunk/src/client/qed-client/session.coffee	(revision 468)
@@ -7,6 +7,8 @@
     Session = declare [Evented],
         info: null
+
         get: () ->
             @info
+
         restore: () ->
             request '/api/login',
@@ -16,5 +18,6 @@
                 @_set res
             , () =>
-                @_set null
+                throw (@_set null)
+
         login: (username, password) ->
             request '/api/login',
@@ -27,18 +30,20 @@
                 @_set res
             , () =>
-                @_set null
+                throw (@_set null)
+
         logout: () ->
             request '/api/logout',
                 method: "POST"
                 handleAs: "json"
-            .then ((res) =>
-                console.log "Logged out"
-                @_set null), () =>
-                console.log "Error logging out"
+            .then (res) =>
+                @_set null
+            , () =>
+                @_set null
         
         _set: (newInfo) ->
-            @info = newInfo
-            @emit 'change', @info
-            @info
+            if (newInfo isnt @info)
+                @info = newInfo
+                @emit 'change', @info
+                @info
 
     new Session()
Index: Dev/trunk/src/client/qed-client/stddeps.js
===================================================================
--- Dev/trunk/src/client/qed-client/stddeps.js	(revision 467)
+++ Dev/trunk/src/client/qed-client/stddeps.js	(revision 468)
@@ -33,5 +33,5 @@
     './model/widgets/SurveyWidget',
 
-    './ui/LoginDialog',
+    './ui/LoginDialogWrapper',
     './ui/MainMenu',
     './ui/MenuBarLink',
Index: Dev/trunk/src/client/qed-client/store.js
===================================================================
--- Dev/trunk/src/client/qed-client/store.js	(revision 467)
+++ Dev/trunk/src/client/qed-client/store.js	(revision 468)
@@ -5,9 +5,11 @@
     'dojox/json/schema',
     './model/schema',
+    './request',
     './store/CouchStore'
-],function(stamp,Cache,Memory,jsonSchema,schema,CouchStore){
+],function(stamp,Cache,Memory,jsonSchema,schema,request,CouchStore){
     
     var couchStore = new CouchStore({
-        target: 'api/data/' /*,
+        target: 'api/data/',
+        request: request /*,
         validate: function(object) {
             var result = jsonSchema.validate(object,schema);
Index: Dev/trunk/src/client/qed-client/store/CouchStore.js
===================================================================
--- Dev/trunk/src/client/qed-client/store/CouchStore.js	(revision 467)
+++ Dev/trunk/src/client/qed-client/store/CouchStore.js	(revision 468)
@@ -28,4 +28,5 @@
         _responseIdProperty: "id",
         _responseRevProperty: "rev",
+        request: request,
         constructor: function(options){
             declare.safeMixin(this, options);
@@ -39,5 +40,5 @@
         info: function(){
             var dfd = new Deferred();
-            request(this.target, {
+            this.request(this.target, {
                 method: "GET",
                 handleAs: "json",
@@ -58,5 +59,5 @@
         get: function(id){
             var dfd = new Deferred();
-            request(this.target + encodeURIComponent(id), {
+            this.request(this.target + encodeURIComponent(id), {
                 method: "GET",
                 handleAs: "json",
@@ -103,5 +104,5 @@
             var id = options.id ? options.id : this.getIdentity(object);
             var hasId = typeof id !== "undefined";
-            request(hasId ? this.target + encodeURIComponent(id) : this.target, {
+            this.request(hasId ? this.target + encodeURIComponent(id) : this.target, {
                 method: hasId ? "PUT" : "POST",
                 data: json.toJson(object),
@@ -129,5 +130,5 @@
         remove: function(id,rev){
             var dfd = new Deferred();
-            request(this.target + encodeURIComponent(id), {
+            this.request(this.target + encodeURIComponent(id), {
                 method: "DELETE",
                 headers: {
@@ -243,5 +244,5 @@
             }
             
-            request(this.target + query, {
+            this.request(this.target + query, {
                 method: "GET", 
                 handleAs: "json",
Index: Dev/trunk/src/client/qed-client/store/request.js
===================================================================
--- Dev/trunk/src/client/qed-client/store/request.js	(revision 467)
+++ 	(revision )
@@ -1,43 +1,0 @@
-define([
-    "dojo/Deferred",
-    "dojo/request"
-], function(Deferred, request) {
-
-    var authenticated = true;
-    var queue = [];
-    
-    var _request = function(url, options) {
-        var dfd = new Deferred();
-        if ( authenticated ) {
-            var req = request(url,options);
-
-            // forward successfull response
-            req.then(function(data){
-                dfd.resolve(data);
-            });
-            
-            req.response.then(function(response){
-                // if we are recovering, launch other requests
-            }, function(error) {
-                if ( error.response.status === 401 ) {
-                    queue.push({
-                        url: url,
-                        options: options,
-                        dfd: dfd
-                    });
-                    console.log("Retry goes here :)");
-                } else {
-                    dfd.reject(error);
-                }
-            });
-        } else {
-            queue.push({
-                url: url,
-                options: options,
-                dfd: dfd
-            });
-        }
-        return dfd.promise;
-    };
-    return _request;
-});
Index: Dev/trunk/src/client/qed-client/ui/LoginDialog.coffee
===================================================================
--- Dev/trunk/src/client/qed-client/ui/LoginDialog.coffee	(revision 467)
+++ 	(revision )
@@ -1,32 +1,0 @@
-define [
-    "../session",
-    "dijit/_WidgetsInTemplateMixin",
-    "dijit/Dialog",
-    "dijit/form/ValidationTextBox",
-    "dojo/_base/declare",
-    "dojo/_base/event",
-    "dojo/dom-construct",
-    "dojo/text!./templates/LoginDialog.html"
-], (session, _WidgetsInTemplateMixin, Dialog,
-    ValidationTextBox, declare, event,
-    domConstruct, template) ->
-    declare [Dialog,_WidgetsInTemplateMixin],
-        templateString: template
-        title: "Login to QED"
-        show: () ->
-            @loginForm.reset()
-            @inherited arguments
-        onLogin: (evt) ->
-            if @loginForm.validate
-                value = @loginForm.get 'value'
-                session.login value.username,
-                              value.password
-                .then (() => @hide()),
-                      () => alert "Login failed!"
-            @hide()
-            event.stop evt if evt
-            false
-        onCancel: (evt) ->
-            @hide()
-            event.stop evt if evt
-            false
Index: Dev/trunk/src/client/qed-client/ui/LoginDialogWrapper.coffee
===================================================================
--- Dev/trunk/src/client/qed-client/ui/LoginDialogWrapper.coffee	(revision 468)
+++ Dev/trunk/src/client/qed-client/ui/LoginDialogWrapper.coffee	(revision 468)
@@ -0,0 +1,36 @@
+define [
+    "../session",
+    "dijit/_WidgetBase",
+    "dijit/_TemplatedMixin",
+    "dijit/_WidgetsInTemplateMixin",
+    "dijit/registry",
+    "dojo/_base/declare",
+    "dojo/_base/event",
+    "dojo/_base/lang",
+    "dojo/on",
+    "dojo/text!./templates/LoginDialogWrapper.html"
+], (session, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin,
+    registry, declare, event, lang, _on, template) ->
+    declare [_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin],
+        templateString: template
+        startup: () ->
+            if @_started then return
+            @inherited arguments
+            _on session, 'change', (lang.hitch @, @onUserChange)
+            @onUserChange session.get()
+        onLogin: (evt) ->
+            if @loginForm.validate()
+                value = @loginForm.get 'value'
+                session.login value.username,
+                              value.password
+                .then () =>
+                    @loginDialog.hide()
+                , () =>
+                    alert "Login failed!"
+            event.stop evt if evt
+            false
+        onUserChange: (user) ->
+            if user
+                @loginDialog.hide()
+            else
+                @loginDialog.show()
Index: Dev/trunk/src/client/qed-client/ui/SessionMenu.coffee
===================================================================
--- Dev/trunk/src/client/qed-client/ui/SessionMenu.coffee	(revision 467)
+++ Dev/trunk/src/client/qed-client/ui/SessionMenu.coffee	(revision 468)
@@ -1,16 +1,13 @@
 define [
     "../session",
-    "dijit/DropDownMenu",
-    "dijit/registry",
+    "./LoginDialogWrapper",
     "dijit/MenuBarItem",
     "dojo/_base/declare",
     "dojo/_base/event",
     "dojo/on"
-], (session, DropDownMenu, registry, MenuBarItem, declare, event, _on) ->
+], (session, LoginDialogWrapper, MenuBarItem, declare, event, _on) ->
     declare [MenuBarItem],
         info: null
         postCreate: () ->
-            @loginDialog = registry.byId 'loginDialog'
-            console.error "Cannot find loginDialog" if not @loginDialog
             @set 'label', "Login"
         startup: () ->
@@ -29,5 +26,5 @@
                 session.logout()
             else
-                @loginDialog.show()
+                LoginDialogWrapper.show()
             event.stop evt if evt
             false
Index: Dev/trunk/src/client/qed-client/ui/templates/LoginDialog.html
===================================================================
--- Dev/trunk/src/client/qed-client/ui/templates/LoginDialog.html	(revision 467)
+++ 	(revision )
@@ -1,51 +1,0 @@
-<div class="dijitDialog"
-     role="dialog"
-     aria-labelledby="${id}_title">
-    <div data-dojo-attach-point="titleBar"
-         class="dijitDialogTitleBar">
-        <span data-dojo-attach-point="titleNode"
-              class="dijitDialogTitle"
-              id="${id}_title" role="heading" level="1"></span>
-        <span data-dojo-attach-point="closeButtonNode"
-              class="dijitDialogCloseIcon"
-              data-dojo-attach-event="ondijitclick: onCancel"
-              title="${buttonCancel}" role="button" tabindex="0">
-            <span data-dojo-attach-point="closeText"
-                  class="closeText" title="${buttonCancel}">x</span>
-        </span>
-    </div>
-    <div class="dijitDialogPaneContent">
-        <form data-dojo-type="dijit/form/Form"
-              data-dojo-attach-point="loginForm"
-              data-dojo-attach-event="onSubmit:onLogin">
-            <div>
-              <label for="username"
-                     class="qedLabel">Username</label>
-              <div name="username"
-                   class="qedField"
-                   data-dojo-type="dijit/form/ValidationTextBox"
-                   data-dojo-props="required: true"></div>
-            </div>
-            <div>
-              <label for="password" class="qedLabel">Password</label>
-              <div name="password"
-                   class="qedField"
-                   data-dojo-type="dijit/form/ValidationTextBox"
-                   data-dojo-props="required: true"></div>
-            </div>
-            <div data-dojo-type="dijit/form/Button"
-                 type="submit"
-                 data-dojo-attach-event="onLogin">Login</div>
-            <div data-dojo-type="dijit/form/Button"
-                 data-dojo-attach-event="onCancel">Cancel</div>
-        </form>
-        <!-- Container node is required, otherwise
-             root is taken and emptied
-         -->
-        <div data-dojo-attach-point="containerNode"></div>
-    </div>
-</div>
-
-
-
-
Index: Dev/trunk/src/client/qed-client/ui/templates/LoginDialogWrapper.html
===================================================================
--- Dev/trunk/src/client/qed-client/ui/templates/LoginDialogWrapper.html	(revision 468)
+++ Dev/trunk/src/client/qed-client/ui/templates/LoginDialogWrapper.html	(revision 468)
@@ -0,0 +1,30 @@
+<div>
+    <div id="loginDialog"
+         data-dojo-type="dijit/Dialog"
+         data-dojo-props="title:'Login to QED', closable:false"
+         data-dojo-attach-point="loginDialog">
+        <form data-dojo-type="dijit/form/Form"
+              data-dojo-attach-point="loginForm"
+              data-dojo-attach-event="onSubmit:onLogin">
+            <div>
+              <label for="username"
+                     class="qedLabel">Username</label>
+              <div name="username"
+                   class="qedField"
+                   data-dojo-type="dijit/form/ValidationTextBox"
+                   data-dojo-props="required: true"></div>
+            </div>
+            <div>
+              <label for="password" class="qedLabel">Password</label>
+              <div name="password"
+                   type="password"
+                   class="qedField"
+                   data-dojo-type="dijit/form/ValidationTextBox"
+                   data-dojo-props="required: true"></div>
+            </div>
+            <div data-dojo-type="dijit/form/Button"
+                 type="submit"
+                 data-dojo-attach-event="onLogin">Login</div>
+        </form>
+    </div>
+</div>
