Well... I'm out of ideas on what to write about. Do you have any ideas? Please, write me in any comment section you find, and I will do my best to tell you about that particular subject

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.

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 gravatar.com. Looking at the tutorial about grabing the users gravatars (https://en.gravatar.com/site/implement/images/) 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.

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)

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: http://codebyjeff.com/blog/2013/01/using-hooks-with-ion_auth.