codeigniter auth code example

Example 1: CODEIGNITER codeigniter 4 auth

<?php

namespace App\models;

use CodeIgniter\Model;

class Users_model extends Model {

    protected $table = 'users';
    protected $primaryKey = 'id';
    protected $allowedFields = ['first_name', 'last_name', 'email', 'password'];

}

Example 2: CODEIGNITER codeigniter 4 auth

<?php

namespace App\Controllers;

use App\models\Users_model;

class Signin extends BaseController {

    public function index() {
        return view('signin/index');
    }

    public function authenticate() {
        if ($this->exists($_POST['email'], $_POST['password']) != NULL) {
            $session = session();
            $session->set('email', $_POST['email']);
            return $this->response->redirect(site_url('signin/profile'));
        } else {
            $data['msg'] = 'wrong';
            return view('signin', $data);
        }
    }

    public function profile() {
        return view('signin/profile');
    }

    private function exists($email, $password) {
        $model = new Users_model();
        $account = $model->where('email', $email)->first();
        if ($account != NULL) {
            if (password_verify($password, $account['password'])) {
                return $account;
            }
        }
        return NULL;
    }

}

Tags:

Php Example