redirect screen while trying to sign-in with wrong email or password

Why does this screen comes up instead of displaying an error message that the credentials are not right.
No description
36 Replies
ZachDaniel
ZachDaniel3y ago
🤔 Is your form set up to submit the action? set up to submit the action over a controller route, I mean At the moment, the behavior is to submit over HTTP to log you in, which means that you'd need to, in your AuthController, redirect on failure This is what ash_hq does
def failure(
conn,
{:password, :sign_in},
%AshAuthentication.Errors.AuthenticationFailed{}
) do
conn
|> put_flash(
:error,
"Username or password is incorrect"
)
|> redirect(to: "/sign-in")
end

def failure(conn, activity, reason) do
stacktrace =
case reason do
%{stacktrace: %{stacktrace: stacktrace}} -> stacktrace
_ -> nil
end

Logger.error("""
Something went wrong in authentication

activity: #{inspect(activity)}

reason: #{Exception.format(:error, reason, stacktrace || [])}
""")

conn
|> put_flash(
:error,
"Something went wrong"
)
|> redirect(to: "/sign-in")
end
def failure(
conn,
{:password, :sign_in},
%AshAuthentication.Errors.AuthenticationFailed{}
) do
conn
|> put_flash(
:error,
"Username or password is incorrect"
)
|> redirect(to: "/sign-in")
end

def failure(conn, activity, reason) do
stacktrace =
case reason do
%{stacktrace: %{stacktrace: stacktrace}} -> stacktrace
_ -> nil
end

Logger.error("""
Something went wrong in authentication

activity: #{inspect(activity)}

reason: #{Exception.format(:error, reason, stacktrace || [])}
""")

conn
|> put_flash(
:error,
"Something went wrong"
)
|> redirect(to: "/sign-in")
end
talha-azeem
talha-azeemOP3y ago
<.simple_form
:let={form}
id="login_form"
for={@form}
action={
route_helpers(@socket).auth_path(
@socket.endpoint,
{@subject_name, :password, :sign_in}
)
}
method="post"
phx-submit="save"
phx-trigger-action={@trigger_action}
phx-change="validate"
>
<.simple_form
:let={form}
id="login_form"
for={@form}
action={
route_helpers(@socket).auth_path(
@socket.endpoint,
{@subject_name, :password, :sign_in}
)
}
method="post"
phx-submit="save"
phx-trigger-action={@trigger_action}
phx-change="validate"
>
these are the current settings of the login form now it redirects to the same page but doesn't show the error message 😅
ZachDaniel
ZachDaniel3y ago
So you need to add some code to your page/layout to render flash messages There is a guide for that in phoenix/phoenix liveview I'm sure 🙂
talha-azeem
talha-azeemOP3y ago
Yeah i know. I already have the flash messages setup Thanks
ZachDaniel
ZachDaniel3y ago
🤔 so its not showing the flash messages you're setting?
talha-azeem
talha-azeemOP3y ago
It started working itself now. Thank you @Zach Daniel ❤️
ZachDaniel
ZachDaniel3y ago
👍
talha-azeem
talha-azeemOP3y ago
@Zach Daniel can you tell me one thing? I was looking into the auth forms and there is one reset_form as well. but there is not link or route to access that
ZachDaniel
ZachDaniel3y ago
Correct, it switches not on a route It does it using liveview state there is a discussion about that here: https://discord.com/channels/711271361523351632/1068488089514811432 If you want to do it with a separate route you can 🙂
talha-azeem
talha-azeemOP3y ago
reset password?
ZachDaniel
ZachDaniel3y ago
Yeah, works the same way You can serve it over a different route if you want, just make another liveview for it
talha-azeem
talha-azeemOP3y ago
I thought that reset_form is for resetting the password
ZachDaniel
ZachDaniel3y ago
yes, it is Its for requesting a forgotten password
talha-azeem
talha-azeemOP3y ago
then how can i access it? 😅
ZachDaniel
ZachDaniel3y ago
like requesting an email with a password reset link You're writing your own liveview... So you can access it however you want. I'm not sure I understand.
talha-azeem
talha-azeemOP3y ago
yes i used my own liveviews for registeration and login
ZachDaniel
ZachDaniel3y ago
You'll need to write your own liveview for password reset too
talha-azeem
talha-azeemOP3y ago
My concern was what strategy is being used there like i can access the default sign-in page through localhost:4000/sign-in. I was wondering if there is any way to access that one as well
ZachDaniel
ZachDaniel3y ago
There is no route for password reset currently. I think that will change because of issues like the one I linked where people want to link directly to those pages but for now, you can't link to something like /forgot-password Because it doesn't have its own route But thats with the default built in stuff if you're writing your own liveview, you can do :magic_sparkles: whatever you want
talha-azeem
talha-azeemOP3y ago
Noted. Can you tell me what strategy is being used there? 😅 so i can implement that in my own liveviews?
ZachDaniel
ZachDaniel3y ago
What do you mean what strategy?
talha-azeem
talha-azeemOP3y ago
like sign-in page have this strategy:
%AshAuthentication.Strategy.Password{
identity_field: :email,
hashed_password_field: :hashed_password,
hash_provider: AshAuthentication.BcryptProvider,
confirmation_required?: true,
password_field: :password,
password_confirmation_field: :password_confirmation,
register_action_name: :register_with_password,
sign_in_action_name: :sign_in_with_password,
resettable: [],
name: :password,
provider: :password,
resource: DataDrivenMicroscopy.Accounts.User
}
%AshAuthentication.Strategy.Password{
identity_field: :email,
hashed_password_field: :hashed_password,
hash_provider: AshAuthentication.BcryptProvider,
confirmation_required?: true,
password_field: :password,
password_confirmation_field: :password_confirmation,
register_action_name: :register_with_password,
sign_in_action_name: :sign_in_with_password,
resettable: [],
name: :password,
provider: :password,
resource: DataDrivenMicroscopy.Accounts.User
}
ZachDaniel
ZachDaniel3y ago
Thats the same strategy I mean: that is the strategy for all of the forms around passwords I didn't realize you meant the actual AshAuthentication.Strategy Have you configured your strategy to be resettable?
talha-azeem
talha-azeemOP3y ago
but here the resettable is empty list no? 😅 how can i do that?
ZachDaniel
ZachDaniel3y ago
password :password do
identity_field :email
hashed_password_field :hashed_password

resettable do
sender AshHq.Accounts.User.Senders.SendPasswordResetEmail
end
end
password :password do
identity_field :email
hashed_password_field :hashed_password

resettable do
sender AshHq.Accounts.User.Senders.SendPasswordResetEmail
end
end
thats what I have in ash_hq Take a look at the docs and search for resettable
talha-azeem
talha-azeemOP3y ago
i did search and found nothing
frankdugan3
frankdugan33y ago
Ooh, that's a cool command! 😄
ZachDaniel
ZachDaniel3y ago
🙂 I need to make a private version of it so it will respond not to everyone in the chat
talha-azeem
talha-azeemOP3y ago
I was about to say that @frankdugan3
ZachDaniel
ZachDaniel3y ago
We're going to be making the DSL docs much better soon but anyway, you can configure it the way I've shown
talha-azeem
talha-azeemOP3y ago
AshHq.Accounts.User.Senders.SendPasswordResetEmail This is another module using swoosh or something to send email?
ZachDaniel
ZachDaniel3y ago
GitHub
GitHub - ash-project/ash_hq: The Ash Framework homepage and documen...
The Ash Framework homepage and documentation site. - GitHub - ash-project/ash_hq: The Ash Framework homepage and documentation site.
ZachDaniel
ZachDaniel3y ago
You can see all of the source code of ash_hq there So you can take a look at it and see what it does 🙂
talha-azeem
talha-azeemOP3y ago
Noted with Thanks

Did you find this page helpful?