software engineer. former new yorker — current chicagoan

Mnesia Configuration Helper

@zackkitzmiller 2020-10-04 04:04

Important:

If you need to use the Mnesia library for cluster syncing make sure that you change the backing store for Pow here.

defmodule Z19rpwWeb.Credentials do
  @moduledoc "Authentication helper functions"

  alias Z19rpw.Users.User
  alias Phoenix.LiveView.Socket
  alias Pow.Store.CredentialsCache

  @doc """
  Retrieves the currently-logged-in user from the Pow credentials cache.
  """
  @spec get_user(
          socket :: Socket.t(),
          session :: map(),
          config :: keyword()
        ) :: %User{} | nil

  def get_user(socket, session, config \\ [otp_app: :z19rpw])

  def get_user(socket, %{"z19rpw_auth" => signed_token}, config) do
    conn = struct!(Plug.Conn, secret_key_base: socket.endpoint.config(:secret_key_base))
    salt = Atom.to_string(Pow.Plug.Session)

    with {:ok, token} <- Pow.Plug.verify_token(conn, salt, signed_token, config),
         {user, _metadata} <-
           CredentialsCache.get([backend: Pow.Store.Backend.MnesiaCache], token) do
      user
    else
      _any -> nil
    end
  end

  def get_user(_, _, _), do: nil
end