5.8 KiB · text History 1ba5ec3
defmodule CartosnapAppWeb.Router do
  use CartosnapAppWeb, :router

  import CartosnapAppWeb.UserAuth
  import NeiamAuth.Router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :put_root_layout, html: {CartosnapAppWeb.Layouts, :root}
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug :fetch_current_scope_for_user
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  # The Android app's API. A paired device's key stands in for the session
  # cookie and assigns the same `current_scope`.
  pipeline :api_authenticated do
    plug :accepts, ["json"]
    plug CartosnapAppWeb.Api.Auth
  end

  # Which Android app may open this site's magic links (/users/log-in/:token)
  # as verified App Links. Package: config :neiam_auth, :app; signing certs:
  # ANDROID_CERT_FINGERPRINTS.
  get "/.well-known/assetlinks.json", NeiamAuth.App.AssetLinksPlug, []

  # Pairing: trading a code from the web for a device key, before there is one.
  scope "/api/v1", CartosnapAppWeb.Api.V1 do
    pipe_through :api

    post "/pair", PairingController, :create

    # The app's log-in page (shared with every NEIAM app; see
    # AuthController). Each answers with the same device key as pairing.
    get "/auth/methods", AuthController, :methods
    post "/auth/password", AuthController, :password
    post "/auth/magic_link", AuthController, :magic_link
    post "/auth/magic_link/exchange", AuthController, :magic_link_exchange

    post "/auth/oidc/exchange",
         NeiamAuth.App.ExchangePlug,
         [issue: {CartosnapApp.Accounts, :issue_app_token}],
         alias: false
  end

  scope "/api/v1", CartosnapAppWeb.Api.V1 do
    pipe_through :api_authenticated

    post "/auth/magic_link/check", AuthController, :magic_link_check

    get "/me", MeController, :show
    put "/device", MeController, :update_device
    delete "/device", MeController, :delete_device
    get "/trips", TripController, :index
    post "/photos", PhotoController, :create
  end

  # Other scopes may use custom stacks.
  # scope "/api", CartosnapAppWeb do
  #   pipe_through :api
  # end

  # Enable LiveDashboard and Swoosh mailbox preview in development
  if Application.compile_env(:cartosnap_app, :dev_routes) do
    # If you want to use the LiveDashboard in production, you should put
    # it behind authentication and allow only admins to access it.
    # If your application does not have an admins-only section yet,
    # you can use Plug.BasicAuth to set up some basic authentication
    # as long as you are also using SSL (which you should anyway).
    import Phoenix.LiveDashboard.Router

    scope "/dev" do
      pipe_through :browser

      live_dashboard "/dashboard", metrics: CartosnapAppWeb.Telemetry
      forward "/mailbox", Plug.Swoosh.MailboxPreview
    end
  end

  ## Authentication routes

  scope "/", CartosnapAppWeb do
    pipe_through [:browser, :require_authenticated_user]

    live_session :require_authenticated_user,
      on_mount: [{CartosnapAppWeb.UserAuth, :require_authenticated}] do
      live "/users/settings", UserLive.Settings, :edit
      live "/users/settings/confirm-email/:token", UserLive.Settings, :confirm_email

      # Everything here is one person's own photos and trips, so it all
      # needs a logged-in user (the contexts filter by @current_scope).
      live "/photos", PhotoLive.Index, :index
      live "/photos/:id", PhotoLive.Show, :show
      live "/map", MapLive, :index
      live "/upload", UploadLive, :index
      live "/trips", TripLive.Index, :index
      live "/trips/new", TripLive.Form, :new
      live "/users/invites", UserLive.Invites, :index
      live "/trips/:id", TripLive.Show, :show
      live "/trips/:id/edit", TripLive.Form, :edit
    end

    post "/users/update-password", UserSessionController, :update_password
  end

  scope "/", CartosnapAppWeb do
    pipe_through [:browser]

    # /auth/oidc/:provider/login, …/callback and /auth/oidc/health. 404
    # (but health) while OIDC_ENABLED is off.
    neiam_auth_routes()

    live_session :current_user,
      on_mount: [{CartosnapAppWeb.UserAuth, :mount_current_scope}] do
      live "/users/register", UserLive.Registration, :new
      live "/users/log-in", UserLive.Login, :new
      live "/users/log-in/:token", UserLive.Confirmation, :new

      # The front page: public trips on a map, for everyone.
      live "/", HomeLive, :index

      # An invite link. Open to the signed-out too: they're offered a new
      # account (addressed invites), or to log in or register and come back.
      live "/trips/join/:token", TripLive.Join, :show

      # A shared trip: the link is the permission, so no login is needed;
      # a signed-in owner is recognised as the host.
      live "/s/:token", ShareLive, :show

      # A public trip: anyone who comes across it, from the front page or
      # wherever. Separate from the share link, and its own room.
      live "/t/:id", ShareLive, :public
    end

    post "/users/log-in", UserSessionController, :create
    post "/trips/join/:token", InviteController, :join
    get "/trips/join/:token/log-in", InviteController, :log_in
    get "/trips/join/:token/register", InviteController, :register

    # NEIAM from an invite: remembered on the way out, so a first login
    # (refused as not invited) comes back to the invite to accept it.
    get "/trips/join/:token/sso", InviteController, :trip_sso
    post "/trips/join/:token/sso", InviteController, :join_sso

    # Registration invites: an addressed one makes its account here
    # (UserLive.Registration posts with phx-trigger-action), and NEIAM as
    # for trip invites.
    post "/users/register/invite", InviteController, :register_invited
    get "/users/register/invite/sso", InviteController, :registration_sso
    post "/users/register/invite/sso", InviteController, :register_sso
    delete "/users/log-out", UserSessionController, :delete
  end
end