bastianwegge
bastianwegge5w ago

I have a very stupid question: Is there

I have a very stupid question: Is there a way to programmatically create a user with password in convex?
17 Replies
erquhart
erquhart5w ago
Can you give a little more context on your goal, might help (I'd also say this isn't a stupid question at all imo)
bastianwegge
bastianweggeOP5w ago
Sure, let me elaborate: I have a controlled / closed environment, meaning it's running on-premise. I want to use convex with convex-auth for authentication. Now I have set-up convex-saas locally with the password provider, but I have no way to create a user programmatically. Neither the tutorial for convex-auth nor any other source I found mentions how to create users with their passwords. I can let them sign up, which would probably work, but would not be viable for my solution since people would receive their logins by an administrator (me basically). There's no need for additional security measures or self-service in terms of resetting the passwords or emails. Hope that's enough context, let me know if I should elaborate on any part 🙏
erquhart
erquhart5w ago
It would take some working around. Have you considered having "sending them their login" being just instructions to sign up with their email address, but you check to ensure the email address is on a list of approved signup emails? Or do you specifically not want folks creating their own passwords and having actual auth
erquhart
erquhart5w ago
You could run the check via createOrUpdateUser(), and do any other mutations you might want for admin purposes https://labs.convex.dev/auth/advanced#controlling-user-creation-and-account-linking-behavior
Advanced: Details - Convex Auth
Authentication library for your Convex backend
bastianwegge
bastianweggeOP5w ago
Since it's a air-gapped environment, there's no way to send mails or access the internet. The only way to access the system is through https on this network. I specifically don't want users to create their own passwords yes 😆
erquhart
erquhart5w ago
Ah I see. Hmm
bastianwegge
bastianweggeOP5w ago
I tried using insert into the collection but this way I don't get a password.
await ctx.db.insert("users", {
name: "John Doe",
});
await ctx.db.insert("users", {
name: "John Doe",
});
erquhart
erquhart5w ago
You could try calling the signin action server side, wrap it in your own function for provisioning users eg., ctx.runAction(api.auth.signIn, { ...params? }) you'd have to figure out the params to send. But if you have no email verification enabled, that might work
bastianwegge
bastianweggeOP5w ago
That's a great start, I'll dig into this 🙏 . Thank you very much
erquhart
erquhart5w ago
Please update if you land on a solution! Might be helpful for others
bastianwegge
bastianweggeOP5w ago
I tried running:
export const createUser = internalAction(async (ctx) => {
await ctx.runAction(api.auth.signIn, {
provider: "password",
params: {
flow: "signup",
email: "jeff@breff.com",
password: "password123",
},
});
});
export const createUser = internalAction(async (ctx) => {
await ctx.runAction(api.auth.signIn, {
provider: "password",
params: {
flow: "signup",
email: "jeff@breff.com",
password: "password123",
},
});
});
But this doesn't create anything and silently fails 😄
erquhart
erquhart5w ago
No errors in the convex logs?
erquhart
erquhart5w ago
GitHub
convex-auth/src/server/implementation/index.ts at 2f77702b0e42fa705...
Library for built-in auth. Contribute to get-convex/convex-auth development by creating an account on GitHub.
erquhart
erquhart5w ago
That's the signIn action Oh, and turn on verbose logging as well that might fix the silent failure (or fix the silence, that is, maybe)
bastianwegge
bastianweggeOP5w ago
I looked up the function signature and found that my parameters would match. I then signed up through the frontend to see what's happening in comparison to my action. So the arguments should be good as far as I see. I'm gonna look how to turn on verbose logging then 😄 Ah, getting errors ⚠️ thanks for the hint That was it ... You will never guess what the error was 😆 It's "signUp" not "signup"
export const createUser = action(async (ctx) => {
await ctx.runAction(api.auth.signIn, {
provider: "password",
params: {
flow: "signUp",
email: "jeff@breff.com",
password: "password123",
},
});
});
export const createUser = action(async (ctx) => {
await ctx.runAction(api.auth.signIn, {
provider: "password",
params: {
flow: "signUp",
email: "jeff@breff.com",
password: "password123",
},
});
});
This one works flawlessly 🎉 . Thank you a thousand times @erquhart ! Do you have a patron / coffee page I can spend some coins on?
erquhart
erquhart5w ago
Nope, this is just Convex community helping each other out ❤️ Glad it worked!
bastianwegge
bastianweggeOP5w ago
Not without your help 🙏 . This was my last blocker I believe. Thank you a lot!

Did you find this page helpful?