lcabrini

lcabrini

I’m fairly new to both Elixir and Phoenix, although I have quite a bit of experience with other languages and frameworks and I’m usually able to figure things out.

I’m working with Phoenix 1.7, liveview and gen.auth. I don’t want users to be able to register. Instead, when you staff members join, an administrator would set up user accounts for them. I would have thought this would be a farily common use case, but maybe I’m wrong, because I haven’t found much searching.

I get that I can remove/comment out the registration endpoint. And I need to create endpoints for an admin to manage users. What I’m now sure about is how to go about the confirmation. Looking at the code, removing confirmed_at from the model doesn’t seem like the right way forward. Or is it?

I’m guessing somebody else has been in the same situation and solved it. How did you go about it? What would be the most straight-forward way to disable self-registration and not require user confirmation?

Showing Posts 1 to 10

sodapopcan

sodapopcan

Hello and welcome!

phx_gen_auth is meant to give you the bare-minimum needed to get going with auth. If you are looking for more fully-featured auth, you can look at Pow although its LiveView support isn’t all there yet but it’s coming.

Otherwise, I recently did what you’re describing in phx_gen_auth by repurposing the reset password form and the confirmation token. I did the following:

  • Removed the registration form and anything related to it
  • Added a user creation form to my admin area which:
    • sets a long random password with :crypto.strong_rand_bytes/1
    • sends the user their confirmation email with the token link
  • Changed the confirmation page to display the reset password form and only mark the user as confirmed when they have set their new password.
  • Ensure users can’t login until they have been confirmed (this is not done by default by the generator)

One gotcha is that if you are generated auth using LiveView and you want to auto-login the user after they have set their new password, the confirmation page should use a controller, not a LiveView. This is because you can’t set session data over websockets.

I know other people around here have done something similar approaches if you search around. If you choose to go this route then I’d be happy to try and answer any questions!

lcabrini

lcabrini OP

Thanks a lot for your reply!

Yes, I had also seen Pow and I found it interesting. I was a bit confused about whether I should try to stick with the built-in batteries or add external ones. You have motivated me to give Pow a try and to compare the results for myself.

Your registration flow is pretty much exactly what I’m also looking to do. And thanks also for pointing out that gotcha. That is typically the kind of thing I’d overlook and spend hours trying to figure out why it’s not working.

sodapopcan

sodapopcan

You can read the motivations behind phx_gen_auth here. If that resonates with you then I would stick with it, otherwise using something else is perfectly acceptable. There is certainly no notion that one is better than the other, it all depends on your needs and usage!

LostKobrakai

LostKobrakai

I’d also add that it’s not meant to be the end of it all. It’s meant to give you a staring point and you can customize from there.

As for the confirmation. I’d still keep confirmation logic around. Doesn’t hurt to confirm that whomever owns the email is actually the intended person to be invited.

lcabrini

lcabrini OP

Good point. I’m not planning on getting rid of it, just leaving it out of the user creation flow (at least for the time being). It may make its way back in eventually, as per the flow @sodapopcan outlined.

lcabrini

lcabrini OP

Having read that document, I think I do agree with the points made.

sodapopcan

sodapopcan

Ha, you called me out on two little sticking points I had with my answer :sweat_smile:

The first I just don’t like how I worded it, but “a starting point” is what I meant by “get going with.” If all you need is an email/password auth system, it’s perfect (with a few tweaks like checking confirmation field and probably adding the user’s name). And just in case: I also didn’t mean “bare minimum” to be reductive in any way!

Secondly yes, you should keep the confirmation step around if having an account lets you do sensitive things! In my case, my client has email correspondence with everyone she invites, there is no sensitive data available, and there are no financial features, so I didn’t want to add that extra step.

sodapopcan

sodapopcan

Sorry to necro this thread but I’m writing a little debrief on this.

I was thinking about this more. As far as I can tell, the main security risk here is that someone intercepts the invite email, likely by already having access to that email account. So sending out a “regular” confirmation email afterwards is not going to help. It seems we’re guarding against a rare scenario where someone is able to intercept the invite but not the confirmation email. So long as we’re ensuring you may not log in without being confirmed, “auto-confirming” after setting up a new password should be enough. Am I missing something?

Thanks!

adw632

adw632

How does the admin provide passwords for the users they setup? How do you do this securely?

Are you using local password or an external authentication service such as social login via Google, Facebook, Apple etc?

If you are using a local password an alternative approach than say sharing passwords with these user’s would be to set a random pre-expired password and to send a confirmation link to the user. When the user comes back, force a first time password reset, this way you’re using time limited confirmation links and only the user knows their password going forward. You generally always want to avoid the admins having visibility of user passwords.

If you have also established secret/answer challenge questions, this would also be the right time to use them.

sodapopcan

sodapopcan

Hey Andrew! I outlined here how I’m doing it (I’m not OP of this thread, I was answering the question).

I’m just doing plain ol’ username/password with confirmation email and reset password repurposed for the invite. LKK mentioned that it might be a good idea to make them confirm their email anyway. That made sense to me but thinking about it more, since the invites are sent from the app to a known email, ie, users aren’t requesting an invite I’m now not seeing how requiring an extra confirmation step on top of that is really that much more secure. If users are requesting invites then it probably makes sense, though they would still need access to the email to get the invites so it doesn’t seem all that much more secure.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews