BenFenner

BenFenner

I have a full working web app with user authentication (sessions) and other capability that I am adding a JSON API to.
The first thing I’d like to allow is user login through the API.

I’m following this API guide: Building a JSON API in Elixir with Phoenix 1.5+

In searching, I found this thread that seems related, but really isn’t: [Programming Phoenix] How do sessions work in Programming Phoenix?

Here are the basics:

router.ex

defmodule MyAppWeb.Router do
  use MyAppWeb, :router

  ###################################################################
  #
  # Pieplines
  #
  ###################################################################

# ...

  pipeline :api do
    plug :accepts, ["json"]
    plug :fetch_session
  end

  pipeline :api_auth do
    plug :ensure_authenticated
  end



  ###################################################################
  #
  # Scopes
  #
  ###################################################################

# ...

  scope "/api", MyAppWeb do
     pipe_through :api
     post "/login", SessionController, :api_create
   end

  scope "/api", MyAppWeb do
     pipe_through [:api, :api_auth]
     get "/logout", SessionController, :api_delete
     get "/settings", SessionController, :api_settings
   end

  ###################################################################
  #
  # Functions
  #
  ###################################################################

  defp ensure_authenticated(conn, _opts) do
    current_user = get_session(conn, :current_user)

    if current_user do
      conn
    else
      conn
      |> put_status(:unauthorized)
      |> put_view(MyAppWeb.ErrorView)
      |> render("401.json", message: "Unauthenticated user.")
      |> halt
    end
  end

end

session_controller.ex

defmodule MyAppWeb.SessionController do
  use MyAppWeb, :controller

  alias MyApp.Repo
  alias MyApp.Account.Authentication

  plug :put_layout, "hero.html"



  ###################################################################
  #
  # Internal Controller Actions
  #
  ###################################################################

# ...



  ###################################################################
  #
  # External (API) Controller Actions
  #
  ###################################################################

  def api_create(conn, params) do
    case Authentication.login(params, Repo) do
      {:ok, user} ->
        conn
        |> put_session(:current_user, user.id)
        |> put_status(:ok)
        |> put_view(MyAppWeb.SessionView)
        |> render("login.json", user: user)
      :error ->
        conn
        |> delete_session(:current_user)
        |> put_status(:unauthorized)
        |> put_view(MyAppWeb.ErrorView)
        |> render("401.json", message: "Invalid credentials.")
    end
  end

  def api_delete(conn, _) do
    conn
    |> delete_session(:current_user)
    |> put_status(:ok)
    |> put_view(MyAppWeb.SessionView)
    |> render("logout.json", message: "Logged out.")
  end

  def api_settings(conn, _) do
    conn
    |> put_view(MyAppWeb.SessionView)
    |> render("settings.json", message: "Settings!")
  end

end

session_view.ex

defmodule MyAppWeb.SessionView do
  use MyAppWeb, :view

  alias MyAppWeb.SharedView

  def render("login.json", %{user: user}) do
    %{
      data: %{
        user: %{
          id: user.id,
          email: user.email
        }
      }
    }
  end

  def render("logout.json", %{message: message}) do
    %{data: %{detail: message}}
  end

  def render("settings.json", %{message: message}) do
    %{data: %{detail: message}}
  end

end

I may have left out some files you’d like to see. Let me know and I will add them.

I am first logging into the site using the API like this:
curl -H "Content-Type: application/json" -X POST -d '{"username":"bfenner","password":"PasswordPassword"}' http://localhost:4000/api/login

That returns me the correct JSON hash of:
{"data":{"user":{"email":"ben.fenner@myapp.com","id":1}}}

I should then be able to access routes behind the api_auth check, and something like this should work:
curl -H "Content-Type: application/json" -X GET http://localhost:4000/api/settings -c cookies.txt -b cookies.txt -i

Instead of getting the expected JSON hash of {"data":{"detail":"Settings!"}} I instead do not pass the api_auth check and get a return of {"errors":{"detail":"Unauthenticated user."}}.

I would expect the router pipeline plugs to fetch the correct session, then run it through the ensure_authenticated function (which it does) but the authentication check fails, because it fetches what looks like an empty session.
What am I doing wrong?

Showing Posts 1 to 4

hlx

hlx

Do you need cookies? The most API’s I have seen return a token upon login for you to sent back with every request in something like a header.

See: Authorization header - HTTP | MDN

BenFenner

BenFenner OP

You’re making lots of sense, and I thought that might be the case earlier. So I decided to test the API against the eventual final solution, which is another web app running in a browser that would retain the cookie. However, I got distracted and never did that.

Mostly because the tutorial I’m following shows this should be possible without a token, or cookies (or is part of the curl command used to retain those?). Which I’m now doubting…

Is this possible?

hlx

hlx

According to that screenshot you forgot to add the cookie params in curl when you login. You need that so curl will persist the cookies so you can reuse them in the next request.

BenFenner

BenFenner OP

Oh, I sure did!
I was using the curl login command from earlier in the tutorial, which lacked the important cookie params.

Lets see how this goes!

Edit: It worked as expected! Thank you hlx for the second set of eyes. What a silly mistake.

— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews