Giter Site home page Giter Site logo

Comments (6)

danschultzer avatar danschultzer commented on May 3, 2024 2

This is a great use case for Pow 😄 I'll test it out to see what tweaks are necessary, but I think that it's almost doable. The user sessions themselves are isolated.

But if Pow.Plug.Session is called multiple times in the same scope, it'll only fetch the last plug configuration in the controllers. It should be possible to fix if I can pass namespace configuration to routes.

I'll get back to this later when I've got a chance to work on it.

from pow.

danschultzer avatar danschultzer commented on May 3, 2024 1

I'm working on a PR to make this possible right now. It will work like this:


Set up Pow to handle multiple user structs in same scope

Let's say that you would like to access both a user and admin session in the same scope.

Pow has built-in isolation with :namespace configuration, so this will be simple. Note that the same can be done with less configuration in an umbrella setup using the :otp_app configuration.

Create admin and user schema

mix pow.ecto.install -r MyApp.Repo Admin admins
mix pow.ecto.install -r MyApp.Repo User users

Set up plugs

Update WEB_PATH/endpoint.ex with the two namespaced configurations:

plug Pow.Plug.Session,
  repo: MyApp.Repo,
  user: MyApp.User,
  namespace: :user

plug Pow.Plug.Session,
  repo: MyApp.Repo,
  user: MyApp.Admin,
  namespace: :admin

The user can be accessed in assigns[:current_user][:user] and assigns[:current_user][:admin], and the configurations can be accessed in conn.private[:pow_config][:user] and conn.private[:pow_config][:admin].

Set up routes

Update WEB_PATH/router.ex to access the namespaced configurations:

scope "/user", private: %{pow_config_namespace: :user} do
  pipe_through :browser

  pow_routes()
end

scope "/admin", private: %{pow_config_namespace: :admin} do
  pipe_through :browser

  pow_routes()
end

That's it! Your namespaced configuration is now shared with the controller.

Modify templates

If you wish to modify the templates, you'll need to generate run the generate mix task with namespace argument:

mix pow.phoenix.gen.templates --namespace user
mix pow.phoenix.gen.templates --namespace admin

Remember to set up both configurations with web_module: MyAppWeb. The namespace is automatically used from the configuration to discover the templates and views.

from pow.

danschultzer avatar danschultzer commented on May 3, 2024 1

I've closed #24 as I didn't feel it was the right approach. I think this is better left to the developer as there's several things to consider in how the contexts are separated. Pow has been refactored quite a lot and with #56, it's now possible to handle several contexts with routes too. This was the last issue that bugged me. Most of the work will just be in figuring out how the pipelines in the router should be set up.

I'll prepare for a 1.0.0 release first, but eventually I'll look into a guide explaining how to handle multiple contexts in same connection with Pow 😄

from pow.

danschultzer avatar danschultzer commented on May 3, 2024

Yeah, Pow is built to be very flexible in this regard. The configuration happens in the plug:

plug Pow.Plug.Session,
  repo: MyApp.Repo,
  user: MyApp.User,
  current_user_assigns_key: :current_user,
  session_key: "auth",
  session_store: {Pow.Store.CredentialsCache,
                  ttl: :timer.minutes(30),
                  namespace: "credentials"},
  session_ttl_renewal: :timer.minutes(15),
  cache_store_backend: Pow.Store.Backend.EtsCache,
  users_context: Pow.Ecto.Users

So you should be able to set it up with scopes.

I would love to see a tutorial or demo for how it works in Devise so I can confirm that Pow can do the same thing. Is this what you want? https://github.com/plataformatec/devise/wiki/How-to-Setup-Multiple-Devise-User-Models

from pow.

chevinbrown avatar chevinbrown commented on May 3, 2024

That's basically it (for devise).
For my needs devise is perfect b/c I don't deal with scoping/authorization. I can use the devise scope current_admin, current_buyer, current_seller respectively, and it pretty well isolates the concerns of those users. 😃

from pow.

danschultzer avatar danschultzer commented on May 3, 2024

Sorry for the delay, other work took my time. I've created a PR #24.

You can read the guide here: https://github.com/danschultzer/pow/blob/e725d04b2a9e91565a1c7c1075411e0ae2cec792/guides/MULTIPLE_USERS_IN_SAME_SCOPE.md

from pow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.