CodeIgniter Forums
Getting User ID from Redux Auth - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Getting User ID from Redux Auth (/showthread.php?tid=18996)

Pages: 1 2


Getting User ID from Redux Auth - El Forum - 05-24-2009

[eluser]Timothy_[/eluser]
Hello,

I'm really lost on this one. I have Redux Auth installed on my system. I've got it so that it checks for a logged in status.

Code:
function index()
   {
      if($this->redux_auth->logged_in()){
        
    echo 'stuff' ;
  
        
    }else{
    
        echo 'other stuff';
        
    }
        
     }

But know I want to retrieve the USER_ID so that I can start querying the database.

Please bear with me here, I'm not very good at PHP and MySQL so Im really just learning by looking at examples. I haven't found any documentation on this one.

Someone in another forum said they were querying the session table. This kind of makes sense, however I have noticed that nothing is actually being inputed into the session table. Something else that I need to fix I guess.

Please help if you can.

Thanks

Tim


Getting User ID from Redux Auth - El Forum - 05-25-2009

[eluser]Dam1an[/eluser]
A very quick look in the redux code, and its stored as 'id'
so this should work 9once you get the session db lookups to work)

Code:
$this->session->userdata('id');



Getting User ID from Redux Auth - El Forum - 05-25-2009

[eluser]Timothy_[/eluser]
Hey Dam1an,

Thanks for your reply. Wow, I couldn't find that anywhere in the code. I must have been looking in the wrong places.

I managed to get my sessions database firing, but unfortunately I am getting no values whatsoever from

Code:
$this->session->userdata('id');

Could you please provide a little more info, or even just post the code where you found it in.

Thanks for all your help

Tim


Getting User ID from Redux Auth - El Forum - 05-25-2009

[eluser]Dam1an[/eluser]
Hmm, maybe its not being set properly when you login :-S
The relevant code from redux_auth.php is
Code:
public function login ($email, $password) // line 142
        {            
            # Grab hash, password, id, activation_code and banned_id from database.
            $result = $this->_login($this->users_table, $this->banned_table, $email);
    
            if ($result)
            {
    
                if(!empty($result->activation_code))
                {
                    $this->ci->session->set_flashdata('login', 'Your Account is Not Activated.');
                    return 'NOT_ACTIVATED';
                }
                elseif(!empty($result->banned_id))
                {
                    $this->ci->session->set_flashdata('login', 'Your Account has been banned for the following reason : '.$result->reason);
                    return 'BANNED';
                }
                else
                {
                    $password = sha1($this->salt.$result->hash.$password);
    
                    if ($password === $result->password)
                    {
                        $this->ci->session->set_userdata(array('id'=> $result->id)); // line 166
    
                        return true;
                    }
                }
            }
            
            return false;
        }

The part where it sets it is line 166

I can't really provide much more info, as I don't use this auth librray, this is just what I gathered from a quick look

To check the session data is being set properly, try this profiler extension, which also shows session data (make sure the profiler is enabled)


Getting User ID from Redux Auth - El Forum - 05-25-2009

[eluser]Timothy_[/eluser]
Hello,

Thanks so much for your reply. Unfortunately I just realised I have actually got Redux Auth version 2 BETA, (Doh!!!) so my code looks a little different from yours.

Really sorry about that :red:

This is how the login function works in version 2 Beta
Code:
public function login($identity = false, $password = false)
    {
        $identity_column = $this->config->item('identity');
        $users_table     = $this->tables['users'];
        
        if ($identity === false || $password === false || $this->identity_check($identity) == false)
        {
            return false;
        }
        
        $query = $this->db->select($identity_column.', password, activation_code')
                           ->where($identity_column, $identity)
                           ->limit(1)
                           ->get($users_table);
        
        $result = $query->row();
        
        if ($query->num_rows() == 1)
        {
            $password = $this->hash_password_db($identity, $password);
            
            if (!empty($result->activation_code)) { return false; }
            
            if ($result->password === $password)
            {
                $this->session->set_userdata($identity_column,  $result->{$identity_column});
                return true;
            }
        }
        
        return false;        
    }
}

And in Config

Code:
$config['tables']['groups'] = 'groups';
    $config['tables']['users'] = 'users';
    $config['tables']['meta'] = 'meta';
    
    /**
     * Default group, use name
     */
    $config['default_group'] = 'students';
    
    /**
     * Meta table column you want to join WITH.
     * Joins from users.id
     **/
    $config['join'] = 'user_id';
    
    /**
     * Columns in your meta table,
     * id not required.
     **/
    $config['columns'] = array('first_name', 'last_name');
    
    /**
     * A database column which is used to
     * login with.
     **/
    $config['identity'] = 'email';

    /**
     * Email Activation for registration
     **/
    $config['email_activation'] = false;
    
    /**
     * Folder where email templates are stored.
     * Default : redux_auth/
     **/
    $config['email_templates'] = 'redux_auth/';

    /**
     * Salt Length
     **/
    $config['salt_length'] = 10;

So from what I have interpreted, it no longer sets the user ID, in the session, but rather the users email address.

Does anyone see a better way to get a users ID from this code?

Thanks for all your help,

Tim


Getting User ID from Redux Auth - El Forum - 05-25-2009

[eluser]Timothy_[/eluser]
Hello,

I've tried adding a USER_ID field to the sessions table, but thats turning out to be a headache as well.

There's got to be a fairly simple way to get the current user_id. It's such an important piece of information and not much works without it...

Thanks for any help.

Tim


Getting User ID from Redux Auth - El Forum - 05-26-2009

[eluser]Dam1an[/eluser]
looking at the code, it only sets the users email (which is also a unique key) in the session data
If you want the user_id, you'll have to add that line to the login function yourself
(and you're right, the user_id is a key value, so I'm suprised it's not stored (unless it happens somewher eelse)


Getting User ID from Redux Auth - El Forum - 05-26-2009

[eluser]Timothy_[/eluser]
Well as I've said before Im not very good at PHP or MySQL, but ive got to get this working so Im prepared to look like an absolute noob.

The code that populates the user_data in the session table is
Code:
$identity_column = $this->config->item('identity');

if ($result->password === $password)
            {
                $this->session->set_userdata($identity_column,  $result->{$identity_column});
                
                return true;
                
            }
So first thing I did was add a field to the sessions database simply called user_id. Then I added the following code.
Code:
$identity_column = $this->config->item('identity');
$userid_column = $this->config->item('join');

if ($result->password === $password)
            {
                $this->session->set_userdata($identity_column,  $result->{$identity_column});
                $this->session->set_userid($userid_column, $result->{$userid_column});                
                return true;
                
            }

This doesnt work. It crashes on login. Ive tried changing the variables to direct values or various datatypes and nothing changes. Maybe you cant have two queries relating to the same table, but im not sure how to implement them within the same query.

I think the logic isnt far off, but my syntax is almost certainly wrong.

Please, if you can help.

Thanks

Tim


Getting User ID from Redux Auth - El Forum - 05-26-2009

[eluser]Dam1an[/eluser]
Try changing it to
Code:
// Assuming the user id is stored as 'id' in the user table
$this->session->set_userdata('user_id', $result->id);

// Assuming the user id is stored as 'user_id' in the user table
$this->session->set_userdata('user_id', $result->user_id);

You will also need to add this to the select clause (a little firther up) to be
Code:
$query = $this->db->select($identity_column.', password, activation_code, id')...
or
$query = $this->db->select($identity_column.', password, activation_code, user_id')...



Getting User ID from Redux Auth - El Forum - 05-26-2009

[eluser]Timothy_[/eluser]
Dam1an

You are obviously an expert.

Your suggestions are working perfectly, but obviously they are going into the user_data field in the sessions data base.

So i end up with a user_data field that looks like
Code:
a:2:{s:5:"email";s:13:"[email protected]";s:7:"user_id";s:1:"3";}

This is not a problem if you can reveal how I can extract the user id out of the user_data field.

Thank you again for all your help.

Tim