BA
Better Auth•2mo ago
Luke

Sign up a user that already exists

I'm trying to create a flow where a "guest" has filled out am appointment form but hasn't created an account. I am saving their details in the user table (email and name). What's the best way of transforming the "guest" user to a fully fledged account when they sign up? It would be nice if I can sign the user up, but keep the same user entity since I'm using it to link to other tables to keep track of what they've booked. When using the better auth signUp I get the User Already Exists error: [APIError: User already exists] { status: 'UNPROCESSABLE_ENTITY', body: { code: 'USER_ALREADY_EXISTS', message: 'User already exists' }, headers: {}, statusCode: 422 }
7 Replies
Vlad
Vlad•2mo ago
I think the anonymous plugin is what you need
Luke
LukeOP•2mo ago
Oh cool, I'll check it out. I still want to store the users email and name and link them to my appointments entity, so they can book an appointment but only see their appointments if they decide to create an account. Yeah, I don't think I can do it with this. So, the issue is that I'm currently creating a user (manually in the user table, without an account) when they fill out an appointment form. When a user signs up with an email that exists in the users table I want to be able to create them as a fully fledged user that can now log in. But this isn't possible since the signUpEmail call checks for an existing email in the users table. Is this something I could solve with a custom plugin perhaps? Ah yep, think I'll make a custom plugin for this.
Vlad
Vlad•2mo ago
Isn't it something the plugin does? The users log in anonymously When they sign in, you do the other stuff
Luke
LukeOP•2mo ago
Yeah, my functionality is more i have a user who isn't logged in but then can sign up in a different session, if that makes sense. Not really auth, but it made sense to me to have their details in the user table initially Flow is: Create User (no auth) just an email and a name. User signs up -> if the user exists in the table, make them an account
Petr Bela
Petr Bela•2mo ago
Since you've already created the User record, maybe you can just sign in instead of calling sign up again? The other option (probably cleaner) would be to store the email and name in a different table, something like "appointees" and then just re-link their appointments once they create a user?
Luke
LukeOP•2mo ago
Yeah, it just smelt a little wrong in the future that I would have to do a join on two tables to find the users of appointments. I ended up creating a custom plugin to sign-up a user and if the user exists I just link an account to the user on sign up 🙂 Basically copy and pasted the current sign-up and just modified it to fit:

Did you find this page helpful?