-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b76ee69
commit f543676
Showing
8 changed files
with
276 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,52 @@ | ||
<?php | ||
require_once "../database/connect.php"; | ||
set_error_handler('HANDLE_EXCEPTIONS'); // for any unexpected error | ||
require_once "../database/connect.php"; | ||
|
||
/* ERROR MESSAGES AND CONSTANTS */ | ||
define('HOME_PAGE', '/index.html'); | ||
define('LOGIN_ERROR','Invalid username or password.'); | ||
|
||
try{ | ||
if($_SERVER["REQUEST_METHOD"] !== "POST") EXIT_WITH_JSON(BAD_RESPONSE, INVALID_METHOD, null, $conn); | ||
|
||
if($_SERVER["REQUEST_METHOD"] === "POST") | ||
{ | ||
$username = (isset($_POST['username'])) ? trim($_POST['username']) : null; | ||
$password = (isset($_POST['password'])) ? trim($_POST['password']) : null; | ||
|
||
// Checking Validity. Although it already checked by the javascript logic | ||
if(!isValid($username, $password)) { | ||
$err_msg = "Did you bypass the pattern rules of this login page?"; | ||
EXIT_WITH_JSON(500, $err_msg, null, $conn); | ||
} | ||
if(!isValid($username, $password)) EXIT_WITH_JSON(BAD_RESPONSE, VALIDATION_FAILURE, null, $conn); | ||
|
||
// Prepare a select statement | ||
$sql = "SELECT user_id, username, name, password FROM user_data WHERE username = ?"; | ||
|
||
if($stmt = $conn->prepare($sql)){ | ||
// Bind variables to the prepared statement as parameters | ||
$stmt->bind_param("s", $username); | ||
|
||
// Attempt to execute the prepared statement | ||
if($stmt->execute()){ | ||
// Store result | ||
$stmt->store_result(); | ||
|
||
// Check if username exists, if yes then verify password | ||
if($stmt->num_rows == 1){ | ||
// Bind result variables | ||
$stmt->bind_result($id, $username, $name, $hashed_password); | ||
|
||
if($stmt->fetch()){ | ||
if(password_verify($password, $hashed_password)){ | ||
session_start(); | ||
// Store data in session variables | ||
$_SESSION["user_id"] = $id; | ||
$_SESSION["username"] = $username; | ||
$_SESSION["name"] = $name; | ||
$_SESSION["isLoggedIn"] = true; | ||
|
||
EXIT_WITH_JSON(200, "", "/index.html", $conn, $stmt); | ||
} else { | ||
// Password is not valid, display a generic error message | ||
$login_err = "Invalid username or password."; | ||
EXIT_WITH_JSON(500, $login_err, null, $conn, $stmt); | ||
} | ||
} | ||
} else{ | ||
// Username doesn't exist, display a generic error message | ||
$login_err = "Invalid username or password."; | ||
EXIT_WITH_JSON(500, $login_err, null, $conn, $stmt); | ||
} | ||
} else{ | ||
$err_msg = "Oops! something went wrong. Please try again later.\n"; | ||
EXIT_WITH_JSON(500, $err_msg, null, $conn, $stmt); | ||
} | ||
} else { | ||
$err_msg = "Oops! something went wrong. Please try again later.\n"; | ||
EXIT_WITH_JSON(500, $err_msg, null, $conn); | ||
$sql = "SELECT * FROM user_data WHERE username = ?"; | ||
$stmt = $conn->prepare($sql); | ||
|
||
if(!$stmt) EXIT_WITH_JSON(BAD_RESPONSE, EXECUTION_FAILURE, null, $conn); | ||
|
||
// Bind variables to the prepared statement as parameters | ||
$stmt->bind_param("s", $username); | ||
|
||
// Attempt to execute the prepared statement | ||
if(!$stmt->execute()) EXIT_WITH_JSON(BAD_RESPONSE, EXECUTION_FAILURE, null, $conn, $stmt); | ||
|
||
$user = $stmt->get_result(); | ||
|
||
if($user->num_rows != 1) EXIT_WITH_JSON(BAD_RESPONSE, LOGIN_ERROR, null, $conn, $stmt); | ||
|
||
$user_data = $user->fetch_assoc(); | ||
|
||
if(!password_verify($password, $user_data['password'])) | ||
EXIT_WITH_JSON(BAD_RESPONSE, LOGIN_ERROR, null, $conn, $stmt); | ||
|
||
session_start(); | ||
|
||
foreach ($user_data as $key => $value) { | ||
if($key !== "password") $_SESSION[$key] = $value; | ||
} | ||
} else exit(500); | ||
|
||
$_SESSION["isLoggedIn"] = true; | ||
|
||
EXIT_WITH_JSON(GOOD_RESPONSE, null, HOME_PAGE, $conn, $stmt); | ||
|
||
} catch (Exception $error) { | ||
$err_msg = "An unexpected error has occurred.\n" | ||
. "Please disregard the following error and try again later:\n" | ||
. $error->getMessage(); | ||
EXIT_WITH_JSON(BAD_RESPONSE, $err_msg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,64 @@ | ||
<?php | ||
require_once "../database/connect.php"; | ||
set_error_handler('HANDLE_EXCEPTIONS'); // for any unexpected error | ||
/* ERROR MESSAGES */ | ||
define('USERNAME_EXIST','This username is already taken.'); | ||
|
||
/* GOOD RESPONSE MESSAGES AND CONSTANTS */ | ||
define('SIGNUP_SUCCESS', "Congratulations! Your signup was successful.\nThank you for joining us!"); | ||
define('LOGIN_PAGE','/pages/auth/login.html'); | ||
|
||
|
||
try { | ||
if($_SERVER["REQUEST_METHOD"] !== "POST") EXIT_WITH_JSON(BAD_RESPONSE, INVALID_METHOD, null, $conn); | ||
|
||
if($_SERVER["REQUEST_METHOD"] === "POST") | ||
{ | ||
$name = (isset($_POST['name'])) ? trim($_POST['name']) : null; | ||
$username = (isset($_POST['username'])) ? trim($_POST['username']) : null; | ||
$email = (isset($_POST['email'])) ? trim($_POST['email']) : null; | ||
$username = (isset($_POST['username'])) ? trim($_POST['username']) : null; | ||
$password = (isset($_POST['password'])) ? trim($_POST['password']) : null; | ||
|
||
// Checking Validity. Although it already checked by the javascript logic | ||
if(!isValid($username, $password, $name, $email)) { | ||
$err_msg = "Did you bypass the pattern rules of this signup page?"; | ||
EXIT_WITH_JSON(500, $err_msg, null, $conn); | ||
EXIT_WITH_JSON(BAD_RESPONSE, $err_msg, null, $conn); | ||
} | ||
|
||
// Prepare a select statement for username availablity check. | ||
$sql = "SELECT user_id FROM user_data WHERE username = ?"; | ||
|
||
// Preparing mysql connection and sql statement. | ||
if($stmt = $conn->prepare($sql)) { | ||
// Binding username parameter with sql statement. | ||
$stmt->bind_param("s", $username); | ||
|
||
// Trying to execute the statement | ||
if($stmt->execute()){ | ||
// Storing the result. | ||
$stmt->store_result(); | ||
|
||
// If username exist end the script. | ||
if($stmt->num_rows() === 1) { | ||
$err_msg = "This username is already taken."; | ||
EXIT_WITH_JSON(500, $err_msg, null, $conn, $stmt); | ||
} else { | ||
$stmt->close();// Close statement. | ||
} | ||
} else { | ||
$err_msg = "Oops! something went wrong. Please try again later."; | ||
EXIT_WITH_JSON(500, $err_msg, null, $conn, $stmt); | ||
} | ||
|
||
} else { | ||
$err_msg = "Oops! something went wrong. Please try again later."; | ||
EXIT_WITH_JSON(500, $err_msg, null, $conn); | ||
} | ||
|
||
$stmt = $conn->prepare($sql); | ||
|
||
if(!$stmt) EXIT_WITH_JSON(BAD_RESPONSE, EXECUTION_FAILURE, null, $conn); | ||
|
||
$stmt->bind_param("s", $username); | ||
|
||
if(!$stmt->execute()) EXIT_WITH_JSON(BAD_RESPONSE, EXECUTION_FAILURE, null, $conn, $stmt); | ||
|
||
$stmt->store_result(); | ||
|
||
if($stmt->num_rows() === 1) EXIT_WITH_JSON(500, USERNAME_EXIST, null, $conn, $stmt); | ||
else $stmt->close(); // closing current statement. | ||
|
||
// Prepare an insert statement. | ||
$sql = "INSERT INTO user_data ( | ||
username, | ||
name, | ||
password, | ||
email_id | ||
) VALUES (?,?,?,?)"; | ||
|
||
if($stmt = $conn->prepare($sql)){ | ||
// Bind variables to the prepared statement as parameter. | ||
$stmt->bind_param("ssss", $username, $name, $hash_password, $email); | ||
|
||
// Hashing password. | ||
$hash_password = password_hash($password, PASSWORD_BCRYPT); | ||
|
||
// Attempt to execute the prepared statement. | ||
if($stmt->execute()){ | ||
$msg = "Congratulations! Your signup was successful.\nThank you for joining us!"; | ||
$redirect_to = "/pages/auth/login.html"; | ||
EXIT_WITH_JSON(200, $msg, $redirect_to, $conn, $stmt); | ||
} else { | ||
$err_msg = "Oops! something went wrong. Please try again later."; | ||
EXIT_WITH_JSON(500, $err_msg, null, $conn, $stmt); | ||
} | ||
|
||
} else { | ||
$err_msg = "Oops! something went wrong. Please try again later."; | ||
EXIT_WITH_JSON(500, $err_msg, null, $conn); | ||
} | ||
} else exit(500); | ||
username, | ||
name, | ||
password, | ||
email_id | ||
) VALUES (?,?,?,?)"; | ||
// Preparing mysql connection and sql statement. | ||
$stmt = $conn->prepare($sql); | ||
|
||
if(!$stmt) EXIT_WITH_JSON(BAD_RESPONSE, EXECUTION_FAILURE, null, $conn); | ||
|
||
// Bind variables to the prepared statement as parameter. | ||
$stmt->bind_param("ssss", $username, $name, $hash_password, $email); | ||
|
||
// ENCRYPT PASSWORD | ||
$hash_password = password_hash($password, PASSWORD_BCRYPT); | ||
|
||
if(!$stmt->execute()) EXIT_WITH_JSON(BAD_RESPONSE, EXECUTION_FAILURE, null, $conn, $stmt); | ||
|
||
EXIT_WITH_JSON(GOOD_RESPONSE, SIGNUP_SUCCESS, LOGIN_PAGE, $conn, $stmt); | ||
|
||
} catch (Exception $error) { | ||
$message = "An unexpected error has occurred.\n" | ||
. "Please disregard the following error and try again later:\n" | ||
. $error->getMessage(); | ||
EXIT_WITH_JSON(BAD_RESPONSE, $message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.