Guardian Hooks
• elixir and guardian
Some time ago I introduced Hooks to Guardian. Hooks are a mechanism for you to plug-in to lifecycle of authentication. These can be useful to extend or customize the behaviour of Guardian within your application.
A Guardian.Hook is implemented as a behvaiour with default implementations of each callback so you only need to implement what you’re interested in. The available hooks are:
To create your own module just use the Guardian.Hooks module.
before_encode_and_sign
Runs before the jwt is generated. This can be used to add claims, or halt and return an error.
after_encode_and_sign
This runs after the JWT has been encoded. Returning an error will not halt, but can be used to to extend behaviour. For example store the token in a DB.
after_sign_in
Runs after a token is signed in (via a session). Use this to record information about logins etc.
before_sign_out
Before logging out of the session, these hooks run. The session will be logged out regardless but provides an opportunity to record it.
on_verify
Runs after claims have successfully been verified from the JWT which occurs every time a request is verified, which could come from a session, header or channel.
on_revoke
Called via Guardian.revoke!. This is run when the token should be considered revoked. By default Guardian takes no specific steps to consider a token revoked. If you’re storing tokens in the DB though, this would be the time to delete them.