Alter the way Ion Auth works by using hooks. Get a gravatar after the user logs in

Created at: November 3, 2015; Last update: March 1, 2016

Ion Auth library has something called hooks. These hooks are just what they’re called… Hooks. In there you can attach any code you want by simply writing the appropriate functions. These hooks are triggered inside the library or model at specific moments within the workflow.

Why do we need hooks?

Well, ain’t that a question. Hooks are helpful because with their help you can at any time alter a script without intervening inside that respective script. This way, you can at any time update the script (in our case we can update the library directly from Github) without being affraid that this may destroy our functionalities.

First of all let’s see what hooks are available:

Inside the library we find:

– pre_password_change
– post_password_change
– password_change_unsuccessful
– password_change_successful
– pre_account_creation
– post_account_creation
– post_account_creation_successful
– post_account_creation_unsuccessful
– logout
– logged_in
– is_admin
– in_group

Inside the model we see:
– extra_where
– pre_activate
– post_activate
– post_activate_unsuccessful
– deactivate
– pre_change_password
– post_change_password
– post_change_password_unsuccessful
– post_change_password_successful
– username_check
– email_check
– identity check
– post_forgotten_password
– post_forgotten_password_unsuccessful
– post_forgotten_password_successful
– pre_forgotten_password_complete
– post_forgotten_password_complete_unsuccessful
– post_forgotten_password_complete_successful
– pre_register
– extra_set
– post_register
– pre_login
– post_login_unsuccessful
– post_login_successful
– limit
– offset
– where
– like
– select
– order_by
– row
– row_array
– result
– result_array
– num_rows
– users
– user
– get_users_group
– add_to_group
– remove_from_group
– groups
– group
– pre_update_user
– post_update_user
– post_update_user_unsuccessful
– post_update_user_successful
– pre_delete_user
– post_delete_user
– post_delete_user_unsuccessful
– post_delete_user_successful
– update_last_login
– set_lang
– pre_set_session
– post_set_session
– pre_remember_user
– post_remember_user
– remember_user_unsuccessful
– pre_login_remembered_user
– post_login_remembered_user
– post_login_remembered_user_unsuccessful
– post_login_remembered_user_successful
– extra_group_set
– post_delete_group
– post_delete_group_notallowed
– post_delete_group_unsuccessful
– post_delete_group_successful

Wow… A lot of hooks… But this is not important (it’s only to fill the page – I have a reputation to maintain).

As you can see by looking to the library’s code, the hooks are called by a trigger_events() method… What does that do? It simply looks if there are any $_ion_hooks defined ($_ion_hooks being an array).

From line 1986 of the Ion Auth model (line which may change in time), we can see that the hooks are created with the help of a set_hook() method which accepts an event name, a variable for storing an anonymous class (stdClass), a class name, a method name, and arguments.

A basic example. Take your gravatar after login

So let’s try and do something… After the user logged in we want to get its gravatar from Looking at the tutorial about grabing the users gravatars ( we see that in order to grab it we need the hashed email address of the user. So, after someone logins we will hash its email address and save it in a session variable named “gravatar”.

OK… Where will we put the code to make that hash? We can put it in the User controller, just where the login() method is. But what hook should we use? I’d say only if the user has successfully logged in we will save the “gravatar” session variable.

If we look inside the model of the Ion Auth we will see that the login() method triggers a hook named “post_login_successful”. So let’s set this hook up.

Now, going back to our User controller we see the login() method:

We will set up the hook after the form validation was successful:

OK… what did we do here? We called the Ion Auth set_hook() method to set up the post_login_successful hook. We gave this call a label named “get_gravatar_hash“, and we told it that it must look for a method named _gravatar() inside our current class. We won’t pass it anything as the user’s email will be taken from a session variable called “email“. But how did this variable get in there? Ion Auth created the session after a person was logged in successfully. Other session variables created by the login() method are: identity, username, user_id, and old_last_login.

Now we only need to create a method named _gravatar() in our User controller (class)

OK… Let’s see the User controller again:


Cool… Now. Let’s go to our dashboard view (application/views/dashboard/index_view.php) and see if we retrieve an image:

Well… If you’ve had a gravatar, after login you should see your gravatar on the dashboard (provided you’ve created an account with a different username than administrator…). Do you see it? Hope you’ve enjoyed this rather trivial tutorial… If not, you can at any time find another great tutorial on the same subject at: