Index: deluge/ui/web/js/Deluge.Login.js
===================================================================
--- deluge/ui/web/js/Deluge.Login.js	(revision c4cdd70a4e0571633b05fa5cf1cecf46d714cca6)
+++ deluge/ui/web/js/Deluge.Login.js	(revision e8374937571b5bd59746d1be8f3cb4053e8f6026)
@@ -24,4 +24,7 @@
 (function(){
 	Ext.deluge.LoginWindow = Ext.extend(Ext.Window, {
+		
+		firstShow: true,
+		
 		constructor: function(config) {
 			config = Ext.apply({
@@ -46,4 +49,5 @@
 			Deluge.Events.on('logout', this.onLogout, this);
 			this.on('show', this.onShow, this);
+			this.on('beforeshow', this.onBeforeShow, this);
 			
 			this.addButton({
@@ -83,8 +87,9 @@
 			Deluge.Client.web.login(passwordField.getValue(), {
 				success: function(result) {
-					if (result == true) {
+					if (result) {
 						Deluge.Events.fire('login');
 						this.hide();
 						passwordField.setRawValue('');
+						Deluge.UI.cookies.set("session", result);
 					} else {
 						Ext.MessageBox.show({
@@ -106,5 +111,38 @@
 		
 		onLogout: function() {
-			this.show();
+			var session = Deluge.UI.cookies.get("session", false);
+			if (session) {
+				Deluge.Client.web.delete_session(session, {
+					success: function(result) {
+						Deluge.UI.cookies.set("session", false);
+						this.show();
+					},
+					scope: this
+				});
+			}
+		},
+		
+		onBeforeShow: function() {
+			var session = Deluge.UI.cookies.get("session", false);
+			if (session) {
+				Deluge.Client.web.check_session(session, {
+					success: function(result) {
+						if (result) {
+							Deluge.Events.fire('login');
+							this.loginForm.items.get('password').setRawValue('');
+							this.hide();
+						} else {
+							Deluge.UI.cookies.set("session", false);
+							this.show();
+						}
+					},
+					failure: function(result) {
+						Deluge.UI.cookies.set("session", false);
+						this.show();
+					},
+					scope: this
+				});
+				return false;
+			}
 		},
 		
Index: deluge/ui/web/js/Deluge.UI.js
===================================================================
--- deluge/ui/web/js/Deluge.UI.js	(revision c4cdd70a4e0571633b05fa5cf1cecf46d714cca6)
+++ deluge/ui/web/js/Deluge.UI.js	(revision e8374937571b5bd59746d1be8f3cb4053e8f6026)
@@ -50,9 +50,12 @@
 		});
 		
-		Deluge.Login.show();
-		
 		Deluge.Events.on("connect", this.onConnect, this);
 		Deluge.Events.on("disconnect", this.onDisconnect, this);
-		Deluge.Client = new Ext.ux.util.RpcClient({url: '/json'});
+		Deluge.Client = new Ext.ux.util.RpcClient({
+			url: '/json'
+		});
+		Deluge.Client.on('connected', function(e) {
+			Deluge.Login.show();
+		});
 		this.update = this.update.bind(this);
 	},
Index: deluge/ui/web/json_api.py
===================================================================
--- deluge/ui/web/json_api.py	(revision c4cdd70a4e0571633b05fa5cf1cecf46d714cca6)
+++ deluge/ui/web/json_api.py	(revision e8374937571b5bd59746d1be8f3cb4053e8f6026)
@@ -26,4 +26,5 @@
 import time
 import base64
+import random
 import urllib
 import hashlib
@@ -428,4 +429,33 @@
         return d
     
+    def _create_session(self, login='admin'):
+        m = hashlib.md5()
+        m.update(login)
+        m.update(str(time.time()))
+        m.update(str(random.getrandbits(999)))
+        m.update(m.hexdigest())
+        session_id = m.hexdigest()
+        
+        config = component.get("DelugeWeb").config
+        config["sessions"][session_id] = {
+            "login": login
+        }
+        return session_id
+    
+    @export
+    def check_session(self, session_id):
+        d = Deferred()
+        config = component.get("DelugeWeb").config
+        d.callback(session_id in config["sessions"])
+        return d
+    
+    @export
+    def delete_session(self, session_id):
+        d = Deferred()
+        config = component.get("DelugeWeb").config
+        del config["sessions"][session_id]
+        d.callback(True)
+        return d
+    
     @export
     def login(self, password):
@@ -437,5 +467,9 @@
         m.update(password)
         d = Deferred()
-        d.callback(m.hexdigest() == config['pwd_md5'])
+        if m.hexdigest() == config['pwd_md5']:
+            # Change this to return a session id
+            d.callback(self._create_session())
+        else:
+            d.callback(False)
         return d
     
Index: deluge/ui/web/server.py
===================================================================
--- deluge/ui/web/server.py	(revision c4cdd70a4e0571633b05fa5cf1cecf46d714cca6)
+++ deluge/ui/web/server.py	(revision e8374937571b5bd59746d1be8f3cb4053e8f6026)
@@ -27,4 +27,5 @@
 import locale
 import shutil
+import signal
 import urllib
 import gettext
@@ -72,5 +73,5 @@
     "pwd_md5": "2c9baa929ca38fb5c9eb5b054474d1ce",
     "base": "",
-    "sessions": [],
+    "sessions": {},
     "sidebar_show_zero": False,
     "sidebar_show_trackers": False,
