Browse Source

context fixes

Nathaniel van Diepen 10 years ago
parent
commit
03a330310a
4 changed files with 20 additions and 9 deletions
  1. 4 0
      api.php
  2. 2 0
      data/pages/register.template
  3. 4 3
      js/index.js
  4. 10 6
      php/security.php

+ 4 - 0
api.php

@@ -29,6 +29,9 @@
 					}else{
 						$context = Array();
 					}
+					if(loggedIn()){
+						$context['key'] = $SESSION['key'];
+					};
 					$ret['context'] = $context;
 					retj($ret,$id);
 				break;
@@ -59,6 +62,7 @@
 								}else{
 									$ret['error'] = "That username already exists!";
 								}
+								$ret['state'] = Array('data'=>Array('type'=>'template','id'=>'register'));
 								retj($ret,$id);
 							break;
 							default:

+ 2 - 0
data/pages/register.template

@@ -25,8 +25,10 @@
 		data.id = 'register';
 		apiCall(data,function(d){
 			if(!d.error){
+				setKey(d.key);
 				loadState('page-index');
 			}else{
+				setKey(null);
 				loadState('page-register');
 			}
 		});

+ 4 - 3
js/index.js

@@ -32,7 +32,7 @@
 		getKey = window.getKey = function(){
 			return Key;
 		},
-		api = window.apiCall = function(data,callback){
+		apiCall = window.apiCall = function(data,callback){
 			data.get = 'api';
 			data.timestamp = +new Date;
 			if(exists(State.data.key)){
@@ -107,8 +107,8 @@
 				switch(State.data.type){
 					case 'template':
 						api(State.data,function(d){
-							if(Key !== null){
-								d.context.key = Key;
+							if(!exists(d.context.key)){
+								setKey(null);
 							}
 							$('#content').html(Handlebars.compile(d.template)(d.context)).mCustomScrollbar('destroy');
 							$('#content,.scroll').mCustomScrollbar({
@@ -128,6 +128,7 @@
 							});
 						});
 					break;
+					case 'action':break;
 					default:
 						alert("Something went wrong!\nYour current state:\n"+JSON.stringify(State));
 				}

+ 10 - 6
php/security.php

@@ -16,16 +16,20 @@
 		return saltedHash($username,$salt);
 	}
 	function authenticate(){
+		global $SESSION;
+		if(loggedIn()){
+			setKey($SESSION['key']);
+		}
+	}
+	function loggedIn(){
 		global $SESSION;
 		if(isset($_GET['key'])&&isset($SESSION['key'])&&isset($SESSION['username'])&&isUser($SESSION['username'])){
-			if($_GET['key'] != $SESSION['key']){
-				setKey(null);
-				retj(Array('error'=>'Invalid key, you were logged out.'));
+			if($_GET['key'] == $SESSION['key']){
+				return true;
 			}
-			setKey($SESSION['key']);
-		}else{
-			setKey(null);
 		}
+		setKey(null);
+		return false;
 	}
 	function setKey($key){
 		global $SESSION;