mirror of
https://github.com/mirego/accent.git
synced 2024-09-11 18:58:14 +03:00
Fix revoked at on old token
This commit is contained in:
parent
fa539362e4
commit
bcab4527f2
@ -1,5 +1,7 @@
|
||||
defmodule Accent.UserRemote.TokenGiver do
|
||||
@moduledoc false
|
||||
import Ecto.Query
|
||||
|
||||
alias Accent.Repo
|
||||
alias Accent.Utils.SecureRandom
|
||||
|
||||
@ -17,9 +19,9 @@ defmodule Accent.UserRemote.TokenGiver do
|
||||
end
|
||||
|
||||
defp invalidate_tokens(user) do
|
||||
user
|
||||
|> Ecto.assoc(:private_access_tokens)
|
||||
|> Repo.update_all(set: [revoked_at: DateTime.utc_now(), updated_at: DateTime.utc_now()])
|
||||
query = from(access_tokens in Ecto.assoc(user, :private_access_tokens), where: is_nil(access_tokens.revoked_at))
|
||||
|
||||
Repo.update_all(query, set: [revoked_at: DateTime.utc_now(), updated_at: DateTime.utc_now()])
|
||||
end
|
||||
|
||||
defp create_token(user) do
|
||||
|
@ -1,12 +1,4 @@
|
||||
defmodule Accent.Repo do
|
||||
use Ecto.Repo, otp_app: :accent, adapter: Ecto.Adapters.Postgres
|
||||
use Scrivener, page_size: 30, max_page_size: 10_000
|
||||
|
||||
@doc """
|
||||
Dynamically loads the repository url from the
|
||||
DATABASE_URL environment variable.
|
||||
"""
|
||||
def init(_, opts) do
|
||||
{:ok, Keyword.put(opts, :url, Application.get_env(:accent, Accent.Repo)[:url])}
|
||||
end
|
||||
end
|
||||
|
@ -14,11 +14,16 @@ defmodule AccentTest.UserRemote.TokenGiver do
|
||||
user = Repo.insert!(@user)
|
||||
token = Repo.insert!(Map.merge(@token, %{user_id: user.id}))
|
||||
|
||||
existing_revoked_token =
|
||||
Repo.insert!(%AccessToken{token: "revoked", revoked_at: NaiveDateTime.utc_now(:second), user_id: user.id})
|
||||
|
||||
TokenGiver.grant_token(user)
|
||||
|
||||
revoked_token = Repo.get_by!(AccessToken, token: token.token)
|
||||
reload_existing_revoked_token = Repo.reload!(existing_revoked_token)
|
||||
|
||||
assert revoked_token.revoked_at !== nil
|
||||
assert reload_existing_revoked_token.revoked_at === existing_revoked_token.revoked_at
|
||||
end
|
||||
|
||||
test "create token" do
|
||||
|
Loading…
Reference in New Issue
Block a user