ashok
Different layout for logged in user based on role either admin or non-admin
I have installed Pow in Phoenix for user registration. I want to differentiate the URL and layout of my logged in users. If logged in as Admin I have created its role as admin while for normal logged in user it is User.
How can I provide the different layout to Admin user and Non-Admin users even I want to land them in different URLs lets for example for admin http://www.example.com/admin/ and for a non-admin user it will be like http://www.example.com/game.
I have read the route document of Phoenix at https://hexdocs.pm/phoenix/routing.html but not sure how to ensure that if it is admin then we need to use a new scope of admin. Can you provide me a code sample of it? Any URL which will help me on how to write code. I am beginner in this language.
Marked As Solved
danschultzer
You will need to add a plug restricting access like this: https://hexdocs.pm/pow/1.0.13/user_roles.html#add-role-plug
Also Liked
LostKobrakai
You could write a plug, which calls put_layout on the conn depending on the roles of the user stored on the conn.
danschultzer
put_layout is the right way to deal with custom templates for certain roles, but I think what you are asking for is how to redirect the user after sign in depending on the role.
You can customize the callback routes to redirect admin users: https://hexdocs.pm/pow/1.0.13/README.html#callback-routes
So you can do something like:
defmodule MyAppWeb.Pow.Routes do
use Pow.Phoenix.Routes
alias MyAppWeb.Router.Helpers, as: Routes
def after_sign_in_path(conn) do
user = Pow.Plug.current_user(conn)
case user.role do
"admin" -> Routes.admin_dashboard_path(conn, :index)
_any -> Routes.game_path(conn, :index)
end
end
This will redirect admin users to the admin dashboard instead of the game page. Remember to add routes_backend: MyAppWeb.Pow.Routes to the config.








