Welcome Guest, Not a member yet? Register   Sign In
Login redirects
#1

[eluser]doubleplusgood[/eluser]
Hi there,
I have a working login form for my site. But I need to expand on this so that different user types are redirected to different pages. Currently, when everyone logs in, they are redirected to account/vehicle_list. But I now want all users who have 'isPublic' in their db record to be redirected to account/vehicle_publiclist and all users with 'isDealer' redirected to account/vehicle_list.

My account controller has the following functions;

Code:
public function index()
    {
    
        $view_data = array();
    
        if ( $this->auth->logged_in() )
        {
            redirect('account/vehicle_list');
        }
        else
        {
            $view_data['title1'] = "Log in, or join in...";
            $view_data['title2'] = "it's up to you";
            $view_data['body'] = "login";
            $view_data['regions'] = $this->dealers->getRegions();
            $view_data['view_file'] = 'account/index';
        }
            
        $this->load->view('layout', $view_data);
    
    }

    public function login()
    {
        $email = $this->input->post('email');
        $password = $this->input->post('password');
        
        if ( empty($email) || empty($password) )
        {
            die('fill it in!');
        }
        
        $logged_in = $this->auth->login($email, $password);
        
        redirect('account');
    }

And my Auth library has;

Code:
function login($email, $password)
        {
            $this->_obj->db->where('email', $email);
            
            $query = $this->_obj->db->get('users', 1);
            
            if ( $query->num_rows() == 0 )
            {
                return FALSE;
            }
            
            $user = $query->row_array();
            
            $hashed = $this->_hash($password, $email, $user['salt']);
            
            if ( $user['password'] === $hashed['hashed'] )
            {
                $this->_set_session($user);
                
                return TRUE;
            }
            
            return FALSE;
        }

So, I'm wondering if someone might be able to help me figure out how to redirect users based on the isDealer or isPublic being set in their db record?

Thanks,
Neil
#2

[eluser]pickupman[/eluser]
Rather than returning just TRUE, return something more meaningful like at least a user_id or return the whole $user array/result. Still pass FALSE on a failed login. Then you can test if the login has succeeded.

Code:
//Auth library
if ( $user['password'] === $hashed['hashed'] )
            {
                $this->_set_session($user);
                
                return $user;
            }

//Controller
public function login()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('email','email address','required|trim|valid_email');
        $this->form_validation->set_rules('password','password','required|trim');

        if($this->form_validation->run() == FALSE)
           redirect('auth/login'); //Empty login fields

        $email = $this->input->post('email');
        $password = $this->input->post('password');
        
        
        $logged_in = $this->auth->login($email, $password);

        if($logged_in == FALSE)
          redirect('auth/login');  //bad login buddy

        $uri = ($logged_in['group'] == 'isDealer') ? 'vehicle_list' : 'vehicle_publiclist'; //what group are we in
        
        redirect('account/'.$uri);
    }
#3

[eluser]doubleplusgood[/eluser]
Awesome, thank you very much man. Now i just have to figure out how to show a different toolbar of account links, based on the type of account logged in. Smile

I currently have a view layout with the following code;

Code:
if ( $this->auth->logged_in() )
{
    $this->load->view('partials/myaccount');
}
else
{
    $this->load->view('partials/subnav');
}

But ideally, i need a third option here, to show a partials/myaccountpublic if the isPublic group is set to 1. So if a dealer is logged in with isDealer set in their db record, then they would just see the partials/myaccount.

In the Auth library, there is the function logged_in as follows;

Code:
function logged_in()
        {
            return $this->_user['logged_in'];
        }
        
        function get_user()
        {
            return $this->_user;
        }
#4

[eluser]pickupman[/eluser]
Code:
$logged = $this->auth->logged_in();

if ( $logged['group'] == 'isDealer' ){
    $this->load->view('partials/dealer_toolbar');
}elseif($logged['group'] == 'isPublic'){
    $this->load->view('partials/public_toolbar');
}else{
    $this->load->view('partials/subnav');
}
#5

[eluser]doubleplusgood[/eluser]
Thanks man. The redirect based on whether the user is a Dealer or Public seems to work fine, but both account types see the public_toolbar with the following code;

Code:
$logged = $this->auth->logged_in();
if ( $logged['isPublic'] == '1' ){
    $this->load->view('partials/public_toolbar');
}elseif($logged['isDealer'] == '1' ){
    $this->load->view('partials/dealer_toolbar');
}else{
    $this->load->view('partials/subnav');
}

My code is slightly different to your solution as the DB stores account types like so;

isDealer => 0 or 1
isPublic => 0 or 1
#6

[eluser]pickupman[/eluser]
The double equals may still be evaluating to boolean (isset). Try triple ===
Code:
$logged = $this->auth->logged_in();
if ( $logged['isPublic'] === '1' ){
    $this->load->view('partials/public_toolbar');
}elseif($logged['isDealer'] === '1' ){
    $this->load->view('partials/dealer_toolbar');
}else{
    $this->load->view('partials/subnav');
}
#7

[eluser]doubleplusgood[/eluser]
Hmm, weird. Doesn't seem to work with triple === either. :S
#8

[eluser]doubleplusgood[/eluser]
Damn, just realised the login redirect isn't actually working either. Thought it was. Confused

Do i need to set the 'isDealer' or 'isPublic' in the session data perhaps?

Really appreciate you taking the time to help me on this.
#9

[eluser]pickupman[/eluser]
Really no need for the session unless, your auth library is setting some sort of variable. Did you change your login method to return the user profile rather than TRUE/FALSE? If so, what do you get when you var_dump($logged_in) in your controller after it calls the login method?

You will probably need to comment out the redirects in order to see this.
#10

[eluser]doubleplusgood[/eluser]
Hey man, so I did a print_r for $logged_in and it displayed this;

Code:
Array ( [id] => 46 [group_id] => 1 [email] => MYEMAIL [password] => PASSWORDHASH [salt] => SALT [first_name] => [last_name] => [reset_password_hash] => [active] => 1 [username] => dealertest [credits] => 10 [business_name] => dealer [business_address] => [business_region] => 9 [business_postcode] => [business_description] => [business_services] => [business_email] => [business_phone] => [business_website] => [business_photo] => [created_on] => 2010-05-25 20:59:34 [image1] => [image2] => [image3] => [image4] => [isDealer] =>  [isPublic] => ) 1

The thing is, that this array is misreporting as the account that I am actually logged in as has the following set in the DB record;

isDealer => 1
isPublic => 0

As you will see above, the array is saying that isPublic is set to 1 for this user. :S




Theme © iAndrew 2016 - Forum software by © MyBB