neiam
/apollos_crib
It's Apollo's Crib
public · Issues · Pulls 1 · Labels · Forks · Compare · Actions failure · Badges · Packages
⭐
Log in to mark this repository.
support endpoints with optional auth
31c5c5c · gmorell · 2022-08-01 14:14
Files changed
modified
apps/room_sanctum/lib/room_sanctum_web.ex
+9
−0
@@ -60,6 +60,15 @@ defmodule RoomSanctumWeb do
| 60 | 60 | end |
| 61 | 61 | end |
| 62 | 62 | |
| 63 | + def live_view_ca do | |
| 64 | + quote do | |
| 65 | + use Phoenix.LiveView, | |
| 66 | + layout: {RoomSanctumWeb.LayoutView, "live.html"} | |
| 67 | + on_mount RoomSanctum.UserLiveCanAuth | |
| 68 | + unquote(view_helpers()) | |
| 69 | + end | |
| 70 | + end | |
| 71 | + | |
| 63 | 72 | def live_component do |
| 64 | 73 | quote do |
| 65 | 74 | use Phoenix.LiveComponent |
modified
apps/room_sanctum/lib/room_sanctum_web/live/landing_live/index.ex
+1
−1
@@ -1,5 +1,5 @@
| 1 | 1 | defmodule RoomSanctumWeb.LandingLive.Index do |
| 2 | − use RoomSanctumWeb, :live_view_a | |
| 2 | + use RoomSanctumWeb, :live_view_ca | |
| 3 | 3 | |
| 4 | 4 | alias RoomSanctum.Configuration |
| 5 | 5 | alias RoomSanctum.Configuration.Query |
modified
apps/room_sanctum/lib/room_sanctum_web/live/live_auth.ex
+21
−0
@@ -17,3 +17,24 @@ defmodule RoomSanctum.UserLiveAuth do
| 17 | 17 | end |
| 18 | 18 | end |
| 19 | 19 | end |
| 20 | + | |
| 21 | +defmodule RoomSanctum.UserLiveCanAuth do | |
| 22 | + import Phoenix.LiveView | |
| 23 | + alias RoomSanctum.Accounts | |
| 24 | + | |
| 25 | + def on_mount(:default, _params, session, socket) do | |
| 26 | + case session["user_token"] do | |
| 27 | + nil -> | |
| 28 | + socket = assign_new(socket, :current_user, fn -> nil end) | |
| 29 | + {:cont, socket} | |
| 30 | + _val -> | |
| 31 | + socket = | |
| 32 | + assign_new( | |
| 33 | + socket, | |
| 34 | + :current_user, | |
| 35 | + fn -> Accounts.get_user_by_session_token(session["user_token"]) end | |
| 36 | + ) | |
| 37 | + {:cont, socket} | |
| 38 | + end | |
| 39 | + end | |
| 40 | +end |
Parents: 95126da