ashok

ashok

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 Routing — Phoenix v1.8.8 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.

Showing Posts 1 to 6

hauleth

hauleth

You need to use Phoenix.Controller.put_layout/2. So assuming that you have different scopes for admin and non admin:

pipeline :admin do
  plug :set_layout, "admin"
end

scope "/admin" do
  pipe_through [:admin]
  # …
end

defp set_layout(conn, layout) do
  Phoenix.Controller.put_layout(conn, layout)
end
ashok

ashok OP

Thanks for the reply but how can I detect at route level that logged in user is admin or non-admin based on user role either ‘admin’ or ‘non-admin-user’

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: Pow — Pow v1.0.13

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.

ashok

ashok OP

Thanks it works but has one issue any non-admin user if type /admin in the browser like https:///www.example.com/admin

it redirect that non-admin user to admin dashboard how can I prevent non-admin user to access admin user pages

danschultzer

danschultzer

Pow Core Team

You will need to add a plug restricting access like this: How to add user roles — Pow v1.0.13

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews