Adding the user to the session

This commit is contained in:
2023-02-18 16:39:17 +00:00
parent e3a9f2f77e
commit 47c92ad44a
4 changed files with 32 additions and 2 deletions

View File

@@ -0,0 +1,27 @@
defmodule PhoenixApiTemplateWeb.Auth.SetUser do
import Plug.Conn
alias PhoenixApiTemplateWeb.Auth.ErrorResponse
alias PhoenixApiTemplate.Accounts
def init(_options) do
end
def call(conn, _options) do
if conn.assigns[:user] do
conn
else
user_id = get_session(conn, :user_id)
if user_id == nil do
raise ErrorResponse.Unauthorized
end
user = Accounts.get_user!(user_id)
cond do
user_id && user -> assign(conn, :user, user)
true -> assign(conn, :user, nil)
end
end
end
end