Ash Authentication in Phoenix Liveviews

I am trying to implement the authentication in liveviews. can anyone provide me a helpful link?
33 Replies
minib
minib3y ago
GitHub
ash_authentication_phoenix/dev at main · team-alembic/ash_authentic...
Drop-in authentication support for Phoenix apps using AshAuthentication. - ash_authentication_phoenix/dev at main · team-alembic/ash_authentication_phoenix
minib
minib3y ago
GitHub
GitHub - axelbdt/ash_authentication_phoenix_example: The simplest p...
The simplest password authentication with Ash Framework, Ash Authentication and Phoenix. - GitHub - axelbdt/ash_authentication_phoenix_example: The simplest password authentication with Ash Framewo...
talha-azeem
talha-azeemOP3y ago
i have followed the second one but unfortunately when i hit this path http://localhost:4000/auth/user/password/register it gives me the below error no "failure" html template defined for DummyWeb.AuthHTML
talha-azeem
talha-azeemOP3y ago
@Zach Daniel i am having this page while hitting the /sign-in route. There are no fields here. What could be the issue?
No description
ZachDaniel
ZachDaniel3y ago
What does the authentication section look like on your user?
talha-azeem
talha-azeemOP3y ago
scope "/" do pipe_through :browser sign_in_route sign_out_route DummyWeb.AuthController auth_routes_for(Dummy.Accounts.User, to: DummyWeb.AuthController) end If you are asking about the scope in router.ex
ZachDaniel
ZachDaniel3y ago
No, want to see the Dummy.Accounts.User Assuming you’ve configured it for authentication
talha-azeem
talha-azeemOP3y ago
defmodule Dummy.Accounts.User do use Ash.Resource, data_layer: AshPostgres.DataLayer, extensions: [AshAuthentication] attributes do uuid_primary_key :id attribute :name, :string, allow_nil?: false attribute :email, :ci_string, allow_nil?: false attribute :hashed_password, :string, allow_nil?: false, sensitive?: true end authentication do api Dummy.Accounts strategies do password :password do identity_field :email end end tokens do enabled? true token_resource Dummy.Accounts.Token signing_secret fn _, _ -> Application.fetch_env(:dummy, :token_signing_secret) end end end postgres do table "users" repo Dummy.Repo end identities do identity :unique_email, [:email] end end I found this in documentation
ZachDaniel
ZachDaniel3y ago
For code snippets, please surround the text with triple backticks and the word “elixir” for syntax highlighting. For example: ‘’’elixir code ‘’’ But with backticks instead of quotes. Do you have a config :ash_apis, […] list? It should be in your root config.exs ``` <- this character
talha-azeem
talha-azeemOP3y ago
defmodule Dummy.Accounts.User do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [AshAuthentication]

attributes do
uuid_primary_key :id
attribute :name, :string, allow_nil?: false
attribute :email, :ci_string, allow_nil?: false
attribute :hashed_password, :string, allow_nil?: false, sensitive?: true
end

authentication do
api Dummy.Accounts

strategies do
password :password do
identity_field :email
end
end

tokens do
enabled? true
token_resource Dummy.Accounts.Token
signing_secret fn _, _ ->
Application.fetch_env(:dummy, :token_signing_secret)
end
end
end

postgres do
table "users"
repo Dummy.Repo
end

identities do
identity :unique_email, [:email]
end
end
defmodule Dummy.Accounts.User do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [AshAuthentication]

attributes do
uuid_primary_key :id
attribute :name, :string, allow_nil?: false
attribute :email, :ci_string, allow_nil?: false
attribute :hashed_password, :string, allow_nil?: false, sensitive?: true
end

authentication do
api Dummy.Accounts

strategies do
password :password do
identity_field :email
end
end

tokens do
enabled? true
token_resource Dummy.Accounts.Token
signing_secret fn _, _ ->
Application.fetch_env(:dummy, :token_signing_secret)
end
end
end

postgres do
table "users"
repo Dummy.Repo
end

identities do
identity :unique_email, [:email]
end
end
ZachDaniel
ZachDaniel3y ago
If you do ```elixir on the first line it will be syntax highlighted
ZachDaniel
ZachDaniel3y ago
Foo.bar()
Foo.bar()
No description
talha-azeem
talha-azeemOP3y ago
No i didn't do this and it wasn't mentioned in documentation as well 😢
ZachDaniel
ZachDaniel3y ago
Have you gone through the initial ash getting started guide? It is assumed for the ash authentication guide that you are familiar with the basics of ash itself
ZachDaniel
ZachDaniel3y ago
Actually, that isn’t in that guide either I’ll add this to the getting started guide and to the ash authentication guides
talha-azeem
talha-azeemOP3y ago
UndefinedFunctionError at GET /sign-in
function DummyWeb.Router.Helpers.auth_path/2 is undefined (module DummyWeb.Router.Helpers is not available)
UndefinedFunctionError at GET /sign-in
function DummyWeb.Router.Helpers.auth_path/2 is undefined (module DummyWeb.Router.Helpers is not available)
ZachDaniel
ZachDaniel3y ago
in your_app_web.ex you need to change helpers: false to helpers: true
talha-azeem
talha-azeemOP3y ago
yes just did. Thank you so much I will let you know if there is any issue 😄
ZachDaniel
ZachDaniel3y ago
👍 if the current issue has been resolved please add the solved tag and then close the thread by right clicking in the sidebar and closing it 🙇
talha-azeem
talha-azeemOP3y ago
Noted
ZachDaniel
ZachDaniel3y ago
Documentation has been updated, and will reflect on ash_hq with the next release. Thanks 🙂
talha-azeem
talha-azeemOP3y ago
@Zach Daniel I am unable to register. It says that
Missing JWT signing secret. Please see the documentation for `AshAuthentication.Jwt` for details
Missing JWT signing secret. Please see the documentation for `AshAuthentication.Jwt` for details
How can i generate token_signing_secret? if i disable this in Dummy.Accounts.User
tokens do
enabled? true
token_resource Dummy.Accounts.Token
signing_secret fn _, _ ->
Application.fetch_env(:dummy, :token_signing_secret)
end
end
tokens do
enabled? true
token_resource Dummy.Accounts.Token
signing_secret fn _, _ ->
Application.fetch_env(:dummy, :token_signing_secret)
end
end
then it works fine. Is it necessary? What is the purpose of this?
ZachDaniel
ZachDaniel3y ago
This stores authentication tokens in the database, which, depending on your setup, you may use for additional features. If you just want log out and log in, you don’t really need it. If you want token revocation, or to have all active sessions tracked in a database, or if you want to send confirmation emails, then you need the token resource set up
talha-azeem
talha-azeemOP3y ago
Okay then i need it. How can i generate the token_signing_secret? I really appreciate that you are helping me out ❤️
ZachDaniel
ZachDaniel3y ago
You can make a secret value with mix phx.gen.secret 32 And then you configure it like any other elixir config config :dummy, :token, signing_secret: your_secret You should not check that into your repo
talha-azeem
talha-azeemOP3y ago
Noted.
ZachDaniel
ZachDaniel3y ago
You should use runtime.exs and environment variables
talha-azeem
talha-azeemOP3y ago
One more thing that if i want a route to be accessed only is the user is authenticated. How can i define that to ash_auth?
ZachDaniel
ZachDaniel3y ago
You’d do that yourself with phoenix tools You can see how we do it with live views in ash_hq So you’d use a plug or a live session on_mount hook
talha-azeem
talha-azeemOP3y ago
@Zach Daniel I am getting this error now
Couldn't recognize the signer algorithm.
Possible values are:

["HS256", "HS384", "HS512", "RS256", "RS384", "RS512", "ES256", "ES384", "ES512", "PS256", "PS384", "PS512", "Ed25519", "Ed25519ph", "Ed448", "Ed448ph"]
Couldn't recognize the signer algorithm.
Possible values are:

["HS256", "HS384", "HS512", "RS256", "RS384", "RS512", "ES256", "ES384", "ES512", "PS256", "PS384", "PS512", "Ed25519", "Ed25519ph", "Ed448", "Ed448ph"]
ZachDaniel
ZachDaniel3y ago
…could you open a new discussion for that? That one is likely one for @jart
talha-azeem
talha-azeemOP3y ago
sure

Did you find this page helpful?