benedmunds / CodeIgniter-Ion-Auth

Simple and Lightweight Auth System for CodeIgniter
http://benedmunds.com/ion_auth/
MIT License
2.35k stars 1.14k forks source link

How to get user id? #1543

Closed Superheld closed 2 years ago

Superheld commented 2 years ago

Hi out there!

I'm struggling with the simple question "how to get user id"... :)

As I saw in the documentation user() should return everything of the logged in user. But if I do a print_r() on this, I only see my configuration an that kind of stuff. What do I misunderstood?

Thats how I used it:

` $this->ionAuth->user()->user_id $this->ionAuth->user()->id $this->ionAuth->user()

$this->ionAuth->loggedIn() ` last line work, so model is loaded and I can use it. Just to say this too :-)

Thank to all here and have a nice 2022 ;-)

gienq commented 2 years ago

Hello, if you want to get the id of the currently logged-in user, you can use this: $this->ion_auth->user()->row()->id; and it applies to each attribute in table, like first_name, active (status), email.

dgvirtual commented 2 years ago

Are there any drawbacks to retrieving the user id from the session? $_SESSION['user_id'] ?

avenirer commented 2 years ago

Are there any drawbacks to retrieving the user id from the session? $_SESSION['user_id'] ?

Even though it is used by the library, by accessing it you have no certainty that the user_id is created by Ion Auth

dgvirtual commented 2 years ago

Ah, and yet, if I change the $_SESSION['user_id'] value, IonAuth starts to think I am a different user :) So I guess that variable is all the memory that IonAuth has of the logged in user in a particular session...

benedmunds commented 2 years ago

Hey, the only downside to grabbing it from the session directly is that you won't know if the user's account has been deactivated or deleted.