How to delete a user creates with oauth?

Is there a way to use the deleteUser api for a user that, for example, signed up via google? As soone as the session isn't fresh the api required a password, but oauth users don't have a password. Do I have to manually cover these use cases? If yes, how can I generate my own token and send it to that user's email?
52 Replies
Duki
DukiOP2w ago
I am doing this, but as mentioned above. You need to provide a password if the session isn't fresh anymore. An oauth user has no password.
Blank
Blank2w ago
what happens if you call it without a password
Duki
DukiOP2w ago
It tells me to re-authenticate
Blank
Blank2w ago
ah there is no api to trigger the send delete mail manually
Duki
DukiOP2w ago
Yea but even if there was, How would I generate a token, which tells BA to delete said user?
Blank
Blank2w ago
ok so you just need to add the method for sending email and call authClient.deleteUser({}); it will send a email with a link which will delete the user
Duki
DukiOP2w ago
I am providing a function already. When I call deleteUser I get the error code SESSION_EXPIRED_REAUTHENTICATE_TO_PERFORM_THIS_ACTION
Blank
Blank2w ago
:hmm: let me check the code
Duki
DukiOP2w ago
Which means I need to provide a password to the deleteUser function but a user created via google, doesn't have a password
Blank
Blank2w ago
no you should not need to have a password
Blank
Blank2w ago
yeah you could also pass in a token but you dont have a api to generate it
Duki
DukiOP2w ago
yea exactly
Blank
Blank2w ago
i think there are missing apis which needs to be implemented @bekacru
Blank
Blank2w ago
it short circuits in the freshAge function, so passing in token also would not help even if you got one
No description
bekacru
bekacru2w ago
“sendDeleteAccountVerification” this is the api, but this means you’d need to send them the form to their email
Blank
Blank2w ago
yeah but how do you trigger it manually?
bekacru
bekacru2w ago
You can set freshAge to 0 to bypass fresh age checks. But if you just to delete user directly, I suggest making another endpoint Yeah we don’t have a direct method. Even if we had, wouldn’t be exposed to the client
Blank
Blank2w ago
yeah that would be a server method so the best optioin is to make a api endpoint on their own server and call auth.api.deleteuser from there?
Duki
DukiOP2w ago
Can I temporarily set the freshAge to 0? I'm actually doing this in a formAction in svelteKit
Blank
Blank2w ago
:Okay:
bekacru
bekacru2w ago
I mean if you’re already on the server, just call your orm but we may should consider different flow for the server side call No I mean you can using hooks Just return new context
Blank
Blank2w ago
I think we are using a custom delete at zero but something needs to be done for oauth users, like triggering the send verification email on server
Duki
DukiOP2w ago
I like the idea of the verfication email, not only because of security reasons but also because the user has to double commit the delete action.
bekacru
bekacru2w ago
Oh that’s should be possible
Blank
Blank2w ago
ok should I make a pr? btw i have updated my old pr as you wanted
Duki
DukiOP2w ago
@Blank Thanks a lot four your help btw 😊
Blank
Blank2w ago
:neuroHeart:
bekacru
bekacru2w ago
I think the only issue here is, we don’t have to be checking for fresh age, if we’re sending them the token to their email.
Blank
Blank2w ago
there is like nothing we can do for oauth user's either way, forcing them to re login is one option but then we dont need the api either way
bekacru
bekacru2w ago
Which one was it?
Blank
Blank2w ago
GitHub
fix: use dynamic list of social providers to allow generic oauth by...
fixes #2610 ref #2557 (comment) the endpoints now use the soclal provider list in context so that generic oauth plugin providers are also allowed in token endpoints
bekacru
bekacru2w ago
Yeah, the idea of fresh age is to require users for re-login to perform some actions Oh thanks! Will check it and merge
Blank
Blank2w ago
is sending a email not enough verification?
Duki
DukiOP2w ago
where in the context do I set the freshAge btw? 🤔 Trying to figure out if that workaround actually works
Blank
Blank2w ago
also there is no api to force re logiin for oauth users
bekacru
bekacru2w ago
It is. That’s why I’m suggesting not checking fresh age if we’re sending the email anyway Just call sign in :))
Blank
Blank2w ago
yeah but that means you are relying on user to have verification email setup
bekacru
bekacru2w ago
Will send code snippet when I’m at my computer. Using phone rn No. All what needs to be done is if we’re sending an email, don’t check for fresh age
Blank
Blank2w ago
so this is the flow
1. authClient.deleteUser({}); // pass nothing, not fresh session, no password setup
2. email sent
3. click on the url to delete account
1. authClient.deleteUser({}); // pass nothing, not fresh session, no password setup
2. email sent
3. click on the url to delete account
correct?
Duki
DukiOP2w ago
ah found it, there is a context inside the context. I've set up the hook like this now, but I still get the re-authentication error (will let you guys discuss now btw 😂 , before I spam you too much)
hooks: {
before: createAuthMiddleware(async (ctx) => {
if (ctx.path === '/delete-user') {
return {
context: {
...ctx,
context: {
...ctx.context,
sessionConfig: {
...ctx.context.sessionConfig,
freshAge: 0
}
},
}
};
}
}),

}
hooks: {
before: createAuthMiddleware(async (ctx) => {
if (ctx.path === '/delete-user') {
return {
context: {
...ctx,
context: {
...ctx.context,
sessionConfig: {
...ctx.context.sessionConfig,
freshAge: 0
}
},
}
};
}
}),

}
Blank
Blank2w ago
is this what will work for you @Duki ?
Duki
DukiOP2w ago
Yea I would be happy with that actually. Imo a verification email sounds good enough to me too, don't need to ask for a password again. I think password isn't required either when a user wants to change their email. We (we = BA) just send a verification email too.
Blank
Blank2w ago
yeah I will make a PR from what we have discussed, will change details if needed
Duki
DukiOP2w ago
Awesome, thank you and @bekacru so much! Really love BA so far (despite some quirks here and there 😂 but at least it's being worked on them!).
Blank
Blank2w ago
GitHub
feat: new user delete flow by BlankParticle · Pull Request #2704 ...
for context: https://discord.com/channels/1288403910284935179/1373686795765547180 This PR changes the delete user account flow to use the email verification for OAuth users to delete their account ...
Blank
Blank2w ago
you can try this out locally if you want to check if it works for your usecase
npm i https://pkg.pr.new/better-auth/better-auth@2704
npm i https://pkg.pr.new/better-auth/better-auth@2704
need to update tests lol
bekacru
bekacru2w ago
yes
Blank
Blank2w ago
hey what do I do If there is verification setup, user has a password setup, but he didnt provide it
// if the user has a password but it was not provided, we can't delete the account
if (account && account.password && !canDelete) {
throw new APIError("BAD_REQUEST", {
message: BASE_ERROR_CODES.USER_ALREADY_HAS_PASSWORD,
});
}
// if the user has a password but it was not provided, we can't delete the account
if (account && account.password && !canDelete) {
throw new APIError("BAD_REQUEST", {
message: BASE_ERROR_CODES.USER_ALREADY_HAS_PASSWORD,
});
}
i am doing this, but it breaks existsing tests ok updated tests to acccount of oauth users seprately
Duki
DukiOP2w ago
Works perfect for my use case. Even when a user has both, a credential type of account and an oauth account linked together. This flow will ask the user to provide their password, which is perfect imo!
Blank
Blank2w ago
:Okay: now we wait for merge

Did you find this page helpful?