Welcome Guest, Not a member yet? Register   Sign In
User Authentication
#11

[eluser]cahva[/eluser]
Well I have done it this way:
- Create the auth library Smile It offcourse consists of login, logout, logged_in etc. methods
- add the auth library to autoload.
- Create the controller for the login and logout which has the login form etc.
- Now in every controller's construct you would have this:
Code:
function __construct()
{
    parent::Controller();
    if(!$this->auth->logged_in()) {
        redirect('login');
    }
    ...

I dont think 2 lines of code in every construct is asking too much Wink
#12

[eluser]John Fuller[/eluser]
You could also run your check login method from a hook.
#13

[eluser]Rick Jolly[/eluser]
[quote author="cahvarno" date="1222471906"]
I dont think 2 lines of code in every construct is asking too much Wink[/quote]
Why do it in every controller when you can do it in one parent controller?
#14

[eluser]Randy Casburn[/eluser]
[quote author="Rick Jolly" date="1222475169"][quote author="cahvarno" date="1222471906"]
I dont think 2 lines of code in every construct is asking too much Wink[/quote]
Why do it in every controller when you can do it in one parent controller?[/quote]

And the terror of the darkness flaps and mighty wisdom flows forth!
#15

[eluser]cahva[/eluser]
[quote author="Randy Casburn" date="1222475817"]And the terror of the darkness flaps and mighty wisdom flows forth![/quote]
Word! Smile

That was the way I did it in my first CI project and that was the beginning of summer so there might be some things I didnt optimize Wink
#16

[eluser]Moon 111[/eluser]
cahvarno, I'm sorry to say this, but that is a rather bad idea. What if you need to change it? What if you have 100 controllers? Or 1000?

My real question is "How do I create a parent controller, and how do I made it die if the user is not logged in.". For the die, should I just use a redirect?
#17

[eluser]Rick Jolly[/eluser]
Lots of examples of extending the controller on the forums. If you create a MY_Controller.php in your libraries folder, you can put any number of parent controller classes in that file. Here's an example: http://ellislab.com/forums/viewthread/90763/#458617
I prefer to just include/require the parent controller at the top of all extending controllers though:
Code:
include APPPATH . 'controllers/parents/admin.php';

class Home extends Admin
{
    function __construct()
    {
        parent::__construct();        
    }

        // ...
}

Use redirect in the authenticating parent's constructor. The CI redirect function calls "exit" after sending the redirect header.
#18

[eluser]Moon 111[/eluser]
Fatal error: Call to undefined method redirect() in C:\wamp\www\CodeIgniter\system\application\libraries\MY_Controller.php on line 51

Code:
<?php
// application/libraries/MY_Controller.php

Class MY_Controller Extends Controller {

    public function __construct() {
    
        parent::__construct();
    }
}



Abstract Class AuthController Extends MY_Controller {

    public function __construct() {
    
        parent::__construct();
    }
    
    abstract public function authenticate();
}



Class AuthNoneController Extends AuthController {

    public function __construct() {
    
        parent::__construct();
    
        if(!$this->authenticate()) {
        
            redirect(APPPATH . 'controllers/notloggedin.php');
        }
    }
    
    public function authenticate() {
    
        return true;
    }
}

Class AuthUserController Extends AuthController {

    public function __construct() {
    
        parent::__construct();
    
        if(!$this->authenticate()) {
        
            redirect(APPPATH . 'controllers/notloggedin.php');
        }
    }
    
    public function authenticate() {
    
        return false;
    }
}

?>

And my test.php controller is extending AuthUserController.

What is wrong with this code?
#19

[eluser]Moon 111[/eluser]
Fatal error: Call to undefined method redirect() in C:\wamp\www\CodeIgniter\system\application\libraries\MY_Controller.php on line 51

Everything besides the redirect works... Why not?
#20

[eluser]Rick Jolly[/eluser]
As with any helper or library in CI, you have to load it.




Theme © iAndrew 2016 - Forum software by © MyBB