Support phx.gen.auth in backend applications

Hi, everyone!

Sometimes, you might want to build your app using Phoenix just for the backend and use another tool for the frontend (such as React or Vue). In this case, you would start your project with the --no-html flag.

Now, if you try using the auth generator, you’ll have this error:

However, if you’ve built your application with HTML templates, you can actually implement API authentication by following this guide: API Authentication — Phoenix v1.7.14

So I wonder whether it makes sense to adapt the auth generator to support backend-only applications.

I’ve been working on a prototype here, to validate the idea, where I’ve managed to make the registration endpoint work.

What are your thoughts on this? Does it make sense to continue working on this?

Thanks!

12 Likes

It would be a nice addition. When I moved over from my previous auth system, I had to add additional functions to the phx.gen.auth generated code for my API. There’s not that much to them, mainly changing flash and redirects into result types for the endpoints to return.

2 Likes

Oh nice, I’m glad they added this section. I implemented API authentication in pretty much the same manner by myself but it’s good that there’s an official guide, because there’s a good chance I screwed something up along the way. :laughing:

Thanks to the Phoenix team for taking a step away from making us roll our own authentication!

@iaguirre88 Huge fan of your proposal BTW.

(@dfalling Sorry for replying to your post, the forum UI drives me nuts. I don’t know how to un-reply to your post and I can’t delete posts on this forum.)

2 Likes

Quick update: I’m still working on this because it’s a great opportunity for me to learn and I’m having fun.
Now it supports login, logout and confirmation besides registration.

So this is the progress so far

  • Done
    • /register
    • /log_in
    • /log_out
    • /confirm
    • /confirm/:token
  • To do
    • /reset_password
    • /reset_password/:token
    • /settings
    • /settings/confirm_email/:token
2 Likes