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

API to receive a session token for mobile clients #112

Merged
merged 1 commit into from
Oct 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/web/controllers/session_token_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Web.SessionTokenController do
use Web, :controller

def create(conn = %{assigns: %{session_token: token}}, _params) do
conn
|> put_status(201)
|> json(%{session_token: token})
end
end
7 changes: 7 additions & 0 deletions lib/web/routers/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ defmodule Web.Router do
end

pipeline :session_token do
plug(:fetch_session)
plug Web.Plugs.SessionToken
end

Expand Down Expand Up @@ -178,6 +179,12 @@ defmodule Web.Router do
post("/token", TokenController, :create)
end

scope "/", Web do
pipe_through([:api, :session_token])

post("/session_tokens", SessionTokenController, :create)
end

if Mix.env() == :dev do
forward("/emails/sent", Bamboo.SentEmailViewerPlug)
end
Expand Down
27 changes: 27 additions & 0 deletions test/web/controllers/session_token_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Web.SessionTokenControllerTest do
use Web.ConnCase

describe "POST /session_tokens" do
test "a session token is returned", %{conn: conn} do
conn = post(conn, Routes.session_token_path(Web.Endpoint, :create))
assert %{"session_token" => token} = json_response(conn, 201)

assert {:ok, uuid} =
Phoenix.Token.verify(Web.Endpoint, "session token", token, max_age: 86_400)

assert UUID.info!(uuid)
end

test "an existing session token can be returned", %{conn: conn} do
conn =
conn
|> Plug.Test.init_test_session(session_token: "steve")
|> post(Routes.session_token_path(conn, :create))

assert %{"session_token" => token} = json_response(conn, 201)

assert Phoenix.Token.verify(Web.Endpoint, "session token", token, max_age: 86_400) ==
{:ok, "steve"}
end
end
end