Skip to content

tweag/guardian_trackable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GuardianTrackable Build Status

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

Installation

The package can be installed by adding guardian_trackable to your list of dependencies in mix.exs:

def deps do
  [
    {:guardian_trackable, "~> 0.2.0"}
  ]
end

Use version 0.1.1 if you need Ecto 2.x support.

Usage

First, you'll need to add the columns for tracking to your table. You can generate the migration using Mix:

mix guardian_trackable.install

Alternatively, you can create it manually:

def change do
  alter table(:users) do
    add :sign_in_count, :integer, default: 0
    add :last_sign_in_ip, :string
    add :last_sign_in_at, :utc_datetime
    add :current_sign_in_ip, :string
    add :current_sign_in_at, :utc_datetime
  end
end

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
    field :email, :string
    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

Running tests

Before you can run the tests, you'll need to setup a database:

$ mix ecto.setup

Now, run the tests:

$ mix test

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •