Comments (6)
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.
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.
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.
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.
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.
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)
- Replacing Phoenix.Token with JWT-based for signing and verifiying of tokens HOT 1
- Unable to logout user in Pow API HOT 2
- Feature Request: Apple Passkeys HOT 1
- Pow.Plug.authenticate_user/1 performance HOT 2
- Reset password - test fails issue HOT 1
- Upgrading to Phoenix 1.7 HOT 5
- Create account without email/password using some unique ID?
- Phoenix 1.7 compilers warning HOT 1
- Maintain case when storing emails HOT 2
- Permissions issues with mnesia backend for Pow HOT 15
- Improve mix tasks
- Crash @missing_field? in Schema.ex HOT 1
- Inject values on registration HOT 2
- users_context seems to be ignored HOT 1
- Upgrading POW from 1.6 to 1.7 HOT 12
- Changeset errors into view HOT 1
- POW & LiveView - best way to implement `assigns.current_user` ? HOT 7
- Elixir 1.15 deprecation warnings HOT 1
- Persistence of stale session ids in MnesiaCache HOT 10
- Phoenix 1.6 App upgraded to 1.7 warning about layout conflicts. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pow.