lardcanoe

lardcanoe

Customizing ash_authentication for dummies (like me)

I’ve been needing to customize our sign in, register, reset password, and forgot password pages for months. Every time I took a look at the ash_authentication_phoenix pages/code I ran away crying. And blog posts like Customising Ash Authentication with Phoenix LiveView did not make it seem easier.

After taking the plunge last night (due to yet another confused customer), I hooked up a custom Forgot Password page… and it all clicked.

We’re just doing POST calls to a few auth endpoints. Duh!

mix phx.routes

auth_path  *     /auth/user/password/register           MyAppWeb.AuthController {:user, :password, :register}
auth_path  *     /auth/user/password/sign_in            MyAppWeb.AuthController {:user, :password, :sign_in}
auth_path  *     /auth/user/password/reset_request      MyAppWeb.AuthController {:user, :password, :reset_request}
auth_path  *     /auth/user/password/reset              MyAppWeb.AuthController {:user, :password, :reset}

With that in mind, everything else made sense. The blogs, the docs, the other posts in this forum.

In iex, run AshAuthentication.Info.strategy!(MyApp.Accounts.User, :password) and things get even more clear, because so much of what seems like cryptic code was just there because literally every thing about ash_auth can be customized. So there is a ton of code to just deal with that. Especially since you can have numerous strategies enabled, and they deal with that. But I have just password and Google, so I can get really simple.

%AshAuthentication.Strategy.Password{
  confirmation_required?: true,
  hash_provider: AshAuthentication.BcryptProvider,
  hashed_password_field: :hashed_password,
  identity_field: :email,
  name: :password,
  password_confirmation_field: :password_confirmation,
  password_field: :password,
  provider: :password,
  register_action_accept: [],
  register_action_name: :register_with_password,
  registration_enabled?: true,
  resettable: %AshAuthentication.Strategy.Password.Resettable{
    token_lifetime: {3, :days},
    request_password_reset_action_name: :request_password_reset_with_password,
    password_reset_action_name: :password_reset_with_password,
    sender: {MyApp.Mailer.SendPasswordResetEmail, []}
  },
  resource: MyApp.Accounts.User,
  sign_in_action_name: :sign_in_with_password,
  sign_in_enabled?: true,
  sign_in_token_lifetime: {60, :seconds},
  sign_in_tokens_enabled?: false,
  sign_in_with_token_action_name: :sign_in_with_token_for_password,
  strategy_module: AshAuthentication.Strategy.Password
}

So when I see something similar to:

    api = AshAuthentication.Info.authentication_domain!(strategy.resource)
    subject_name = AshAuthentication.Info.authentication_subject_name!(strategy.resource)

    AshPhoenix.Form.for_action(strategy.resource, strategy.resettable.request_password_reset_action_name,
      api: api,
      as: to_string(subject_name),
      context: Ash.Helpers.deep_merge_maps(%{}, %{strategy: strategy, private: %{ash_authentication?: true}})
    )

I now realize those were (obviously) just normal forms that I’ve been working with in Ash for over a year. Again, duh!

Heck, you don’t even need any of that. You can make a basic sign in page with static html, similar to:

<form action="/auth/user/password/sign_in" method="POST">
  <input name="user[email]"/>
  <input type="password" name="user[password]"/>
  <button type="submit">Sign In</button>
</form>

(I’m just typing that out, it’s not real, missing CSRF for instance)

So don’t be afraid to write your custom auth pages. You can start with simple HTML!

I have no questions. Just hoping this post saves someone else a few hours.

My router.ex looks like:

    live_session :auth_sign_in do
      live "/sign-in", Live.Auth.SignIn, :sign_in
      live "/register", Live.Auth.Register, :register
      live "/reset", Live.Auth.ForgotPassword, :reset
    end

    auth_routes_for MyApp.Accounts.User, to: AuthController

    scope "/password-reset" do
      live_session :auth_reset do
        live "/:token", Live.Auth.ResetPassword, :reset
      end
    end

Some pages for reference:

https://github.com/team-alembic/ash_authentication/issues/497

First 3 of 3 Posts Switch mode

zachdaniel

zachdaniel

Creator of Ash

Love this! Thank you :heart:. In the near future we hope to have a basic generator to get people started with fully custom auth pages as well :slight_smile: Need to finish the fancy new ash authentication installers first though. But those are nearing the finish line.

tyfrankiv

tyfrankiv

helped a lot, thanks pal

parables

parables

Thank you for pointing out this.

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement