Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.21 KB | None | 0 0
  1. // ŠIS IR CONTROLLERIS
  2.  
  3. public function login()
  4.     {
  5.         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  6.  
  7.             $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
  8.  
  9.             $data = [
  10.                 'email' => trim($_POST['email']),
  11.                 'password' => trim($_POST['password']),
  12.                 'email_err' => '',
  13.                 'password_err' => '',
  14.             ];
  15.  
  16.             // Validate data
  17.             if (empty($data['email'])) {
  18.                 $data['email_err'] = 'Please enter your email';
  19.             }
  20.  
  21.             if (empty($data['password'])) {
  22.                 $data['password_err'] = 'Please enter your you password';
  23.             }
  24.  
  25.             // Find user
  26.             if ($this->userModel->findUserByEmail($data['email'])) {
  27.             } else {
  28.                 $data['email_err'] = 'Sorry, we could not find this user';
  29.             }
  30.  
  31.             if (empty($data['email_err']) && empty($data['password_err'])) {
  32.                 // Validated, now check and set logged in user
  33.                 $loggedInUser = $this->userModel->login($data['email'], $data['password']);
  34.  
  35.                 if ($loggedInUser) {
  36.                     // Create session
  37.                     $this->createUserSession($loggedInUser);
  38.                 } else {
  39.                     $data['password_err'] = 'Password incorrect';
  40.                     $this->view('users/login', $data);
  41.                 }
  42.             } else {
  43.                 $this->view('users/login', $data);
  44.             }
  45.  
  46.         } else {
  47.             $data = [
  48.                 'email' => '',
  49.                 'password' => '',
  50.                 'email_err' => '',
  51.                 'password_err' => '',
  52.             ];
  53.  
  54.             $this->view('users/login', $data);
  55.         }
  56.     }
  57.  
  58. // ŠIS IR MODELIS
  59.  
  60. // Login User
  61.     public function login($email, $password)
  62.     {
  63.         $this->db->query('SELECT * FROM users WHERE email = :email');
  64.         $this->db->bind(':email', $email);
  65.  
  66.         $row = $this->db->single();
  67.  
  68.         $hashed_password = $row->password;
  69.         if (password_verify($password, $hashed_password)) {
  70.             return $row;
  71.         } else {
  72.             return false;
  73.         }
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement