Syntax error in request on updateUser (postgres) #282

Closed
opened 2026-03-13 07:40:39 -05:00 by GiteaMirror · 8 comments
Owner

Originally created by @MrKepzie on GitHub (Nov 26, 2024).

Use-case: I figured that when users are clicking on the email to reset their password containing their token, the resetPassword function should not only let them reset their password but also verify their account (if not done yet) at the same time.

This happens for example if as an admin I want to create an account for somebody, and right away send them the forgotten password email so that they can initialize it.

So anyway, in my endpoint to update the user password, I also want to mark him as verified like so:


// Sign the user
const signInRep = await auth.api.signInEmail({body: {
            email: email,
            password: password,
            dontRememberMe: true
 }, headers: request.headers, asResponse: true});

// This seems necessary otherwise updateUser doesn't have the appropriate headers.
 const header = signInRep.headers.get("set-cookie");
 const parsedCookies = parseSetCookieHeader(header || "");
 const signedCookie = parsedCookies.get("better-auth.session_token");
 let headers = new Headers();
headers.set("cookie", header);

// Update user verified state
 await auth.api.updateUser({body: {
            emailVerified: true,
   }, headers: headers})

But updateUser is throwing the following exception on the postgresql request:

syntax error at or near where
update user set where email = $1 returning *

Note that I use updateUser in the same way elsewhere but to modify other fields and it works well.

Originally created by @MrKepzie on GitHub (Nov 26, 2024). Use-case: I figured that when users are clicking on the email to reset their password containing their token, the resetPassword function should not only let them reset their password but also verify their account (if not done yet) at the same time. This happens for example if as an admin I want to create an account for somebody, and right away send them the forgotten password email so that they can initialize it. So anyway, in my endpoint to update the user password, I also want to mark him as verified like so: ``` // Sign the user const signInRep = await auth.api.signInEmail({body: { email: email, password: password, dontRememberMe: true }, headers: request.headers, asResponse: true}); // This seems necessary otherwise updateUser doesn't have the appropriate headers. const header = signInRep.headers.get("set-cookie"); const parsedCookies = parseSetCookieHeader(header || ""); const signedCookie = parsedCookies.get("better-auth.session_token"); let headers = new Headers(); headers.set("cookie", header); // Update user verified state await auth.api.updateUser({body: { emailVerified: true, }, headers: headers}) ``` But updateUser is throwing the following exception on the postgresql request: `syntax error at or near where` `update user set where email = $1 returning *` Note that I use updateUser in the same way elsewhere but to modify other fields and it works well.
Author
Owner

@Bekacru commented on GitHub (Nov 26, 2024):

Yeah, fair point that we should update the email verification status during password reset. But in your case, why not directly update the emailVerified field in the database? updateUser won’t allow it since emailVerified isn’t a user-input field.

@Bekacru commented on GitHub (Nov 26, 2024): Yeah, fair point that we should update the email verification status during password reset. But in your case, why not directly update the `emailVerified` field in the database? `updateUser` won’t allow it since `emailVerified` isn’t a user-input field.
Author
Owner

@MrKepzie commented on GitHub (Nov 26, 2024):

Using something like this ?

const context = await auth.$context;
        context.adapter.update<User>({
            model: 'user',
            where: [
                {
                    field: "email",
                    value: email
                }
            ],
            update: {
                emailVerified: true
            }
        });
@MrKepzie commented on GitHub (Nov 26, 2024): Using something like this ? ``` const context = await auth.$context; context.adapter.update<User>({ model: 'user', where: [ { field: "email", value: email } ], update: { emailVerified: true } }); ```
Author
Owner

@Bekacru commented on GitHub (Nov 26, 2024):

no I was suggesting the ORM you're using but this could work as well :) (although won't recommend)

@Bekacru commented on GitHub (Nov 26, 2024): no I was suggesting the ORM you're using but this could work as well :) (although won't recommend)
Author
Owner

@MrKepzie commented on GitHub (Nov 26, 2024):

I am actually not using anything, right now in this project only better-auth is using the DB. It is convenient to use the same API/connection.
Why dont't your recommend ?

@MrKepzie commented on GitHub (Nov 26, 2024): I am actually not using anything, right now in this project only better-auth is using the DB. It is convenient to use the same API/connection. Why dont't your recommend ?
Author
Owner

@Bekacru commented on GitHub (Nov 26, 2024):

If you're already using an ORM, this is a wrapper over it, so it might be overkill and potentially inefficient. But for something like this, it's definitely usable.

@Bekacru commented on GitHub (Nov 26, 2024): If you're already using an ORM, this is a wrapper over it, so it might be overkill and potentially inefficient. But for something like this, it's definitely usable.
Author
Owner

@MrKepzie commented on GitHub (Nov 26, 2024):

ok - it's more than good enough for my needs.
Also regarding the piece of code in the original post where I copy the content of the set-cookie header to the cookie of a new header, is there a better way to use the API than that ?

@MrKepzie commented on GitHub (Nov 26, 2024): ok - it's more than good enough for my needs. Also regarding the piece of code in the original post where I copy the content of the **set-cookie** header to the **cookie** of a new header, is there a better way to use the API than that ?
Author
Owner

@Bekacru commented on GitHub (Nov 26, 2024):

Since you don't need to use updateUser now, you don't need to parse the cookie. But, that would be required since the original headers don't have the cookie.

@Bekacru commented on GitHub (Nov 26, 2024): Since you don't need to use `updateUser` now, you don't need to parse the cookie. But, that would be required since the original headers don't have the cookie.
Author
Owner

@MrKepzie commented on GitHub (Nov 26, 2024):

ok thanks. Maybe this could be documented a bit better. Had to go through the source code to find relevant bits

@MrKepzie commented on GitHub (Nov 26, 2024): ok thanks. Maybe this could be documented a bit better. Had to go through the source code to find relevant bits
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#282