Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter ion auth trying to get the user name of a logged in user in to a string

Hi team i am new to CI and ion auth and i want to find the best way to get the user name on the logged user and make it a string. I have try this and im getting an error.

Object of class CI_Email could not be converted to string

This is my Controller

 class Dashboard extends CI_Controller {

    function index() {
        $user = $this->ion_auth->user();
        $email = $user->email;  

        $data['email']= $email;
        $data['mainContent'] = 'dashboard_view';
        $this->load->view('template', $data);
    }

and view

<div id='dashboarWap'>
    <?php echo form_open('dashboard/dashInput'); ?> 

    <?php echo $email; ?>

Any idea on why i am getting this error would be a big help thanks.

like image 229
1ftw1 Avatar asked Jan 31 '12 22:01

1ftw1


2 Answers

Ion Auth sets the username, email, user id, and last login time in session variables when the user logs in (have a look at ln 683 ish in the ion_auth_model's login function.)

The simplest way to get this info is by using CI's session class like so:

$username = $this->session->userdata( 'username' );

Of course, I abuse helpers so to make this even simpler I'll normally use a variant of this helper function:

function userdata( $key, $val = null ){
  $ci = &get_instance();
  if ( $val !== null ){
    $ci->session->set_userdata( $key, $val );
  } else {
    return $ci->session->userdata( $key );
  }
}

This gives us a handy (& global scoped) function to call on as needed, wherever needed:

$username = userdata( 'username' );
like image 141
Louis Avatar answered Oct 21 '22 17:10

Louis


$this->ion_auth->user() is a DB object.

$this->ion_auth->user()->row(); returns the user object which you can query for first_name, number_of_cats_owned, or whatever.

https://github.com/benedmunds/CodeIgniter-Ion-Auth/blob/2/controllers/auth.php

like image 36
Seabass Avatar answered Oct 21 '22 16:10

Seabass