/** * @param $username string * @param $password string * @return mixed * * Returns User object if successful, otherwise null. */ public static function login($username, $password) { $user = User::findByUsername($username); if (!$user) { return null; } $passwordHash = $user->password; $passwordSalt = $user->salt; if (Auth::verifyPassword($password, $passwordHash, $passwordSalt)) { return $user; } return null; }
# code... } else { mysqli_close($conn); header('Content-type: application/json'); echo json_encode(array("status" => false, "msg" => "Incomplete Request")); die; } $oldPassword = $_POST['oldPassword']; $newPassword = $_POST['newPassword']; if (strlen($oldPassword) > 15 || strlen($oldPassword) < 4 || strlen($newPassword) > 15 || strlen($newPassword) < 4) { mysqli_close($conn); header('Content-type: application/json'); echo json_encode(array("status" => false, "msg" => "Password length should be between 4 and 15")); die; } $stat = Auth::verifyPassword($userID, $oldPassword, $conn); if (!$stat) { mysqli_close($conn); header('Content-type: application/json'); echo json_encode(array("status" => false, "msg" => "old Password did not match.")); die; } $status = Auth::changePassword($userID, $newPassword, $conn); mysqli_close($conn); header('Content-type: application/json'); if ($status) { echo json_encode(array("status" => true, "msg" => "Password changed successfully.")); die; } else { echo json_encode(array("status" => false, "msg" => "Internal Error, please try later.")); die;
/** * Authenticate & sign User in */ public function signIn() { if ($this->slim->request->isGet()) { $this->slim->render('signin.html.twig'); } elseif ($this->slim->request->isPost()) { $email = $_POST['email']; $password = $_POST['password']; $user = $this->getUserFinder()->findOneBy('email', $email); if (!empty($user)) { $auth = new Auth(); $passwordVerified = $auth->verifyPassword($password, $user->getPassword()); if ($passwordVerified === true) { $auth->signIn($user); $this->slim->flash('success', 'Signed In'); $this->slim->redirect('/categories'); } } $this->slim->flash('error', 'Incorrect email or password'); $this->slim->redirect('/signin'); } }