Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking logins #585

Open
Sam23D opened this issue Mar 5, 2024 · 1 comment
Open

Tracking logins #585

Sam23D opened this issue Mar 5, 2024 · 1 comment

Comments

@Sam23D
Copy link
Contributor

Sam23D commented Mar 5, 2024

It would be nice if alongside the Users & Tokens resources ash_authentication also had a Login resource, that would create a new record whenever a user logs in.
This record can be used to track information about the user, either browser, device or location information. I'm thinking something simple like this:

defmodule Login do
  use Ash.Resource

  attributes do
    uuid_primary_key(:id)
    attribute(:user_id, :uuid)
    attribute(:meta, :map)
    timestamps()
  end
  
  actions do
    create :track_user_login do
      accept [:meta]
      argument :user_id, :uuid do
        allow_nil?(false)
      end
      change(manage_relationship(:user_id, :user, type: :append_and_remove))
    end
  end

end

Users can then add it it on the success/4 from the AuthController

defmodule MarketplacexWeb.AuthController do
  def success(conn, {login_type, _}=_activity, user, _token) do
    return_to = get_session(conn, :return_to) || ~p"/"
    Login.track_user_login (%{ meta: %{type: login_type }, user_id: user.id})

    conn
    |> delete_session(:return_to)
    |> store_in_session(user)
    |> assign(:current_user, user)
    |> redirect(to: return_to)
  end
end
@jimsynz
Copy link
Collaborator

jimsynz commented Mar 6, 2024

This is a great idea. I'd definitely welcome a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants