ashok

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

danschultzer

Pow Core Team

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

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

danschultzer

Pow Core Team

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.

Where Next?

Popular in Questions Top

Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
vegabook
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53578 245
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement