Log in with email or username Codeigniter

0

Good afternoon I need to login or with the email table or username I am not finding in which area I should add the fields

The Login.php driver

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

     class Login extends CI_Controller {

    var $data;

     function __construct()
    {
        parent::__construct();
        $this->data = new stdClass;
        $this->data->meta_title = SITE_NAME;
        $this->data->meta_description = '';
        $this->data->meta_robots = 'index, follow';

        $this->load->helper('form');
        $this->load->library('form_validation');

        if ($this->session->flashdata('alert_messages')) {
            $this->data->alert_messages = $this->session->flashdata('alert_messages');
            $this->data->alert_type = $this->session->flashdata('alert_type');
        }

    }

    public function index()
    {

        $this->data->meta_title = 'Iniciar sesión | '.SITE_NAME;

        $this->data->is_loggedin = $this->Login_model->is_loggedin();
        // Current user is loggedin?
        if ($this->data->is_loggedin == TRUE) {
            redirect('/backend/dashboard');
        }

        // form general attributes
        $this->data->form_attributes = array(
            'class' => 'login-form',
            'id' => 'login-form'
        );

        // inputs form
        $this->data->form_inputs = array(
            'email' => array(
                'id' => 'email',
                'name' => 'email',
                'class' => 'form-control',
                'value' => '',
                'type' => 'text',
                //'placeholder' => 'Ej: david',
                'required' => 'required',
                'autofocus' => 'autofocus'
            ),
            'password' => array(
                'id' => 'password',
                'name' => 'password',
                'class' => 'form-control',
                'value' => '',
                'type' => 'password',
                //'placeholder' => '******',
                'required' => 'required'
            )
        );

        // buttons form
        $this->data->form_button = array(
            'type' => 'submit',
            'class' => 'button_type_4 bg_scheme_color pull-left',
            'content' => 'Ingresar'
        );

        $this->load->view('backend/embeds/header.php', $this->data);
        $this->load->view('backend/login_view.php');
        $this->load->view('backend/embeds/footer.php');
    }

    public function recover()
    {

        $this->data->is_loggedin = $this->Login_model->is_loggedin();
        // Current user is loggedin?
        if ($this->data->is_loggedin == TRUE) {
            redirect('/backend/dashboard');
        }

        $this->data->meta_title = 'Recuperar contraseña';

        // form general attributes
        $this->data->form_attributes = array(
            'class' => 'login-form',
            'id' => 'login-form'
        );

        // inputs form
        $this->data->form_inputs = array(
            'email' => array(
                'id' => 'email',
                'name' => 'email',
                'class' => 'form-control',
                'value' => '',
                'type' => 'text',
                //'placeholder' => 'Ej: [email protected]',
                'required' => 'required',
                'autofocus' => 'autofocus'
            )
        );

        // buttons form
        $this->data->form_button = array(
            'type' => 'submit',
            'class' => 'button_type_4 bg_scheme_color pull-left',
            'content' => 'Enviar'
        );

        $this->load->view('backend/embeds/header.php', $this->data);
        $this->load->view('backend/login_recover.php');
        $this->load->view('backend/embeds/footer.php');
    }

    public function send_recover() {

        $this->session->set_flashdata('alert_type', 'danger');

        $this->form_validation->set_rules('email', '<strong>email</strong>', 'required');

        $this->form_validation->set_message('required', 'No has ingresado tu %s.');

        if ($this->form_validation->run() == TRUE) {

            if($this->aauth->remind_password($_POST['email'])){
                $this->session->set_flashdata('alert_messages', 'Hemos <strong>enviado</strong> un código de verificación al mail ingresado.');
                $this->session->set_flashdata('alert_type', 'success');
            }else{
                $this->session->set_flashdata('alert_messages', 'El <strong>email</strong> ingresado no existe.');
            }
            redirect('/backend/login/recover');

        } else {
            $this->session->set_flashdata('alert_messages', validation_errors());
            redirect('/backend/login/recover');
        }
    }

    public function reset_password($user_id, $ver_code)
    {

        if(!empty($user_id) && !empty($ver_code) && $this->aauth->reset_password($user_id, $ver_code)){
            $this->session->set_flashdata('alert_messages', 'Hemos <strong>enviado</strong> una nueva contraseña a tu email!');
            $this->session->set_flashdata('alert_type', 'success');
        }
        else{
            $this->session->set_flashdata('alert_messages', 'No hemos podido recuperar tu contraseña. Favor inténtelo nuevamente.');
            $this->session->set_flashdata('alert_type', 'danger');
        }

        redirect('/backend');

    }

    public function verify() {

        $this->form_validation->set_rules('email', '<strong>Email</strong>', 'required');
        $this->form_validation->set_rules('password', '<strong>Contraseña</strong>', 'required');

        $this->form_validation->set_message('required', 'No has ingresado tu %s.');

        if ($this->form_validation->run() == TRUE) {
            $remember = isset($_POST['remember']) ? true : false;
            if ($this->aauth->login($_POST['email'], $_POST['password'], $remember)) {

                //Si es vendedor y está confirmado su registro
                $this->load->model('Seller_model');
                if($seller = $this->Seller_model->get_seller_data($this->session->userdata('id'))){
                    if($seller->status != 1){
                        $this->aauth->logout();
                        if($seller->status == 0){
                            redirect('/login/processing');
                        }elseif($seller->status == 2){
                            redirect('/login/rejected');
                        }
                    }
                }

                redirect('/backend/dashboard');

            } else {
                foreach ($this->aauth->get_errors_array() as $error) {
                    $this->session->set_flashdata('alert_messages', $error);
                    $this->session->set_flashdata('alert_type', 'danger');
                }
                redirect('/backend');
            }
        } else {
            $this->session->set_flashdata('alert_messages', validation_errors());
            $this->session->set_flashdata('alert_type', 'danger');
            redirect('/backend');
        }
    }

    public function logout() {
        $this->aauth->logout();
        redirect('/');
    }

    public function login_seller() {

        $result = array('code' => 0, 'error' => '', 'token' => $this->security->get_csrf_hash());

        $this->form_validation->set_rules('email', '<strong>Email</strong>', 'required');
        $this->form_validation->set_rules('password', '<strong>Contraseña</strong>', 'required');
        $this->form_validation->set_message('required', 'No has ingresado tu %s.');

        if ($this->form_validation->run() == TRUE) {
            $form_data = (object) $this->input->post();
            if ($this->aauth->login($form_data->email, $form_data->password, true)) {

                $result['code'] = 1; //Usuario logueado

                //Si es vendedor y está confirmado su registro
                $this->load->model('Seller_model');
                if($seller = $this->Seller_model->get_seller_data($this->session->userdata('id'))){
                    if($seller->status != 1){
                        $this->aauth->logout();
                        if($seller->status == 0){
                            $result['code'] = 3;
                        }elseif($seller->status == 2){
                            $result['code'] = 4;
                        }
                    }
                }

            } else {
                $result['code'] = 2;
                foreach ($this->aauth->get_errors_array() as $error) {
                    $result['error'] .= $error;
                }
            }
        } else {
            $result['code'] = 2;
            $result['error'] = validation_errors();
        }

        echo json_encode($result);
    }

    public function recover_seller() {

        $result = array('code' => 0, 'error' => '', 'token' => $this->security->get_csrf_hash());

        $this->form_validation->set_rules('email', '<strong>Email</strong>', 'required');
        $this->form_validation->set_message('required', 'No has ingresado tu %s.');

        if ($this->form_validation->run() == TRUE) {
            $form_data = (object) $this->input->post();
            if($this->aauth->remind_password($form_data->email)){

                $result['code'] = 1; //Enviado

            }else{
                $result['code'] = 2;
                $result['error'] = 'El <strong>Email</strong> ingresado no existe.';
            }

        } else {
            $result['code'] = 2;
            $result['error'] = validation_errors();
        }

        echo json_encode($result);
    }

    public function reset_password_seller($user_id, $ver_code)
    {

        if(!empty($user_id) && !empty($ver_code) && $this->aauth->reset_password($user_id, $ver_code)){
            redirect('/login?r=ok');
        }
        else{
            redirect('/login?r=no');
        }

    }

}

The model Login_model.php

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

class Login_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    public function is_loggedin() {
        $is_loggedin = ($this->aauth->is_loggedin() ? true : false);
        return $is_loggedin;
    }

    public function logout() {
        $this->aauth->logout();
    }

    public function redirect_login() {
        // Current user is loggedin?
        if ($this->is_loggedin() == FALSE) {
            $this->session->set_flashdata('error-login-form', '<strong>ERROR!</strong> Primero debes iniciar sesión!');
            $url_referer = (uri_string() != 'login') ? '?redirect='.uri_string() : '';
            redirect('login'.$url_referer);
        } else {
            return true;
        }
    }

    public function is_admin() {
        return $this->aauth->is_admin();
    }

    public function is_editor() {
        return $this->aauth->is_member('editor');
    }

    public function is_seller() {
        return $this->aauth->is_member('seller');
    }

}

The login.php view

<div class="links-nav">
<div class="container">
  <div class="row">
     <div class="col-xs-12">
        <a href="<?=base_url()?>">Inicio</a>
        <i class="fa fa-angle-right" aria-hidden="true"></i>
        <span>Iniciar sesión</span>
     </div>
  </div>
 </div>
 </div>

<div id="section-login" class="form-page">
<div class="container">

  <div class="row">
     <div class="col-md-8">
        <div class="row">
           <div class="col-xs-12">
              <h1 class="title-st-1">Iniciar sesión</h1>
           </div>
        </div>
     </div>
  </div>

  <div class="row">
     <div class="col-xs-12">
        <div class="form-content">
           <h3>Accedé a tu cuenta</h3>
           <div class="form-login">
              <form id="form-login" class="form-horizontal" method="post">
                 <div class="form-group">
                    <div class="col-sm-6">
                       <label for="email">E-mail o C.I:</label>
                       <input type="email" class="form-control" id="email" name="email" required />
                    </div>
                 </div>
                 <div class="form-group">
                    <div class="col-sm-6">
                       <label for="password">Contraseña:</label>
                       <input type="password" class="form-control" id="password" name="password" required />
                       <a class="pull-right forgot-pass" href="<?php echo base_url(); ?>login/recover">Olvidó su contraseña?</a>
                    </div>
                 </div>
                 <div class="form-group btn-content">
                    <div class="col-sm-12">
                       <input type="hidden" id="url-referer" value="<?=$this->input->get('redirect')?>" />
                       <input type="hidden" class="aldea_token" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="" />
                       <button id="btn-form-submit" type="submit" class="btn btn-green btn-big">Iniciar Sesión</button>
                       <div class="not-has-account visible-xs">
                          ¿No tienes una cuenta? <a class="item-sigin" href="<?=base_url('registrarme')?>">Registrate aquí!</a>
                       </div>
                    </div>
                 </div>
              </form>
           </div>
        </div>
     </div>
  </div>
</div>

    
asked by Cesar 29.08.2018 в 19:31
source

1 answer

0

Here is an example of a login in codeigniter

VIEW

<form action="<?php echo base_url() ?>login/procesar" method="post">

<input type="email" name="email" placeholder="Email" autocomplete="off">
<input type="password" name="password" placeholder="Password" autocomplete="off">

<button type="submit">Iniciar Sesión</button>

CONTROLLER

public function procesar(){
$email      = $this->input->post('email');
$password   = $this->input->post('password');

$check_user = $this->config_ml->login_user($email,$password);

if($check_user == TRUE)
{
    $data = array(
        'is_logued_in'      =>  TRUE,
        'idusuario'         =>  $check_user->idusuario,
        'rut'               =>  $check_user->rut,
        'nombre'            =>  $check_user->nombre,
        'email'             =>  $check_user->email,
    );

    $this->session->set_userdata($data);
    $this->index();
}}

MODEL

function login_user($email,$password){
$this->db->where('email',$email);
$this->db->where('password',$password);

$query = $this->db->get('usuario');

if($query->num_rows() == 1)
{
    return $query->row();

}else{

    $this->session->set_flashdata('usuario_incorrecto','Los datos introducidos son incorrectos');
    redirect(base_url().'login','refresh');
}}
    
answered by 30.08.2018 в 21:47