router.ex (2485B)
1 defmodule DailyTrackerWeb.Router do 2 use DailyTrackerWeb, :router 3 4 import DailyTrackerWeb.UserAuth 5 6 pipeline :browser do 7 plug :accepts, ["html"] 8 plug :fetch_session 9 plug :fetch_live_flash 10 plug :put_root_layout, html: {DailyTrackerWeb.Layouts, :root} 11 plug :protect_from_forgery 12 plug :put_secure_browser_headers 13 plug :fetch_current_scope_for_user 14 end 15 16 pipeline :api do 17 plug :accepts, ["json"] 18 end 19 20 scope "/", DailyTrackerWeb do 21 pipe_through :browser 22 23 get "/", PageController, :home 24 end 25 26 # Other scopes may use custom stacks. 27 # scope "/api", DailyTrackerWeb do 28 # pipe_through :api 29 # end 30 31 # Enable LiveDashboard and Swoosh mailbox preview in development 32 if Application.compile_env(:daily_tracker, :dev_routes) do 33 # If you want to use the LiveDashboard in production, you should put 34 # it behind authentication and allow only admins to access it. 35 # If your application does not have an admins-only section yet, 36 # you can use Plug.BasicAuth to set up some basic authentication 37 # as long as you are also using SSL (which you should anyway). 38 import Phoenix.LiveDashboard.Router 39 40 scope "/dev" do 41 pipe_through :browser 42 43 live_dashboard "/dashboard", metrics: DailyTrackerWeb.Telemetry 44 forward "/mailbox", Plug.Swoosh.MailboxPreview 45 end 46 end 47 48 ## Authentication routes 49 50 scope "/", DailyTrackerWeb do 51 pipe_through [:browser, :require_authenticated_user] 52 53 live_session :require_authenticated_user, 54 on_mount: [{DailyTrackerWeb.UserAuth, :require_authenticated}] do 55 live "/entries", EntryLive.Index, :index 56 live "/entries/new", EntryLive.Form, :new 57 live "/entries/:id", EntryLive.Show, :show 58 live "/entries/:id/edit", EntryLive.Form, :edit 59 60 live "/users/settings", UserLive.Settings, :edit 61 live "/users/settings/confirm-email/:token", UserLive.Settings, :confirm_email 62 end 63 64 post "/users/update-password", UserSessionController, :update_password 65 end 66 67 scope "/", DailyTrackerWeb do 68 pipe_through [:browser] 69 70 live_session :current_user, 71 on_mount: [{DailyTrackerWeb.UserAuth, :mount_current_scope}] do 72 live "/users/register", UserLive.Registration, :new 73 live "/users/log-in", UserLive.Login, :new 74 live "/users/log-in/:token", UserLive.Confirmation, :new 75 end 76 77 post "/users/log-in", UserSessionController, :create 78 delete "/users/log-out", UserSessionController, :delete 79 end 80 end