Skip to content

Commit

Permalink
create Auth.Session.end_session/1 function for #30
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Nov 22, 2021
1 parent b675e3b commit eed5281
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
10 changes: 5 additions & 5 deletions lib/auth/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Auth.Session do
schema "sessions" do
field :app_id, :id
field :auth_provider, :string
field :end_at, :utc_datetime
field :end, :naive_datetime
# field :key_id, :integer
field :person_id, :id
field :user_agent_id, :id
Expand All @@ -18,7 +18,7 @@ defmodule Auth.Session do
end

def changeset(session, attrs) do
cast(session, attrs, [:app_id, :person_id, :auth_provider, :user_agent_id, :end_at])
cast(session, attrs, [:app_id, :person_id, :auth_provider, :user_agent_id, :end])
|> validate_required([:app_id, :person_id])
end

Expand All @@ -43,7 +43,7 @@ defmodule Auth.Session do
and # match on UA in case person has multiple devices/sessions
s.user_agent_id == ^Auth.UserAgent.get_user_agent_id(conn)
and # only the sessions that haven't been "ended"
is_nil(s.end_at),
is_nil(s.end),
# sort by most recent in case there are older un-ended sessions:
order_by: [desc: :inserted_at]
)
Expand All @@ -52,7 +52,7 @@ defmodule Auth.Session do
# update session to end it
def end_session(conn) do
get(conn)
|> changeset(%{end_at: DateTime.utc_now()})
|> Repo.update()
|> changeset(%{end: DateTime.utc_now()})
|> Repo.update!()
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Auth.Repo.Migrations.AddAppIdToSessions do
alter table(:sessions) do
add :app_id, references(:apps, on_delete: :nothing)
add :auth_provider, :string
add :end_at, :utc_datetime
add :end, :naive_datetime
remove :person_id # avoid tight coupling
add :person_id, references(:people, on_delete: :nothing)
add :user_agent_id, :integer
Expand Down
14 changes: 12 additions & 2 deletions test/auth/session_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Auth.SessionTest do
assert session.app_id == conn.assigns.person.app_id
assert session.person_id == conn.assigns.person.id
assert session.auth_provider == conn.assigns.person.auth_provider
assert session.end_at == nil
assert session.end == nil
end

test "Auth.Session.get/1 retrieves a session record", %{conn: conn} do
Expand All @@ -20,6 +20,16 @@ defmodule Auth.SessionTest do
assert session.app_id == ses.app_id
assert session.person_id == ses.person_id
assert session.auth_provider == ses.auth_provider
assert ses.end_at == nil
assert ses.end == nil
end

test "Auth.Session.end_session/1 updates the session record", %{conn: conn} do
conn = non_admin_login(conn)
session = Auth.Session.insert(conn)
assert session.end == nil

# End the Session:
updated = Auth.Session.end_session(conn)
assert updated.end == updated.updated_at
end
end

0 comments on commit eed5281

Please sign in to comment.