key :current_user not found error in ash_authentication_phoenix tutorial sample home.html.heex

Hello, I have progressed with implementing https://ash-hq.org/docs/guides/ash_authentication_phoenix/latest/getting-started-with-ash-authentication-phoenix but unfortunately I get a key :current_user not found error when starting the server as in https://ash-hq.org/docs/guides/ash_authentication_phoenix/latest/getting-started-with-ash-authentication-phoenix#start-phoenix I have double checked the generated routes, the contents of the AuthController and the associated auth_html.ex and failure.html.heex. I can create a new user through localhost:4000/sign-in but then after I still get the same error. It seems the current_user simply does not get assigned. How can I debug this?
Ash HQ
Guide: Getting Started With Ash Authentication Phoenix
Read the "Getting Started With Ash Authentication Phoenix" guide on Ash HQ
16 Replies
ZachDaniel
ZachDaniel3y ago
What is the stacktrace of the error you get?
nkr
nkr3y ago
I had the same erro today I changed it to if assigns[:current_user] to make it work But I’m still wondering if I needed to do that
siros123
siros123OP3y ago
@Zach Daniel here is the stacktrace as requested
ZachDaniel
ZachDaniel3y ago
Yeah, I think if assigns.current_user doesn’t make much sense because if a user isn’t logged in the assign won’t be populated
siros123
siros123OP3y ago
@Zach Daniel the tutorial's suggested home.html.heex template says if @current_user , should we submit a pull request to change that to if assigns[:current_user] as per @nkr ? That does seem to work for me too. FYI the if @current_user version did not work whether there was an authenticated user or not
ZachDaniel
ZachDaniel3y ago
Hmm…I think that’s not the right call We should add an assign_new(:current_user, fn -> nil end) At the top of a liveview that may have a non logged in user
nkr
nkr3y ago
its the pagecontroller actually in this case
ZachDaniel
ZachDaniel3y ago
Hmm… Does it happen when a user should be logged in? If it only happens when a user is not logged in that makes sense
nkr
nkr3y ago
Yea not logged but according to the tutorial it would show a sign in button
ZachDaniel
ZachDaniel3y ago
I’ll be around on Sunday/Monday to explore more, but it does sound like there is an issue with the tutorial. With the fix of using [:current_user] is the behavior generally correct or is there still something missing?
nkr
nkr3y ago
I was missing adding plug :load_from_session to roujter router Im testing but Im sure this is the problem lol At least in my case Yes I can confirm that was the problem. It's not a tutorial problem
siros123
siros123OP3y ago
@nkr thanks for identifying the error, I missed the same line! @Zach Daniel confirmed that there is no tutorial problem, my bad! Sorry to waste you guys' time on this 😦
axdc
axdc3y ago
I have plug :load_from_session in my router and I'm getting this error when accessing the example_live_view from the phoenix tutorial. (But not from the home page). Am I perhaps not assigning something correctly in the liveview? found https://hexdocs.pm/phoenix_live_view/security-model.html#mounting-considerations pardon my initial confusion. Is this info relevant/recommended for working with Ash?
ZachDaniel
ZachDaniel3y ago
Are you using ash_authentication_live_session around your liveview routes?
axdc
axdc3y ago
I don't think so. Reading about that now. I grok the live auth flow now after reading through the ash_hq source, thank you 😅
Dinner Roll
Dinner Roll3y ago
Hit the same error and couldn't see anything on the /sign-in page either. Problem was that I forgot to add my Accounts API to ash_apis: in config/config.exs

Did you find this page helpful?