[PR #4572] [MERGED] fix(lastLoginMethod): inherit cross-subdomain cookie settings in lastLoginMethod plugin #22356

Closed
opened 2026-04-15 20:59:19 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4572
Author: @lumpinif
Created: 9/10/2025
Status: Merged
Merged: 9/11/2025
Merged by: @himself65

Base: canaryHead: fix-last-login-method-cross-subdomain-clean


📝 Commits (2)

  • 54cba38 fix: inherit cross-subdomain cookie settings in lastLoginMethod plugin
  • 9f0887f fix(lastLoginMethod) inherit cross-domain cookie settings in lastLoginMethod plugin

📊 Changes

3 files changed (+249 additions, -6 deletions)

View changed files

📝 docs/content/docs/plugins/last-login-method.mdx (+10 -0)
packages/better-auth/src/plugins/last-login-method/custom-prefix.test.ts (+225 -0)
📝 packages/better-auth/src/plugins/last-login-method/index.ts (+14 -6)

📄 Description

Summary

The lastLoginMethod plugin was using hardcoded cookie attributes, bypassing Better Auth's centralized cookie system. This prevented the plugin from inheriting cross-subdomain cookie settings when crossSubDomainCookies.enabled was true.

Changes

  • Replace hardcoded cookie attributes with proper inheritance from crossSubDomainCookies configuration
  • Add comprehensive tests for cross-subdomain functionality with custom prefixes
  • Update documentation with cross-subdomain usage examples

Test Plan

  • All existing tests pass
  • Added 4 new test cases covering:
    • Default cookie name with custom prefix
    • Custom cookie name with prefix
    • Custom cookie name regardless of prefix
    • Cross-subdomain functionality with custom prefix
  • Manual testing of cross-subdomain cookie inheritance

Before/After

Before: The plugin used hardcoded cookie attributes, ignoring global cross-subdomain settings

ctx.setCookie(config.cookieName, lastUsedLoginMethod, {
  maxAge: config.maxAge,
  secure: false,
  httpOnly: false,
  path: "/",
});

After: The plugin inherits cross-subdomain settings from Better Auth configuration
const cookieAttributes = {
  maxAge: config.maxAge,
  secure: false,
  httpOnly: false,
  path: "/",
  // Inherit cross-subdomain domain if enabled
  ...(ctx.context.options.advanced?.crossSubDomainCookies?.enabled
    ? {
        domain:
          ctx.context.options.advanced.crossSubDomainCookies.domain ||
          (ctx.context.options.baseURL
            ? new URL(ctx.context.options.baseURL).hostname
            : undefined),
      }
    : {}),
};

Related Issues

Fixes cross-subdomain cookie inheritance for lastLoginMethod plugin to align with Better Auth's centralized cookie system.


Summary by cubic

Fixes lastLoginMethod to inherit cross-subdomain cookie settings so the last used login method works across subdomains when enabled. Adds tests and docs for custom cookie names/prefixes and domain handling.

  • Bug Fixes
    • Replace hardcoded cookie attributes with values derived from advanced.crossSubDomainCookies.
    • Set cookie Domain from configured domain or baseURL hostname when enabled, and respect the exact configured cookieName (not affected by cookiePrefix).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/4572 **Author:** [@lumpinif](https://github.com/lumpinif) **Created:** 9/10/2025 **Status:** ✅ Merged **Merged:** 9/11/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix-last-login-method-cross-subdomain-clean` --- ### 📝 Commits (2) - [`54cba38`](https://github.com/better-auth/better-auth/commit/54cba3858714313d2688156ed24030b7c406070a) fix: inherit cross-subdomain cookie settings in lastLoginMethod plugin - [`9f0887f`](https://github.com/better-auth/better-auth/commit/9f0887f9a644582e9680c202ef178b22bf09cb57) fix(lastLoginMethod) inherit cross-domain cookie settings in lastLoginMethod plugin ### 📊 Changes **3 files changed** (+249 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/last-login-method.mdx` (+10 -0) ➕ `packages/better-auth/src/plugins/last-login-method/custom-prefix.test.ts` (+225 -0) 📝 `packages/better-auth/src/plugins/last-login-method/index.ts` (+14 -6) </details> ### 📄 Description ## Summary The lastLoginMethod plugin was using hardcoded cookie attributes, bypassing Better Auth's centralized cookie system. This prevented the plugin from inheriting cross-subdomain cookie settings when `crossSubDomainCookies.enabled` was true. ## Changes - Replace hardcoded cookie attributes with proper inheritance from `crossSubDomainCookies` configuration - Add comprehensive tests for cross-subdomain functionality with custom prefixes - Update documentation with cross-subdomain usage examples ## Test Plan - [x] All existing tests pass - [x] Added 4 new test cases covering: - Default cookie name with custom prefix - Custom cookie name with prefix - Custom cookie name regardless of prefix - Cross-subdomain functionality with custom prefix - [x] Manual testing of cross-subdomain cookie inheritance ## Before/After **Before:** The plugin used hardcoded cookie attributes, ignoring global cross-subdomain settings ```typescript ctx.setCookie(config.cookieName, lastUsedLoginMethod, { maxAge: config.maxAge, secure: false, httpOnly: false, path: "/", }); After: The plugin inherits cross-subdomain settings from Better Auth configuration const cookieAttributes = { maxAge: config.maxAge, secure: false, httpOnly: false, path: "/", // Inherit cross-subdomain domain if enabled ...(ctx.context.options.advanced?.crossSubDomainCookies?.enabled ? { domain: ctx.context.options.advanced.crossSubDomainCookies.domain || (ctx.context.options.baseURL ? new URL(ctx.context.options.baseURL).hostname : undefined), } : {}), }; ``` Related Issues Fixes cross-subdomain cookie inheritance for lastLoginMethod plugin to align with Better Auth's centralized cookie system. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes lastLoginMethod to inherit cross-subdomain cookie settings so the last used login method works across subdomains when enabled. Adds tests and docs for custom cookie names/prefixes and domain handling. - **Bug Fixes** - Replace hardcoded cookie attributes with values derived from advanced.crossSubDomainCookies. - Set cookie Domain from configured domain or baseURL hostname when enabled, and respect the exact configured cookieName (not affected by cookiePrefix). <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 20:59:19 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#22356