How to Use DB/Redis based Sessions with AshAuthentication Instead of JWTs?
Hi everyone! ๐ I just purchased the Ash Framework book from pragstudio.com to learn more about the framework, as I'm hoping to port a legacy Rails app over to Elixir/Phoenix/Ash.
When I reached the authentication section, I noticed that it only covers JWT-based sessions for the two chapters on authentication and authorization and there is no mention of database-stored / cookie-based sessions.
For my use case, I'd prefer traditional server-side sessions (ideally stored in Redis). Could someone guide me on how I might implement this with AshAuthentication? Any examples, docs, or best practices would be greatly appreciated! ๐
12 Replies
The JWT with AshAuthentication is only used to identify the user and for you to add additional claims if you want
You can use Phoenix's standard session logic, orthogonally to how your user is authenticated
I'm running into an issue and have documented everything in a Markdown file. For some reason, I can't paste it directly into this Discord chat. Is there a reference on how to post console output and code snippets with syntax highlighting?
๐ค you should be able to just copy/paste it essentially
is it just too much content?
Otherwise, http://gist.github.com would probably do well enough.
'''markdown
<your stuff>
'''
replace the quotes with backticks
is the syntax for sytnax highlighting
Looks like gist is the best method. Here you go: https://gist.github.com/pupdogg/ee45f4ae3020701bb292096a6efa22b2
Hmm......
Try copying the source of the hash password change from AshAuthentication into your own change
You can then see whats going wrong hashing the password
Ok, let me track that down
The
Assumption failed: Error hashing password
error occurs because:
1. Root Cause: AshAuthentication.Info.find_strategy/3
returns :error
when called from HashPasswordChange
2. Missing Context: The create_with_password
action doesn't automatically map to the :password strategy
3. Bcrypt
Works Fine: Direct Bcrypt.hash_pwd_salt/1
and AshAuthentication.BcryptProvider.hash/1
both work perfectly
Immediate Fix
Add explicit strategy context to changeset:
|> Ash.Changeset.set_context(%{strategy_name: :password})
Working seed:
๐ค ah, you can put that in your action
change set_context(%{strategy_name: :password})
Can you open an issue for us to improve that error message?Which repo should I create it under?
ash-project/ash
or ash-authentication
GitHub
Confusing error message: "Assumption failed: Error hashing password...
Summary The HashPasswordChange produces a misleading error message "Assumption failed: Error hashing password" when the actual issue is that the strategy cannot be found in the changeset ...
@bigtomcallahan I like your proposed fix BTW if you just want to open a PR with that change
You got it, thank you! ๐
GitHub
Update hash_password_change.ex by pupdogg ยท Pull Request #1058 ยท ...
Updated change method in AshAuthentication.Strategy.Password.HashPasswordChange per issue #1057