Skip to content

Commit

Permalink
Accept additional on_mount hooks to oban_dashboard
Browse files Browse the repository at this point in the history
Closes #93
  • Loading branch information
sorentwo committed Apr 6, 2024
1 parent f447d4a commit 63ccfad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/oban/web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ defmodule Oban.Web.Router do
Note that the default name is `Oban`, setting `oban_name: Oban` in the example above was purely
for demonstration purposes.
### On Mount Hooks
You can provide a list of hooks to attach to the dashboard's mount lifecycle. Additional hooks
are prepended before [Oban Web's own Authentication](Oban.Web.Resolver). For example, to run a
user-fetching hook and an activation checking hook before mount:
```elixir
scope "/" do
pipe_through :browser
oban_dashboard "/oban", on_mount: [MyApp.UserHook, MyApp.ActivatedHook]
end
```
### Customizing the Socket Connection
Applications that use a live socket other than "/live" can override the default socket path in
Expand Down Expand Up @@ -210,7 +224,7 @@ defmodule Oban.Web.Router do

Enum.each(opts, &validate_opt!/1)

session_name = Keyword.get(opts, :as, :oban_dashboard)
on_mount = Keyword.get(opts, :on_mount, []) ++ [Oban.Web.Authentication]

session_args = [
prefix,
Expand All @@ -222,11 +236,13 @@ defmodule Oban.Web.Router do
]

session_opts = [
on_mount: Oban.Web.Authentication,
on_mount: on_mount,
session: {__MODULE__, :__session__, session_args},
root_layout: {Oban.Web.Layouts, :root}
]

session_name = Keyword.get(opts, :as, :oban_dashboard)

{session_name, session_opts, as: session_name}
end

Expand Down
10 changes: 10 additions & 0 deletions test/oban/web/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ defmodule Oban.Web.RouterTest do
assert %{"access" => :read_only, "refresh" => 5, "user" => %{id: 1}} = session
end

test "passing additional on_mount hooks through to session opts" do
{_name, sess_opts, _opts} = Router.__options__("/oban", [])

assert [Oban.Web.Authentication] = Keyword.get(sess_opts, :on_mount)

{_name, sess_opts, _opts} = Router.__options__("/oban", on_mount: [My.Hook])

assert [My.Hook, Oban.Web.Authentication] = Keyword.get(sess_opts, :on_mount)
end

test "falling back to default values with a partial resolver implementation" do
conn =
:get
Expand Down

0 comments on commit 63ccfad

Please sign in to comment.