Giter Site home page Giter Site logo

guardian_trackable's Introduction

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

guardian_trackable's People

Contributors

rzane avatar juddey avatar lleger avatar bgracie avatar

Stargazers

 avatar Jacob Luetzow avatar Hector De Diego avatar Joakim Nylén avatar sitch avatar Forest avatar Zak Singh avatar Ray avatar Ahsan Nabi Dar avatar Veera avatar Mayeu avatar axlk avatar marcos ferreira avatar Juri Hahn avatar Patrick McEvoy avatar Michael avatar Simon Bowen avatar Jordan P.R. avatar Yordis Prieto avatar  avatar @cdcme avatar  avatar Rich Smith avatar Jacky Alciné avatar Sam Gaw avatar Yue Zhuo avatar TierraLibre avatar

Watchers

Greg Sterndale avatar James Cloos avatar

guardian_trackable's Issues

Fails to Compile with Ecto 3.0

Hello!

Upgrading to Ecto 3.0 gives me this:

==> guardian_trackable
Compiling 3 files (.ex)

== Compilation error in file lib/mix/tasks/guardian_trackable.install.ex ==
** (CompileError) lib/mix/tasks/guardian_trackable.install.ex:39: undefined function migrations_path/1
    (stdlib) lists.erl:1338: :lists.foreach/2
    (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
could not compile dependency :guardian_trackable, "mix compile" failed. You can recompile this dependency with "mix deps.compile guardian_trackable", update it with "mix deps.update guardian_trackable" or clean it with "mix deps.clean guardian_trackable

Running with a fork right now, but wondered if you were interested in a PR. Cheers.

Add generator to bootstrap quicker

Great project! Had implemented something myself before but appreciated not having to do the work again.

It might be nice to include a generator with the package? Thinking the generator could create a migration and possible insert into a given schema. Would make getting started with this package a bit easier. Would be happy to take a stab at a PR if you think it’s a good idea.

Thanks again!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.