I was wondering how I could implement having two pages in the same root "/" using Phoenix? One for unauthenticated users, and one for authenticated users. Examples of use cases where this happens are LinkedIn and Facebook, where a login-page is served on "/" and an application on "/" after logging in.
I use Guardian for authentication, and have my router set up as:
pipeline :auth do
plug Guardian.Plug.EnsureAuthenticated, handler: App.AuthHandler
end
pipeline :unauth do
plug Guardian.Plug.EnsureNotAuthenticated, handler: App.AuthHandler
end
scope "/", App.Web do
pipe_through [:browser, :unauth]
get "/", FrontController, :index
end
scope "/", App.Web do
pipe_through [:browser, :auth]
get "/page", ApplicationController, :index
end
Where FrontController serves the pages accessible by unauthenticated users (e.g. a login-page), and ApplicationController serves the actual application.
When ApplicationController serves "/" instead of "/page", a "this clause cannot match because a previous clause at line 1 always matches" error is thrown.
I imagine using if-statements and one controller for serving both pages, unfortunately I couldn't find documentation on how to implement such a strategy.