lardcanoe

lardcanoe

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

Showing Posts 1 to 3

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? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews