In some cases, you want to turn off the possibility to signup and create an account. Most likely, you want to have the page live during development. In this tutorial I will show you how to turn off Signups and the admin section in production only so you can build out your application and not worrying about exposing not finished sections to users.
In this post. I will go through how Im doing it on a new application based on the SAAS Starter Kit that I am building. You can check it out on: https://liveview.build
The best place to put this kind of logic, is in the configuration files. The idea is that this should only concern production, so first step is to disable it in theprod.exs
file. There will be two new config variables. One concerns admin and the other concerns the app. And with app, I mean access to the app that is being built. So, open up the prod config and add:
# prod.exs
config :screen_builder,
admin_enabled: false,
app_enabled: false
Since this is for production only, they needs to be added in the config file and enabled by default. In the SAAS Starter Kit,. there are already some configuration about the app inconfig.exs
. Add the same config variables and make sure they are enabled.
# config.exs
config :screen_builder,
admin_enabled: true,
app_enabled: true
This means that everything should work as normal on tests and development.
Open up the routes file and go down to the routes that handles the user registration and sessions. Wrap the entire block in the Application.compile_env(:screen_builder, :app_enabled)
condition:
# router.ex
if Application.compile_env(:screen_builder, :app_enabled) do
scope "/", ScreenBuilderWeb do
pipe_through [:browser, :redirect_if_user_is_authenticated, :session_layout]
get "/users/register", UserRegistrationController, :new
...
end
scope "/", ScreenBuilderWeb do
pipe_through [:browser, :require_authenticated_user]
get "/users/settings", UserSettingsController, :edit
...
end
end
And turn off the admin routes by wrapping them in the condition:
# router.ex
if Application.compile_env(:screen_builder, :admin_enabled) do
scope "/admin", ScreenBuilderWeb.Admin, as: :admin do
pipe_through [:browser, :admin_session_layout]
get "/sign_in", SessionController, :new
...
end
scope "/admin", ScreenBuilderWeb.Admin, as: :admin do
pipe_through [:browser, :require_current_admin, :admin_layout]
live "/", DashboardLive.Index, :index
...
end
end
With the routes disabled in the routes file, you also need to remove them from the front page and root layout. You could either wrap them in the same kind of condition or remove them as a whole.
Last step here is to deploy and make sure that the configuration works.