guardian_trackable v0.2.2 GuardianTrackable

A Guardian hook to track user sign in. Tracks the following values:

  • sign_in_count - Increased every time a sign in is made
  • current_sign_in_at - A timestamp updated when the user signs in
  • last_sign_in_at - Holds the timestamp of the previous sign in
  • current_sign_in_ip - The remote ip updated when the user sign in
  • last_sign_in_ip - Holds the remote ip of the previous sign in

To use it, you'll need to setup your schema like this:

defmodule MyApp.User do
  use Ecto.Schema
  use GuardianTrackable.Schema

  schema "users" do
    guardian_trackable()
  end
end

Then, you can add the following configuration to your Guardian module:

defmodule MyApp.Guardian do
  use Guardian, otp_app: :my_app

  @impl true
  def after_sign_in(conn, resource, _token, _claims, _opts) do
    GuardianTrackable.track!(MyApp.Repo, resource, conn.remote_ip)
    {:ok, conn}
  end
end

Link to this section Summary

Functions

Updates a resource with tracking information.

Creates a changeset for tracking.

Link to this section Functions

Link to this function

track!(repo, resource, ip_address)
track!(
  repo :: Ecto.Repo.t(),
  resource :: Ecto.Schema.t(),
  ip_address :: :inet.ip_address()
) :: Ecto.Schema.t() | no_return()

Updates a resource with tracking information.

Example

iex> GuardianTrackable.track!(MyApp.Repo, user, {127, 0, 0, 1})
%User{
  current_sign_in_at: #DateTime<2017-10-31 19:42:42.372012Z>,
  current_sign_in_ip: "127.0.0.1",
  last_sign_in_at: #DateTime<2017-10-31 19:42:42.372012Z>,
  last_sign_in_ip: "127.0.0.1",
  sign_in_count: 1
}
Link to this function

trackable_changeset(resource, ip_address)
trackable_changeset(
  resource :: Ecto.Schema.t(),
  ip_address :: :inet.ip_address()
) :: Ecto.Changeset.t()

Creates a changeset for tracking.

Example

iex> GuardianTrackable.trackable_changeset(user, {127, 0, 0, 1})
%Ecto.Changset{changes: %{
  current_sign_in_at: #DateTime<2017-10-31 19:42:42.372012Z>,
  current_sign_in_ip: "127.0.0.1",
  last_sign_in_at: #DateTime<2017-10-31 19:42:42.372012Z>,
  last_sign_in_ip: "127.0.0.1",
  sign_in_count: 1
}}