Last login method plugin overview
The last login method plugin tracks the most recent authentication method used by users (email, OAuth providers, etc.). It enables displaying helpful indicators on login pages such as 'Last signed in with Google' or prioritizing certain login methods based on user preferences.
Install last login method plugin on server
Import lastLoginMethod from 'better-auth/plugins' and add it to the plugins array in betterAuth configuration.
Install last login method client plugin
Import lastLoginMethodClient from 'better-auth/client/plugins' and add it to the plugins array in createAuthClient configuration.
Client method getLastUsedLoginMethod
The authClient.getLastUsedLoginMethod() method returns the last used login method as a string, such as 'google', 'email', 'github', or other provider names.
Client method isLastUsedLoginMethod
The authClient.isLastUsedLoginMethod(method) method checks if a specific authentication method was the last one used, returning a boolean.
Client method clearLastUsedLoginMethod
The authClient.clearLastUsedLoginMethod() method clears the stored last used login method.
Last login method database persistence
By default, the last login method is stored only in cookies. Enable database storage by setting storeInDatabase to true in the plugin configuration. The plugin automatically adds a lastLoginMethod field to the user table.
Enable database migration for last login method
When storeInDatabase is enabled, run 'npx auth@latest migrate' or 'npx auth@latest generate' to apply the database schema changes that add the lastLoginMethod field to the user table.
Access lastLoginMethod from session server-side
When database storage is enabled, access the lastLoginMethod field from user objects via auth.api.getSession({ headers }) on the server side, which returns values like 'google', 'email', etc.
Access lastLoginMethod from session client-side
When database storage is enabled, access the lastLoginMethod field from user objects via authClient.useSession(), which returns the last authentication method used.
User table schema for last login method
When storeInDatabase is enabled, the plugin adds a field to the user table with the following specification: name='lastLoginMethod', type='string', description='The last authentication method used by the user', isOptional=true.
Custom schema field name for last login method
Configure a custom database field name by setting lastLoginMethod in the schema.user object when storeInDatabase is enabled. For example: lastLoginMethod({ storeInDatabase: true, schema: { user: { lastLoginMethod: 'last_auth_method' } } }).
Last login method plugin server configuration options
Server-side configuration accepts the following options: cookieName (string, default 'better-auth.last_used_login_method'), maxAge (number in seconds, default 2592000 for 30 days), storeInDatabase (boolean, default false), customResolveMethod (function), beforeStoreCookie (async hook function), and schema (object for custom field names).
Last login method maxAge configuration
The maxAge option specifies cookie expiration time in seconds. Default is 2592000 (30 days).
Last login method storeInDatabase configuration
The storeInDatabase option (boolean, default false) determines whether to store the last login method in the database. When enabled, adds a lastLoginMethod field to the user table.
Last login method customResolveMethod configuration
The customResolveMethod option accepts a function with signature (ctx: GenericEndpointContext) => string | null. It allows custom logic to determine the login method from the request context. Return null to use default resolution logic. Useful for custom OAuth providers or authentication flows.
Last login method beforeStoreCookie configuration
The beforeStoreCookie option accepts an async hook function with signature (ctx: GenericEndpointContext, lastUsedLoginMethod: string) => Promise<boolean> | boolean. It runs before storing the last login method cookie. Return true to allow the cookie to be set, false to prevent it. Useful for GDPR compliance. If the function throws an error, the cookie will not be set (error is logged but doesn't break authentication).
Last login method schema configuration
The schema option is an object that customizes database field names when storeInDatabase is enabled. It allows mapping the lastLoginMethod field to a custom column name via schema.user.lastLoginMethod.
Store only in database without cookie
To store the last login method only in the database without setting a cookie, combine storeInDatabase: true with beforeStoreCookie: () => false.
Last login method client plugin configuration options
Client-side configuration accepts: cookieName (string, default 'better-auth.last_used_login_method') which must match the server-side cookieName, and domain (string) which specifies the domain to use when clearing the cookie, required for crossSubDomainCookies configuration.
Last login method client cookieName option
The cookieName option in the client plugin specifies the name of the cookie to read the last login method from. Must match the server-side cookieName configuration. Default is 'better-auth.last_used_login_method'.
Last login method client domain option
The domain option in the client plugin specifies the domain to use when clearing the cookie. Required when using crossSubDomainCookies so the client can properly expire the cookie set by the server. Should match the domain value in the server's crossSubDomainCookies configuration.
Default last login method detection
By default, the plugin tracks these authentication methods: 'email' for email authentication, provider IDs like 'google', 'github', 'discord' for OAuth providers, provider ID from URL path for OAuth callbacks, and tracked the same as sign in methods for sign up methods. The plugin automatically detects the method from endpoints /callback/:id, /sign-in/email, and /sign-up/email.
Last login method cross-domain cookie inheritance
The plugin automatically inherits cookie settings from Better Auth's centralized cookie system. When you enable crossSubDomainCookies or crossOriginCookies in Better Auth config, the plugin will automatically use the same domain, secure, and sameSite settings as your session cookies, ensuring consistent behavior across your application.
Last login method domain requirement for crossSubDomainCookies
When using crossSubDomainCookies, you must pass the domain option to lastLoginMethodClient() so that clearLastUsedLoginMethod() can properly expire the cookie. Without it, browsers will silently ignore the clear request because the domain doesn't match.
Last login method GDPR compliance
The last login method cookie is considered a non-essential cookie under GDPR regulations. To comply with GDPR and similar privacy laws, you should only store this cookie if the user has given explicit consent.
Custom provider tracking with customResolveMethod
For custom OAuth providers or authentication methods, use the customResolveMethod option to track custom authentication flows. For example, you can track SAML providers, magic link authentication, or phone authentication by returning the method name from the context path.
Last login method with Expo
When using Better Auth with Expo, import the client plugin from '@better-auth/expo/plugins' rather than from 'better-auth/plugins/client' to ensure the last login method is stored correctly using the configured storage. In Expo-only apps where browser support isn't needed, you can omit the server plugin and rely solely on the client plugin.
Last login method Expo client configuration
When using Expo, configure the lastLoginMethodClient with storagePrefix and storage options to match your Expo client configuration. Example: lastLoginMethodClient({ storagePrefix: 'myapp', storage: SecureStorage })