I want everything to be well and secure with my authentication code. I've done many signup and login systems before but just for testing purposes, and didn't take into consideration all the security measures like SQL injection, password hashing etc... This is the code. If you can, please point out any function that could be enhanced.
Signup:
public function setUser(){
$fullName = htmlspecialchars($_POST['fullName']);
$email = htmlspecialchars($_POST['email']);
$password = htmlspecialchars($_POST['password']);
$DOB = htmlspecialchars($_POST['DOB']);
$nationality = htmlspecialchars($_POST['nationality']);
$workTitle = htmlspecialchars($_POST['workTitle']);
$workPlace = htmlspecialchars($_POST['workPlace']);
$phoneNumber = htmlspecialchars($_POST['phoneNumber']);
$userType = htmlspecialchars($_POST['userType']);
//PASSWORD HASHING
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
if($userType == "Investor"){
$accNumber = "A" . sprintf("%06d", mt_rand(1, 999999));
}else{
$accNumber = "B" . sprintf("%06d", mt_rand(1, 999999));
}
try {
$sql = "INSERT INTO user (fullName, email, password, DOB, nationality, workTitle, workPlace, phoneNumber, userType, accNumber) VALUES (?,?,?,?,?,?,?,?,?,?)";
$stmt = $this->connect()->prepare($sql);
$stmt->execute([$fullName, $email, $hashedPassword, $DOB, $nationality, $workTitle, $workPlace, $phoneNumber, $userType, $accNumber]);
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
header('location:login.php');
} //setUser()
Login:
//RETRIEVE HASHED PASSWORD FROM DB
public function getPassword($email){
$sql = "SELECT password FROM user WHERE email = ?";
$stmt = $this->connect()->prepare($sql);
$stmt->execute([$email]);
if($stmt->rowCount()){
while($row = $stmt->fetch()){
return $row['password'];
}
}
} //getPassword()
//LOGIN THE USER
public function loginUser(){
$email = htmlspecialchars($_POST['email']);
$password = htmlspecialchars($_POST['password']);
$hashedPassword = $this->getPassword($email);
//Check if hashed password is equal to password
if(password_verify($password,$hashedPassword)){
try {
$sql = "SELECT * FROM user WHERE email = ?";
$stmt = $this->connect()->prepare($sql);
$stmt->execute([$email]);
if($stmt->rowCount()){
while($row = $stmt->fetch()){
$_SESSION['userID'] = $row['userID'];
$_SESSION['fullName'] = $row['fullName'];
$_SESSION['email'] = $row['email'];
$_SESSION['DOB'] = $row['DOB'];
$_SESSION['nationality'] = $row['nationality'];
$_SESSION['workTitle'] = $row['workTitle'];
$_SESSION['workPlace'] = $row['workPlace'];
$_SESSION['phoneNumber'] = $row['phoneNumber'];
$_SESSION['userType'] = $row['userType'];
$_SESSION['accNumber'] = $row['accNumber'];
$_SESSION['status'] = $row['status'];
$_SESSION['profilePic'] = $row['profilePic'];
header('location:dashboard.php');
}
}
} catch (\Exception $e) {
throw new Exception($e->getMessage);
}
}else{
echo"Invalid email or password";
}
} //loginUser()
I haven't worked on validation yet, but I will. I just need to make sure that everything else is good to go.