[GH-ISSUE #486] 🐛 SQL_PARSE_ERROR: near WHERE, "None" on sign-in with idToken for existing account #8290

Closed
opened 2026-04-13 03:21:57 -05:00 by GiteaMirror · 11 comments
Owner

Originally created by @abegehr on GitHub (Nov 10, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/486

Describe the bug
Getting the following error when trying to sign-in with Apple when the account already exists:

LibsqlError: SQL_PARSE_ERROR: SQL string could not be parsed: near WHERE, "None": syntax error at (1, 28)
    at mapHranaError (file:///var/task/node_modules/.pnpm/@libsql+client@0.8.1/node_modules/@libsql/client/lib-esm/hrana.js:262:16)
    at file:///var/task/node_modules/.pnpm/@libsql+client@0.8.1/node_modules/@libsql/client/lib-esm/http.js:76:23
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async LibsqlConnection.executeQuery (file:///var/task/node_modules/.pnpm/@libsql+kysely-libsql@0.4.1_kysely@0.27.4/node_modules/@libsql/kysely-libsql/lib-esm/index.js:70:24)
    at async file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-executor/query-executor-base.js:35:28
    at async DefaultConnectionProvider.provideConnection (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/driver/default-connection-provider.js:10:20)
    at async DefaultQueryExecutor.executeQuery (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-executor/query-executor-base.js:34:16)
    at async UpdateQueryBuilder.execute (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-builder/update-query-builder.js:388:24)
    at async UpdateQueryBuilder.executeTakeFirst (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-builder/update-query-builder.js:407:26)
    at async Object.update (file:///var/task/node_modules/.pnpm/better-auth@0.8.2-beta.1/node_modules/better-auth/dist/index.js:84:15035) {
  code: 'SQL_PARSE_ERROR',
  rawCode: undefined,
  [cause]: [ResponseError: SQL string could not be parsed: near WHERE, "None": syntax error at (1, 28)] {
    code: 'SQL_PARSE_ERROR',
    proto: {
      message: 'SQL string could not be parsed: near WHERE, "None": syntax error at (1, 28)',
      code: 'SQL_PARSE_ERROR'
    }
  }
}

To Reproduce
I'm using a libsql kysely db adapter connected to a Turso db:

import { LibsqlDialect } from "@libsql/kysely-libsql";
import { betterAuth } from "better-auth";

const dialect = new LibsqlDialect({
  url: import.meta.env.ASTRO_DB_REMOTE_URL,
  authToken: import.meta.env.ASTRO_DB_APP_TOKEN,
});

export const auth = betterAuth({
  database: {
    dialect,
    type: "sqlite",
  },
  
};

Expected behavior
I would expect it to work or throw a more indicative error.

Additional context
I think this line throws the error in the kysely adapter: 691510711e/packages/better-auth/src/adapters/kysely-adapter/index.ts (L252)

Originally created by @abegehr on GitHub (Nov 10, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/486 **Describe the bug** Getting the following error when trying to sign-in with Apple when the account already exists: ``` LibsqlError: SQL_PARSE_ERROR: SQL string could not be parsed: near WHERE, "None": syntax error at (1, 28) at mapHranaError (file:///var/task/node_modules/.pnpm/@libsql+client@0.8.1/node_modules/@libsql/client/lib-esm/hrana.js:262:16) at file:///var/task/node_modules/.pnpm/@libsql+client@0.8.1/node_modules/@libsql/client/lib-esm/http.js:76:23 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async LibsqlConnection.executeQuery (file:///var/task/node_modules/.pnpm/@libsql+kysely-libsql@0.4.1_kysely@0.27.4/node_modules/@libsql/kysely-libsql/lib-esm/index.js:70:24) at async file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-executor/query-executor-base.js:35:28 at async DefaultConnectionProvider.provideConnection (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/driver/default-connection-provider.js:10:20) at async DefaultQueryExecutor.executeQuery (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-executor/query-executor-base.js:34:16) at async UpdateQueryBuilder.execute (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-builder/update-query-builder.js:388:24) at async UpdateQueryBuilder.executeTakeFirst (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-builder/update-query-builder.js:407:26) at async Object.update (file:///var/task/node_modules/.pnpm/better-auth@0.8.2-beta.1/node_modules/better-auth/dist/index.js:84:15035) { code: 'SQL_PARSE_ERROR', rawCode: undefined, [cause]: [ResponseError: SQL string could not be parsed: near WHERE, "None": syntax error at (1, 28)] { code: 'SQL_PARSE_ERROR', proto: { message: 'SQL string could not be parsed: near WHERE, "None": syntax error at (1, 28)', code: 'SQL_PARSE_ERROR' } } } ``` **To Reproduce** I'm using a libsql kysely db adapter connected to a Turso db: ```ts import { LibsqlDialect } from "@libsql/kysely-libsql"; import { betterAuth } from "better-auth"; const dialect = new LibsqlDialect({ url: import.meta.env.ASTRO_DB_REMOTE_URL, authToken: import.meta.env.ASTRO_DB_APP_TOKEN, }); export const auth = betterAuth({ database: { dialect, type: "sqlite", }, … }; ``` **Expected behavior** I would expect it to work or throw a more indicative error. **Additional context** I think this line throws the error in the kysely adapter: https://github.com/better-auth/better-auth/blob/691510711e6030a38c17b01f998cc62823bb1a06/packages/better-auth/src/adapters/kysely-adapter/index.ts#L252
GiteaMirror added the locked label 2026-04-13 03:21:57 -05:00
Author
Owner

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

Hey could you please check if v0.8.2-beta.2 resolves the issue?

<!-- gh-comment-id:2467358128 --> @Bekacru commented on GitHub (Nov 11, 2024): Hey could you please check if v0.8.2-beta.2 resolves the issue?
Author
Owner

@abegehr commented on GitHub (Nov 11, 2024):

Thanks for the quick response, @Bekacru! I'll try this out now to see if it fixes the issue with "Sign in with Apple".

EDIT: no issue on Google sign-in

<!-- gh-comment-id:2467620329 --> @abegehr commented on GitHub (Nov 11, 2024): Thanks for the quick response, @Bekacru! I'll try this out now to see if it fixes the issue with "Sign in with Apple". EDIT: no issue on Google sign-in
Author
Owner

@abegehr commented on GitHub (Nov 11, 2024):

Seems like I still get the error on 0.8.2-beta.2:

BetterAuth API Error: LibsqlError: SQL_PARSE_ERROR: SQL string could not be parsed: near WHERE, "None": syntax error at (1, 28)
    at mapHranaError (file:///var/task/node_modules/.pnpm/@libsql+client@0.8.1/node_modules/@libsql/client/lib-esm/hrana.js:262:16)
    at file:///var/task/node_modules/.pnpm/@libsql+client@0.8.1/node_modules/@libsql/client/lib-esm/http.js:76:23
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async LibsqlConnection.executeQuery (file:///var/task/node_modules/.pnpm/@libsql+kysely-libsql@0.4.1_kysely@0.27.4/node_modules/@libsql/kysely-libsql/lib-esm/index.js:70:24)
    at async file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-executor/query-executor-base.js:35:28
    at async DefaultConnectionProvider.provideConnection (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/driver/default-connection-provider.js:10:20)
    at async DefaultQueryExecutor.executeQuery (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-executor/query-executor-base.js:34:16)
    at async UpdateQueryBuilder.execute (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-builder/update-query-builder.js:388:24)
    at async UpdateQueryBuilder.executeTakeFirst (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-builder/update-query-builder.js:407:26)
    at async Object.update (file:///var/task/node_modules/.pnpm/better-auth@0.8.2-beta.2/node_modules/better-auth/dist/index.js:84:15035) {
  code: 'SQL_PARSE_ERROR',
  rawCode: undefined,
  [cause]: [ResponseError: SQL string could not be parsed: near WHERE, "None": syntax error at (1, 28)] {
    code: 'SQL_PARSE_ERROR',
    proto: {
      message: 'SQL string could not be parsed: near WHERE, "None": syntax error at (1, 28)',
      code: 'SQL_PARSE_ERROR'
    }
  }
}

This is the code:

      const res = await auth.signIn.social({
        provider: "apple",
        idToken: {
          token: result.identityToken,
          // Notice: don't add nonce, since the idToken doesn't contain it.
        },
      });
      console.debug("signIn.social.apple", res);

(the identityToken is obtained from native iOS Apple sign-in via @capgo/capacitor-social-login)

Do you have an idea how I can best debug this in better-auth, @Bekacru? The error above is coming from the Astro app running in Vercel. Interesting is that the error doesn't occur on the first sign-in with Apple but only on subsequent ones, when the account, user, and session are successfully created in the db. On subsequent logins, no new sessions are created.

<!-- gh-comment-id:2468409943 --> @abegehr commented on GitHub (Nov 11, 2024): Seems like I still get the error on 0.8.2-beta.2: ``` BetterAuth API Error: LibsqlError: SQL_PARSE_ERROR: SQL string could not be parsed: near WHERE, "None": syntax error at (1, 28) at mapHranaError (file:///var/task/node_modules/.pnpm/@libsql+client@0.8.1/node_modules/@libsql/client/lib-esm/hrana.js:262:16) at file:///var/task/node_modules/.pnpm/@libsql+client@0.8.1/node_modules/@libsql/client/lib-esm/http.js:76:23 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async LibsqlConnection.executeQuery (file:///var/task/node_modules/.pnpm/@libsql+kysely-libsql@0.4.1_kysely@0.27.4/node_modules/@libsql/kysely-libsql/lib-esm/index.js:70:24) at async file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-executor/query-executor-base.js:35:28 at async DefaultConnectionProvider.provideConnection (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/driver/default-connection-provider.js:10:20) at async DefaultQueryExecutor.executeQuery (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-executor/query-executor-base.js:34:16) at async UpdateQueryBuilder.execute (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-builder/update-query-builder.js:388:24) at async UpdateQueryBuilder.executeTakeFirst (file:///var/task/node_modules/.pnpm/kysely@0.27.4/node_modules/kysely/dist/esm/query-builder/update-query-builder.js:407:26) at async Object.update (file:///var/task/node_modules/.pnpm/better-auth@0.8.2-beta.2/node_modules/better-auth/dist/index.js:84:15035) { code: 'SQL_PARSE_ERROR', rawCode: undefined, [cause]: [ResponseError: SQL string could not be parsed: near WHERE, "None": syntax error at (1, 28)] { code: 'SQL_PARSE_ERROR', proto: { message: 'SQL string could not be parsed: near WHERE, "None": syntax error at (1, 28)', code: 'SQL_PARSE_ERROR' } } } ``` This is the code: ```ts const res = await auth.signIn.social({ provider: "apple", idToken: { token: result.identityToken, // Notice: don't add nonce, since the idToken doesn't contain it. }, }); console.debug("signIn.social.apple", res); ``` (the identityToken is obtained from native iOS Apple sign-in via `@capgo/capacitor-social-login`) Do you have an idea how I can best debug this in better-auth, @Bekacru? The error above is coming from the Astro app running in Vercel. Interesting is that the error doesn't occur on the first sign-in with Apple but only on subsequent ones, when the account, user, and session are successfully created in the db. On subsequent logins, no new sessions are created.
Author
Owner

@luksch42 commented on GitHub (Nov 11, 2024):

Had the same with drizzle by the way. Is it because Apple return the user only on the first response and updateAccount does not handle that? 

https://v2.lucia-auth.com/oauth/providers/apple/

<!-- gh-comment-id:2468443525 --> @luksch42 commented on GitHub (Nov 11, 2024): Had the same with drizzle by the way. Is it because Apple return the user only on the first response and updateAccount does not handle that?  https://v2.lucia-auth.com/oauth/providers/apple/
Author
Owner

@abegehr commented on GitHub (Nov 16, 2024):

I think this is an issue with the database or how better-auth makes a query, since I can run the social-apple-login with the same token and it always succeeds on the first try (on empty database) but when the account+user+session exists and I make the request to better-auth again, it fails with the db error.

const res = await auth.signIn.social({
  provider: "apple",
  idToken: {
    token: "…"
  },
});
<!-- gh-comment-id:2480764139 --> @abegehr commented on GitHub (Nov 16, 2024): I think this is an issue with the database or how better-auth makes a query, since I can run the social-apple-login with the same token and it always succeeds on the first try (on empty database) but when the account+user+session exists and I make the request to better-auth again, it fails with the db error. ```ts const res = await auth.signIn.social({ provider: "apple", idToken: { token: "…" }, }); ```
Author
Owner

@abegehr commented on GitHub (Nov 16, 2024):

I tracked the issue down the rabbit hole:

  1. e8767100dd/packages/better-auth/src/oauth2/link-account.ts (L77)
  2. e8767100dd/packages/better-auth/src/adapters/kysely-adapter/kysely-adapter.ts (L305)
  3. e8767100dd/packages/better-auth/src/adapters/kysely-adapter/kysely-adapter.ts (L225)

It's failing with LibsqlError: SQLITE_ERROR: near "where": syntax error, even though the where-clause is super basic:

withReturning() {
  values: {
    accessToken: undefined,
    idToken: undefined,
    refreshToken: undefined,
    expiresAt: undefined
  },
  builder: UpdateQueryBuilder {},
  model: 'account',
  where: [ { field: 'id', value: 'xyz' } ]
}
convertWhereClause eb() { field: 'id', operator: '=', value: 'xyz' }

Not sure where the issue is but all looks good to me in better-auth part.

<!-- gh-comment-id:2480833433 --> @abegehr commented on GitHub (Nov 16, 2024): I tracked the issue down the rabbit hole: 1. https://github.com/better-auth/better-auth/blob/e8767100ddbfd6185f3a43180b775beb631e4d89/packages/better-auth/src/oauth2/link-account.ts#L77 2. https://github.com/better-auth/better-auth/blob/e8767100ddbfd6185f3a43180b775beb631e4d89/packages/better-auth/src/adapters/kysely-adapter/kysely-adapter.ts#L305 3. https://github.com/better-auth/better-auth/blob/e8767100ddbfd6185f3a43180b775beb631e4d89/packages/better-auth/src/adapters/kysely-adapter/kysely-adapter.ts#L225 It's failing with `LibsqlError: SQLITE_ERROR: near "where": syntax error`, even though the where-clause is super basic: ``` withReturning() { values: { accessToken: undefined, idToken: undefined, refreshToken: undefined, expiresAt: undefined }, builder: UpdateQueryBuilder {}, model: 'account', where: [ { field: 'id', value: 'xyz' } ] } convertWhereClause eb() { field: 'id', operator: '=', value: 'xyz' } ``` Not sure where the issue is but all looks good to me in better-auth part.
Author
Owner

@luksch42 commented on GitHub (Nov 20, 2024):

@Bekacru Any idea what's happening here?

<!-- gh-comment-id:2488181803 --> @luksch42 commented on GitHub (Nov 20, 2024): @Bekacru Any idea what's happening here?
Author
Owner

@productdevbook commented on GitHub (Nov 21, 2024):

Wouldn't it be better to make an example or add docs that works with capacitor ? Expo

<!-- gh-comment-id:2491808127 --> @productdevbook commented on GitHub (Nov 21, 2024): Wouldn't it be better to make an example or add docs that works with capacitor ? [Expo](https://www.better-auth.com/docs/integrations/expo)
Author
Owner

@reslear commented on GitHub (Dec 12, 2024):

hi @Bekacru maybe it would be possible to patch (set/update) authorization data manually?

for example if we in web - use better-auth, if in native ios/andoid - use native like plugins for native authorization and then patch better auth state

https://github.com/capacitor-community/apple-sign-in or https://github.com/Cap-go/capacitor-social-login

import { Capacitor } from '@capacitor/core';
import {
  SignInWithApple,
} from '@capacitor-community/apple-sign-in';

// draft setSession
const { data: session, setSession } = authClient.useSession();

if (Capacitor.isNativePlatform()) {
  SignInWithApple.authorize(options)
    .then((result: SignInWithAppleResponse) => {
      // Handle user information
      // Validate token with server and create new session
      
      // path better auth for useSession usable using native ? 
      // set like SignInWithAppleResponse 
      // draft setSession
      await setSession({
         // result.response.identityToken 
      })
      
      })
      
    .catch(error => {
    // Handle error
    });
} else {
  await auth.signIn.social({
    provider: "apple",
    // ...
  });
}

// 
console.log(session) 

Need a way to get in, because rewriting your off-the-shelf social networking plugins is a bad idea.

<!-- gh-comment-id:2540057548 --> @reslear commented on GitHub (Dec 12, 2024): hi @Bekacru maybe it would be possible to patch (set/update) authorization data manually? for example if we in web - use better-auth, if in native ios/andoid - use native like plugins for native authorization and then patch better auth state https://github.com/capacitor-community/apple-sign-in or https://github.com/Cap-go/capacitor-social-login ```ts import { Capacitor } from '@capacitor/core'; import { SignInWithApple, } from '@capacitor-community/apple-sign-in'; // draft setSession const { data: session, setSession } = authClient.useSession(); if (Capacitor.isNativePlatform()) { SignInWithApple.authorize(options) .then((result: SignInWithAppleResponse) => { // Handle user information // Validate token with server and create new session // path better auth for useSession usable using native ? // set like SignInWithAppleResponse // draft setSession await setSession({ // result.response.identityToken }) }) .catch(error => { // Handle error }); } else { await auth.signIn.social({ provider: "apple", // ... }); } // console.log(session) ``` Need a way to get in, because rewriting your off-the-shelf social networking plugins is a bad idea.
Author
Owner

@Bekacru commented on GitHub (Dec 16, 2024):

@reslear you can pass the idToken as

await auth.signIn.social({
    provider: "apple",
    idToken: {
        token: //
    }
})
<!-- gh-comment-id:2545194135 --> @Bekacru commented on GitHub (Dec 16, 2024): @reslear you can pass the idToken as ```ts await auth.signIn.social({ provider: "apple", idToken: { token: // } }) ```
Author
Owner

@Bekacru commented on GitHub (Jan 13, 2025):

can someone please confirm me if this is still an issue?

<!-- gh-comment-id:2586363629 --> @Bekacru commented on GitHub (Jan 13, 2025): can someone please confirm me if this is still an issue?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8290