Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ŠIS IR CONTROLLERIS
- public function login()
- {
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
- $data = [
- 'email' => trim($_POST['email']),
- 'password' => trim($_POST['password']),
- 'email_err' => '',
- 'password_err' => '',
- ];
- // Validate data
- if (empty($data['email'])) {
- $data['email_err'] = 'Please enter your email';
- }
- if (empty($data['password'])) {
- $data['password_err'] = 'Please enter your you password';
- }
- // Find user
- if ($this->userModel->findUserByEmail($data['email'])) {
- } else {
- $data['email_err'] = 'Sorry, we could not find this user';
- }
- if (empty($data['email_err']) && empty($data['password_err'])) {
- // Validated, now check and set logged in user
- $loggedInUser = $this->userModel->login($data['email'], $data['password']);
- if ($loggedInUser) {
- // Create session
- $this->createUserSession($loggedInUser);
- } else {
- $data['password_err'] = 'Password incorrect';
- $this->view('users/login', $data);
- }
- } else {
- $this->view('users/login', $data);
- }
- } else {
- $data = [
- 'email' => '',
- 'password' => '',
- 'email_err' => '',
- 'password_err' => '',
- ];
- $this->view('users/login', $data);
- }
- }
- // ŠIS IR MODELIS
- // Login User
- public function login($email, $password)
- {
- $this->db->query('SELECT * FROM users WHERE email = :email');
- $this->db->bind(':email', $email);
- $row = $this->db->single();
- $hashed_password = $row->password;
- if (password_verify($password, $hashed_password)) {
- return $row;
- } else {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement