tegmentum
I followed the tutorial to setup Ash Authentication.
I have the following:
router.ex
...
scope "/", WfdBaseWeb do
pipe_through :browser
get "/", PageController, :home
sign_in_route(on_mount: [{WfdBaseWeb.LiveUserAuth, :live_no_user}])
sign_out_route AuthController
auth_routes_for WfdBase.Accounts.User, to: AuthController
reset_route []
ash_authentication_live_session :authentication_optional,
on_mount: {WfdBaseWeb.LiveUserAuth, :live_user_optional} do
live "/app", App
end
...
end
The helper for liveview:
defmodule WfdBaseWeb.LiveUserAuth do
@moduledoc """
Helpers for authenticating users in LiveViews.
"""
import Phoenix.Component
use WfdBaseWeb, :verified_routes
def on_mount(:live_user_optional, _params, _session, socket) do
if socket.assigns[:current_user] do
{:cont, socket}
else
{:cont, assign(socket, :current_user, nil)}
end
end
def on_mount(:live_user_required, _params, _session, socket) do
if socket.assigns[:current_user] do
{:cont, socket}
else
{:halt, Phoenix.LiveView.redirect(socket, to: ~p"/sign-in")}
end
end
def on_mount(:live_no_user, _params, _session, socket) do
if socket.assigns[:current_user] do
{:halt, Phoenix.LiveView.redirect(socket, to: ~p"/")}
else
{:cont, assign(socket, :current_user, nil)}
end
end
end
My controller:
defmodule WfdBaseWeb.AuthController do
use WfdBaseWeb, :controller
use AshAuthentication.Phoenix.Controller
def success(conn, _activity, user, _token) do
return_to = get_session(conn, :return_to) || ~p"/app"
conn
|> delete_session(:return_to)
|> store_in_session(user)
|> assign(:current_user, user)
|> redirect(to: return_to)
end
def failure(conn, _activity, _reason) do
conn
|> put_status(401)
|> render("failure.html")
end
def sign_out(conn, _params) do
return_to = get_session(conn, :return_to) || ~p"/"
conn
|> clear_session()
|> redirect(to: return_to)
end
end
And my failure markup file at lib/wfd_base_web/controllers/auth_html/failure.html.heex
Signing in with a existing user and correct credentials works fine and redirects to /app as I expect.
The problem is that when I submit wrong credentials, I still get redirected to /app, which is not right, I expect a redirect to failure.html
Looking at /routes, there is no route for the failure page, is that expected ? how is this usually handled ?
Is there documentation on how to do this ?
Trending in Questions
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
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
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
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
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
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
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
You shouldn’t need a route for that, no. Although TBH you typically don’t want to use that
failure.htmlpattern, you’d typically redirect back with a flash message.Can you see if the
failure/3callback orsuccess/4callback is being invoked when you use the incorrect credentials? When you say it redirects to/appis it redirecting you with a logged in user? with no logged in user?tegmentum
Hyey Zach, thanks for getting back to me!
The
failure/3gets called actually.this is the output from inspecting the
connSo the user is actually not found, and i’m redirected wit no user …
zachdaniel
I’m confused why a redirect would be happening. If you’re getting to that failure handler, it shouldn’t redirect at all.
tegmentum
Maybe ‘redirect’ is the wrong word.
The url becomes:
/auth/user/password/sign_in,but the template for
app.html.heexis what i see in the browser.zachdaniel
Hmmmmm…that sounds pretty strange. Not sure what’s up. Ultimately what the failure handler does is up to you though. Here is one from one of my apps.
tegmentum
That helps a lot, thank you for this.
I would say that should be in the docs somewhere, just to communicate it to readers, and open tell them where Ash’s involvement stops. I was really expecting some “magic” to happen. I wouldn’t mind opening a PR to update the docs (if that’s a thing).
Thanks again!
zachdaniel
Definitely open to it! I’d actually like to change the docs to remove the failure.html concept and instead show basically what I pasted to you
kurmetaubanov
hi @zachdaniel
i was having almost the same issue when sign-in tokens were enabled, sign-in form never called failure callback, it just rendered sign in form again without any failure messages
to draw a failure message I had to disable sign-in tokens for now.
What is interesting, can we keep identity_name filled on failure? on redirect to /sign-in it is cleared
zachdaniel
So the sign in tokens allow for a “refresh-less” LV experience, but typically there would be logs for unhandled errors in forms. Do you see a log message telling you about unhandled errors?
kurmetaubanov
No, last thing I see is Sign In Form handles “submit” event when tokens signin are enabled and no error log. Zach, could you point me where I can add put_flash error message on refresh-less liveview sign-in experience please


