[BUG] lastLoginMethod database field never updates after initial user creation #1915

Closed
opened 2026-03-13 09:12:31 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @Maxime-RiseVerse on GitHub (Sep 13, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Configure Better Auth with lastLoginMethod plugin and storeInDatabase: true
  2. Create a new user account via email sign-up
  3. Sign out and sign in again with the Google (same email), It's auto-linking
  4. Check database lastLoginMethod field still shows the initial login method, never updates

Current vs. Expected behavior

Current behavior: The lastLoginMethod database field is only set once during user creation through the user.create.before hook, and never updates on subsequent logins.

Expected behavior: The database field should update on each login to reflect the most recent authentication method used, as stated in the documentation: "tracks the most recent authentication method used by users".

What version of Better Auth are you using?

1.3.9

System info

{
  "system": {
    "platform": "win32",
    "arch": "x64",
    "version": "Windows 11 Pro",
    "release": "10.0.26100",
    "cpuCount": 16,
    "cpuModel": "Intel(R) Core(TM) i7-10700K CPU @ 3.80GHz",
    "totalMemory": "31.88 GB",
    "freeMemory": "20.00 GB"
  },
  "node": {
    "version": "v22.14.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "10.9.2"
  },
  "frameworks": [
    {
      "name": "next",
      "version": "15.5.2"
    },
    {
      "name": "react",
      "version": "19.1.1"
    }
  ],
  "databases": [
    {
      "name": "pg",
      "version": "^8.16.3"
    },
    {
      "name": "drizzle",
      "version": "^0.44.5"
    }
  ],
  "betterAuth": {
    "version": "0.1.0",
    "config": null
  }
}

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

plugins: [
    lastLoginMethod({
      storeInDatabase: true
    })
  ]

Additional context

The cookie behavior works correctly (updates on every login), but the database field remains static after initial user creation. This causes cross-device inconsistency and prevents accurate tracking of login method changes.

Source code reference:
https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/last-login-method/index.ts#L61-L77

Originally created by @Maxime-RiseVerse on GitHub (Sep 13, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Configure Better Auth with lastLoginMethod plugin and storeInDatabase: true 2. Create a new user account via email sign-up 3. Sign out and sign in again with the Google (same email), It's auto-linking 4. Check database lastLoginMethod field still shows the initial login method, never updates ### Current vs. Expected behavior Current behavior: The lastLoginMethod database field is only set once during user creation through the user.create.before hook, and never updates on subsequent logins. Expected behavior: The database field should update on each login to reflect the most recent authentication method used, as stated in the documentation: "tracks the most recent authentication method used by users". ### What version of Better Auth are you using? 1.3.9 ### System info ```bash { "system": { "platform": "win32", "arch": "x64", "version": "Windows 11 Pro", "release": "10.0.26100", "cpuCount": 16, "cpuModel": "Intel(R) Core(TM) i7-10700K CPU @ 3.80GHz", "totalMemory": "31.88 GB", "freeMemory": "20.00 GB" }, "node": { "version": "v22.14.0", "env": "development" }, "packageManager": { "name": "npm", "version": "10.9.2" }, "frameworks": [ { "name": "next", "version": "15.5.2" }, { "name": "react", "version": "19.1.1" } ], "databases": [ { "name": "pg", "version": "^8.16.3" }, { "name": "drizzle", "version": "^0.44.5" } ], "betterAuth": { "version": "0.1.0", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript plugins: [ lastLoginMethod({ storeInDatabase: true }) ] ``` ### Additional context The cookie behavior works correctly (updates on every login), but the database field remains static after initial user creation. This causes cross-device inconsistency and prevents accurate tracking of login method changes. Source code reference: https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/last-login-method/index.ts#L61-L77
GiteaMirror added the bug label 2026-03-13 09:12:31 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 13, 2025):

This is a confirmed bug: the lastLoginMethod database field only updates during user creation and does not change on subsequent logins, even though the documentation suggests it should track the most recent authentication method used by the user. The plugin currently sets the field in the user.create.before hook, but does not update it after login events—only the cookie is updated on every login, not the database field (source).

Workaround:
You can use a custom database hook to update the lastLoginMethod field after each login. The recommended approach is to use a session.create.after hook (or similar) to update the user record in your database. Here’s a conceptual example:

databaseHooks: {
  session: {
    create: {
      async after(session, context) {
        // Determine the login method (reuse your customResolveMethod logic)
        const lastUsedLoginMethod = customResolveMethod(context);
        if (lastUsedLoginMethod && session?.user?.id) {
          await db.user.update({
            where: { id: session.user.id },
            data: { lastLoginMethod: lastUsedLoginMethod },
          });
        }
      },
    },
  },
}

This ensures the field is updated every time a new session is created (i.e., on every login), keeping the database in sync with the user's most recent authentication method. You can adapt this pattern to your ORM and authentication flow (related workaround for similar issues).

If you want the plugin to handle this automatically, it would need to be enhanced to include such a hook. Let me know if you need a more detailed example for your specific stack!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Sep 13, 2025): <!-- Answer --> This is a confirmed bug: the `lastLoginMethod` database field only updates during user creation and does not change on subsequent logins, even though the documentation suggests it should track the most recent authentication method used by the user. The plugin currently sets the field in the `user.create.before` hook, but does not update it after login events—only the cookie is updated on every login, not the database field ([source](https://github.com/better-auth/better-auth/blob/47e3c5fba591de7c208556465d3bf31a14128584/packages/better-auth/src/plugins/last-login-method/index.ts)). **Workaround:** You can use a custom database hook to update the `lastLoginMethod` field after each login. The recommended approach is to use a `session.create.after` hook (or similar) to update the user record in your database. Here’s a conceptual example: ```typescript databaseHooks: { session: { create: { async after(session, context) { // Determine the login method (reuse your customResolveMethod logic) const lastUsedLoginMethod = customResolveMethod(context); if (lastUsedLoginMethod && session?.user?.id) { await db.user.update({ where: { id: session.user.id }, data: { lastLoginMethod: lastUsedLoginMethod }, }); } }, }, }, } ``` This ensures the field is updated every time a new session is created (i.e., on every login), keeping the database in sync with the user's most recent authentication method. You can adapt this pattern to your ORM and authentication flow ([related workaround for similar issues](https://github.com/better-auth/better-auth/issues/2713)). If you want the plugin to handle this automatically, it would need to be enhanced to include such a hook. Let me know if you need a more detailed example for your specific stack! <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/ff811d43-9232-4e15-b0fa-93ca8fc69984?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/ff811d43-9232-4e15-b0fa-93ca8fc69984?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/ff811d43-9232-4e15-b0fa-93ca8fc69984?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/ff811d43-9232-4e15-b0fa-93ca8fc69984?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/ff811d43-9232-4e15-b0fa-93ca8fc69984?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/ff811d43-9232-4e15-b0fa-93ca8fc69984?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/ff811d43-9232-4e15-b0fa-93ca8fc69984?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/4627)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1915