Add Guardian.DB to track API tokens.

This commit is contained in:
2024-08-31 14:32:08 +03:00
parent 8697be9f17
commit 2f4fc790be
6 changed files with 54 additions and 4 deletions
+24
View File
@@ -28,4 +28,28 @@ defmodule LinkShortenerWeb.Auth.Guardian do
{:ok, token, _claims} = encode_and_sign(user)
{:ok, user, token}
end
def after_encode_and_sign(resource, claims, token, _options) do
with {:ok, _} <- Guardian.DB.after_encode_and_sign(resource, claims["typ"], claims, token) do
{:ok, token}
end
end
def on_verify(claims, token, _options) do
with {:ok, _} <- Guardian.DB.on_verify(claims, token) do
{:ok, claims}
end
end
def on_refresh({old_token, old_claims}, {new_token, new_claims}, _options) do
with {:ok, _, _} <- Guardian.DB.on_refresh({old_token, old_claims}, {new_token, new_claims}) do
{:ok, {old_token, old_claims}, {new_token, new_claims}}
end
end
def on_revoke(claims, token, _options) do
with {:ok, _} <- Guardian.DB.on_revoke(claims, token) do
{:ok, claims}
end
end
end
+4 -1
View File
@@ -11,9 +11,12 @@ defmodule LinkShortenerWeb.Telemetry do
children = [
# Telemetry poller will execute the given period measurements
# every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000},
# Add reporters as children of your supervision tree.
# {Telemetry.Metrics.ConsoleReporter, metrics: metrics()}
# to sweep the REST API tokens:
{Guardian.DB.Sweeper, [interval: 60 * 60 * 1000]} # 1 hour
]
Supervisor.init(children, strategy: :one_for_one)