Last active
May 15, 2020 07:05
-
-
Save harryWonder/497da117da502790e25264947fcc836e to your computer and use it in GitHub Desktop.
This class extends the base controller and loads in the RegisterModel as a dependency
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
<?php | |
require_once(__dir__ . '/Controller.php'); | |
require_once('./Model/RegisterModel.php'); | |
class Register extends Controller { | |
public $active = 'Register'; //for highlighting the active link... | |
private $registerModel; | |
/** | |
* @param null|void | |
* @return null|void | |
* @desc Checks if the user session is set and creates a new instance of the RegisterModel... | |
**/ | |
public function __construct() | |
{ | |
if (isset($_SESSION['auth_status'])) header("Location: dashboard.php"); | |
$this->registerModel = new RegisterModel(); | |
} | |
/** | |
* @param array | |
* @return array|boolean | |
* @desc Verifies, Creates, and returns a user by calling the register method on the RegisterModel... | |
**/ | |
public function register(array $data) | |
{ | |
$name = stripcslashes(strip_tags($data['name'])); | |
$email = stripcslashes(strip_tags($data['email'])); | |
$phone = stripcslashes(strip_tags($data['phone'])); | |
$password = stripcslashes(strip_tags($data['password'])); | |
$EmailStatus = $this->registerModel->fetchUser($email)['status']; | |
$Error = array( | |
'name' => '', | |
'email' => '', | |
'phone' => '', | |
'password' => '', | |
'status' => false | |
); | |
if (preg_match('/[^A-Za-z\s]/', $name)) { | |
$Error['name'] = 'Only Alphabets are allowed.'; | |
return $Error; | |
} | |
if (!empty($EmailStatus)) { | |
$Error['email'] = 'Sorry. This Email Address has been taken.'; | |
return $Error; | |
} | |
if (preg_match('/[^0-9_]/', $phone)) { | |
$Error['phone'] = 'Please, use a valid phone number.'; | |
return $Error; | |
} | |
if (strlen($password) < 7) { | |
$Error['password'] = 'Please, use a stronger password.'; | |
return $Error; | |
} | |
$Payload = array( | |
'name' => $name, | |
'email' => $email, | |
'phone' => $phone, | |
'password' => password_hash($password, PASSWORD_BCRYPT) | |
); | |
$Response = $this->registerModel->createUser($Payload); | |
$Data = $this->registerModel->fetchUser($email)['data']; | |
unset($Data['password']); //Makes a whole lot of sense to get rid of any critical information... | |
if (!$Response['status']) { | |
$Response['status'] = 'Sorry, An unexpected error occurred and your request could not be completed.'; | |
return $Response; | |
} | |
$_SESSION['data'] = $Data; | |
$_SESSION['auth_status'] = true; | |
header("Location: dashboard.php"); | |
return true; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment