• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where should the authentication function go?

#1
[eluser]bloodagar[/eluser]
Hi I'm just a newbie on CI and I want to know:
What's the best pratice for creating user/pass checking functions on a login form? Should it be in the Model (since the model handles data from the database, that's what I understand) or should I create a custom Library?

Thanks in advance! Smile

#2
[eluser]bloodagar[/eluser]
Update: Got it resolved! I changed
Code:
$this->db->get('users')
to
Code:
$this->db->get('users')->result_array()
in the if condition. Yay! Smile

Quote:Also I got another problem, whatever I type in the username/password inputs it allows me to view the secure area. Here's the code:

the model
Code:
<?php

class MLogin extends Model {
    
    function MLogin()
    {
        parent::Model();
    }
    
    function login($user, $pass)
    {
        $this->db->where('username', $user);
        $this->db->where('password', $pass);
        if ($this->db->get('users')) {
            $this->session->set_userdata('logged_in', TRUE);
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    function logged_in()
    {
        if ($this->session->userdata('logged_in')==TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
}

?>

the controller
Code:
<?php

class Login extends Controller {
    
    function Login()
    {
        parent::Controller();
        $this->load->scaffolding('users');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->model('mlogin');
    }
    
    function index()
    {
        $data['error'] = "";
        $user = $this->input->post('username');
        $pass = $this->input->post('password');
        if ($user) {
            if ($this->mlogin->login($user, $pass)) {
                redirect('login/secure', 'refresh');
            } else {
                $data['error'] = "Incorrect username/password";
                $this->load->view('login', $data);
            }
        } else {
            $this->load->view('login', $data);
        }
    }
    
    function secure()
    {
        if ($this->mlogin->logged_in()) {
            $this->load->view('secure');
        } else {
            redirect('login', 'refresh');
        }
    }
    
}


?>

I'd also appreciate of you guys have any suggestion/comments about my codes. Thank you very much!

#3
[eluser]Colin Williams[/eluser]
To the original topic, I've actually just considered rewriting a User library I wrote as a Model. I don't know that I like the Model interacting with the session and doing the access checks, among other things. But, I wouldn't want to have it all scattered across a user model, user library and user helper (which might be appropriate). I don't know. I'm still up in the air about it :-P

#4
[eluser]RogerM[/eluser]
I am not sure about this either.

I have my user auth in a model now, but something doesn't feel right.

Roger

#5
[eluser]bloodagar[/eluser]
Colin Williams: Thanks for the insights! I'm thinking: If it involves the database, put it in the model. Otherwise put it in a library.

RogerM: I feel the same way. Actually a lot of times I feel unsure because I'm new to CI and I'm worried about developing with bad practices in my code...

#6
[eluser]RogerM[/eluser]
I am new and learning, I have restarted my current app a few different times, because I learned something new.

Hope everyhting works out


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.