瀏覽代碼

Propogate login failures to the user, so the can tell it failed

Zachary Kotlarek 12 年之前
父節點
當前提交
5c9e95256f
共有 2 個文件被更改,包括 39 次插入 和 6 次删除
  1. 18 6
      web/includes/auth.php
  2. 21 0
      web/login.php

+ 18 - 6
web/includes/auth.php

@@ -1,11 +1,14 @@
 <?
 
+$AUTH_ERR = '';
+
 # Always start a session, unless we're on the CLI
 if (php_sapi_name() != 'cli') {
 	session_start();
 }
 
 function login($username, $password) {
+	global $AUTH_ERR;
 	$authenticated = false;
 	$authorized    = false;
 
@@ -13,11 +16,16 @@ function login($username, $password) {
 	$token = myplexToken($username, $password);
 	if ($token !== false) {
 		$authenticated = true;
+	} else {
+		$AUTH_ERR = 'Invalid Credentials';
 	}
 
 	# Authorize against MyPlex
 	if ($authenticated) {
 		$authorized = myplexAuthorize($token);
+		if (!$authorized) {
+			$AUTH_ERR = 'Not Authorized';
+		}
 	}
 
 	# Login on dual success
@@ -25,11 +33,13 @@ function login($username, $password) {
 		$_SESSION['USER'] = $username;
 		session_regenerate_id(true);
 	} else {
-		logout();
+		logout(false);
 	}
 
 	# Redirect, using the provided target if available
-	login_redirect();
+	if (authenticated()) {
+		login_redirect();
+	}
 }
 
 function login_redirect() {
@@ -41,12 +51,14 @@ function login_redirect() {
 	header('Location: ' . $url);
 }
 
-function logout() {
-	global $MAIN_PAGE;
+function logout($redirect = true) {
 	unset($_SESSION['USER']);
 	session_regenerate_id(true);
-	header('Location: ' . $MAIN_PAGE);
-	exit();
+	if ($redirect) {
+		global $MAIN_PAGE;
+		header('Location: ' . $MAIN_PAGE);
+		exit();
+	}
 }
 
 function username() {

+ 21 - 0
web/login.php

@@ -25,14 +25,35 @@ if (isset($_POST['username']) && isset($_POST['password'])) {
 
 printHeader('UberZach TV - Login');
 
+# Preserve the redirect
 $url = $_SERVER['PHP_SELF'];
 if (isset($_GET['dest'])) {
 	$url .= '?dest=' . urlencode($_GET['dest']);
 }
 
+# Save our AUTH_ERR as a user notice, if it exists
+$notice = '';
+if ($AUTH_ERR) {
+	$notice = $AUTH_ERR;
+}
+
+# Header
 print <<<ENDOLA
 <div style="width: 50%; margin-left: auto; margin-right: auto;">
 <form action="${url}" method="post" data-ajax="false">
+ENDOLA;
+
+# Print the user notice, if any, above the login fields
+if ($notice) {
+	print <<<ENDOLA
+<p style="color: red;">
+$notice
+</p>
+ENDOLA;
+}
+
+# Login fields
+print <<<ENDOLA
 <p>
 <label>Username: <input type="text" name="username" /></label><br/>
 <label>Password: <input type="password" name="password" /></label><br/>