waktu tersisa untuk klaim DISKON 40% dalam program Full Stack Membership. Klaim Promo!

Selalu terdetect Salah password/email saat login

Ditanyakan pada tanggal 1 June 2020 oleh Rifki Aprilian Danoe Edit  Hapus  Buka Kembali  Tandai 

Hallo, Saya mengalami beberapa kendala saat login yang selalu terdetect salah email/passwordnya

Login_model.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Login_model extends MY_Model {

    protected $table = 'user';

    public function getDefaultValues(){
        return [
            'email'     => '',
            'password'  => '',
        ];
    }

    public function getValidationRules(){
        $validationRules = [
            [
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'trim|required|valid_email'
            ],
            [
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required'
            ],
        ];

        return $validationRules;
    }

    public function run($input){  
        $query = $this->where('email', strtolower($input->email))
                ->where('is_active', 1)
                ->first();

        if(!empty($query) && hashEncryptVerify($input->password, $query->password)){
            $sess_data = [
                'id'    => $query->id,
                'name'  => $query->name,
                'email' => $query->email,
                'role'  => $query->role,
                'is_login' => true
            ];
            $this->session->set_userdata($sess_data);
            return true;
        }
        return false;
    }

}

/* End of file Login_model.php */

Login.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $is_login = $this->session->userdata('is_login');

        if($is_login){
            redirect(base_url());
            return;
        }
    }

    public function index(){
        if (!$_POST) {
            $input = (object) $this->login->getDefaultValues();
        }else{
            $input = (object) $this->input->post(null, true);
        }

        if(!$this->login->validate()){
            $data['title'] = 'Login';
            $data['input'] = $input;
            $data['page'] = 'pages/auth/login';

            $this->view($data);
            return;
        }

        if($this->login->run($input)){
            $this->session->set_flashdata('success','selamat anda berhasil login');
            redirect(base_url());
        }else{
            $this->session->set_flashdata('error', 'E-Mail atau Password salah atau akun Anda sedang tidak aktif!');
            redirect(base_url('login'));
        }
    }

}

/* End of file Login.php */

dan method yg ada di helper sudah saya ubah juga menjadi verify

function hashEncryptVerify($input, $hash){
    if (password_verify($input, $hash)) {
        return true;
    }else{
                return false;
        }
}

Menariknya terdapat error di file Login_model.php dan di line saat query :

public function run($input){  
        $query = $this->where('email', strtolower($input->email))
                ->where('is_active', 1)
                ->first();

Jika dirubah dari $query = $this->where ke $query = $this->db->where linenya gak error lagi tapi saat di run terjadi error:

Message: Call to undefined method CI_DB_mysqli_driver::first()

Mohon pencerahannya, saya sudah sangat yakin email & passwordnya sama. Issue ini pernah disampaikan oleh Fachrel Marpaung sebelumnya.

Thanks min

1 JAWABAN / 2 KOMENTAR

User avatar
Ahmad Oriza menjawab pada tanggal 5 June 2020 Jadikan Jawaban Terbaik  Edit  Hapus  Ubah ke Komentar 

Sepertinya ada kode yang ketinggalan. Biasanya seperti ini. Kami coba lampirkan kode asli, coba di cek ya kak

Login_model.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Login_model extends MY_Model 
{

    protected $table = 'user';

    public function getDefaultValues()
    {
        return [
            'email'     => '',
            'password'  => '',
        ];
    }

    public function getValidationRules()
    {
        $validationRules = [
            [
                'field' => 'email',
                'label' => 'E-Mail',
                'rules' => 'trim|required|valid_email'
            ],
            [
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required'
            ]
        ];

        return $validationRules;
    }

    public function run($input)
    {
        $query  = $this->where('email', strtolower($input->email))
                    ->where('is_active', 1)
                    ->first();

        if (!empty($query)) {
            $sess_data = [
                'id'        => $query->id,
                'name'      => $query->name,
                'email'     => $query->email,
                'role'      => $query->role,
                'is_login'  => true,
            ];
            $this->session->set_userdata($sess_data);
            return true;
        }

        if (!empty($query) && hashEncryptVerify($input->password, $query->password)) {
            $sess_data = [
                'id'        => $query->id,
                'name'      => $query->name,
                'email'     => $query->email,
                'role'      => $query->role,
                'is_login'  => true,
            ];
            $this->session->set_userdata($sess_data);
            return true;
        }

        return false;
    }

}

/* End of file Login_model.php */

Login.php controller

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends MY_Controller 
{

    public function __construct()
    {
        parent::__construct();
        $is_login   = $this->session->userdata('is_login');

        if ($is_login) {
            redirect(base_url());
            return;
        }
    }

    public function index()
    {
        if (!$_POST) {
            $input  = (object) $this->login->getDefaultValues();
        } else {
            $input  = (object) $this->input->post(null, true);
        }

        if (!$this->login->validate()) {
            $data['title']  = 'Login';
            $data['input']  = $input;
            $data['page']   = 'pages/auth/login';

            $this->view($data);
            return;
        }

        if ($this->login->run($input)) {
            $this->session->set_flashdata('success', 'Berhasil melakukan login!');
            redirect(base_url());
        } else {
            $this->session->set_flashdata('error', 'E-Mail atau Password salah atau akun Anda sedang tidak aktif!');
            redirect(base_url('login'));
        }
    }

}

/* End of file Login.php */

Login view

<main role="main" class="container">
    <?php $this->load->view('layouts/_alert') ?>
    <div class="row">
        <div class="col-md-8 mx-auto">
            <div class="card">
                <div class="card-header">
                    Formulir Login
                </div>
                <div class="card-body">
                    <?= form_open('login', ['method' => 'POST']) ?>
                        <div class="form-group">
                            <label for="">E-Mail</label>
                            <?= form_input(['type' => 'email', 'name' => 'email', 'value' => $input->email, 'class' => 'form-control', 'placeholder' => 'Masukkan alamat email', 'required' => true]) ?>
                            <?= form_error('email') ?>
                        </div>
                        <div class="form-group">
                            <label for="">Password</label>
                            <?= form_password('password', '', ['class' => 'form-control', 'placeholder' => 'Masukkan password', 'required' => true]) ?>
                            <?= form_error('password') ?>
                        </div>
                        <button type="submit" class="btn btn-primary">Login</button>
                    <?= form_close() ?>
                </div>
            </div>
        </div>
    </div>
</main>
Comment ..

wkwkwk ternyata missnya bukan di model login atau controller login-nya ka, tapi saya lupa gak nge-return salah satu method yg ada di core modelnya. Terima kasih banyak ka Oriza, solved and i really appreciate your response

Dikomentari pada tanggal 5 June 2020 oleh Rifki Aprilian Danoe Edit  Remove 
Comment ..

Mantap

Dikomentari 1 bulan yang lalu oleh Ahmad Oriza Edit  Remove