zizheng

zizheng

I’m creating a Phoenix API backend that will be used with a Vue frontend app. I have experience building a traditional server-rendered app that uses Pow for authentication, but have never built a SPA before.

I’ve gone through How to use Pow in an API — Pow v1.0.39, which makes me question if Pow is the right choice for my case. Our app is an internal management system used by at most a dozen users, so stateless token authentication shouldn’t be a requirement (i.e. we can afford to check the DB on every request), and introducing the refresh token & access token distinction seems to introduce a lot of unnecessary complexity.

Would it be reasonable in my case to hand-roll something like this?

  • Authenticate against a users table
  • Store sever-side sessions in a sessions table in say Postgres, with roughly the following schema: id, user_id, inserted_at, updated_at. The updated_at column can be used to implement a TTL, say 2 weeks, and can be updated every time an authenticated user sends a request.
  • The token sent to the frontend app is simply a signed session ID.
  • (I’m not sure what to do if I want to access additional information, say user permissions, in the frontend app though. In a server-rendered Pow app, I would just consult a permissions field on the current_user assign set by Pow. Any suggestion is appreciated!)

Showing Posts 1 to 9

lud

lud

You don’t have to do that at all if the only consumer for your API is the Vue app ; you can just use the default authentication mechanism from Pow. All you have to do is to ensure that the credentials (i.e. cookies) are enabled in Vue resource (if you use this library).

To access informations such as permissions I would simply add an API route to fetch the info.

hauleth

hauleth

You do not need Pow for that. I would just use Plug.Session with proper Plug.Session.Store.

zizheng

zizheng OP

Is there anything wrong if I do what @lud suggested and just use the normal session-based approach with Pow?

hauleth

hauleth

You do not need Pow in that case. Plug.Session is built in feature into plug, so you have that already.

zizheng

zizheng OP

I’m not sure I follow. That’s just the session part though, I’ll still need to actually do the authentication somehow, either with Pow or hand-roll my own solution.

hauleth

hauleth

Create your custom store (untested, written from memory):

defmodule Plug.Session.PostgreSQL do
  @behaviour Plug.Session.Store

  @impl true
  def init(opts), do: opts

  @impl true
  def get(conn, cookie, %{repo: repo, table: table, salt: salt}) do
    with {:ok, sid} <- Phoenix.Token.verify(conn, salt, cookie),
         session when not is_nil(session) <- repo.get(table, sid)
    do
      {sid, session.data}
    else
      _ -> {nil, %{}}
    end
  end

  @impl true
  def put(conn, sid, data, %{repo: repo, table: table, salt: salt}) do
    session = %{id: sid, data: data}

    with {1, [session]} <- repo.insert_all(table, [session], on_conflict: {:replace, [:data]}, returning: true) do
      Phoenix.Token.sign(conn, salt, session.id)
    else
      _ -> ""
    end
  end

  @impl true
  def delete(_conn, sid, %{repo: repo, table: table}) do
    repo.delete_all(from(s in table, where: s.id == ^sid))
    :ok
  end
end

And then in your controller:

  • Create session: put_session(conn, :user_id, uid)
  • Get session: get_session(conn, :user_id)
  • Destroy session: delete_session(conn, :user_id)
lud

lud

If Pow id used for registration, password recovery and all other features, there is no need to implement your own session solution.

hauleth

hauleth

If - this is important part, if it is not used, then you can implement most of it’s features quite quickly without all that fuss.

danschultzer

danschultzer

Pow Core Team

Yeah, @lud’s recommendation is the best approach. You won’t have to do any additional work and you’ll have session renewal out of the box.

The API guide, though being a simple generic way of setting up API auth, is more useful for mobile apps, and third party integration.

— All posts loaded —

Where Next? Top

Trending in Questions Top

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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews