Package Exports
- remember-last-method-plugin
Readme
Remember Last Auth Method Plugin
This plugin allows you to remember the last authentication provider a user selected (e.g., Google, GitHub, etc.) in your app using the better-auth library. It stores the last used provider in the browser's localStorage and retrieves it when needed, enabling a smoother user experience.
Installation
You can install the plugin using pnpm, npm, or yarn.
npm i remember-last-method-pluginUsage
1. Register the Plugin
Import and register the plugin with your better-auth client setup:
import { createAuthClient } from "better-auth/client";
import { rememberLastMethodClientPlugin } from "remember-last-method-plugin";
const authClient = createAuthClient({
plugins: [
// ...other options
rememberLastMethodClientPlugin(),
],
});2. Saving the Last Used Provider
When a user selects an authentication provider (e.g., clicks a login/sign-up button), call the saveProvider action:
await authClient.saveProvider("email");Replace "google" with the provider's name (e.g., "github", "email", etc.).
3. Retrieving the Last Used Provider
To get the last used provider (e.g., to pre-select a button):
const { data, error } = await authClient.getProvider();API
The plugin exposes two actions:
saveProvider(provider: string): Saves the provider name to localStorage.getProvider(): Retrieves the last saved provider name from localStorage.
Both actions return an object with { data, error }.
Example
// Save provider after user selects it
await authClient.saveProvider("github");
// Get last provider on page load
const { data: lastProvider } = await authClient.getProvider();Notes
- This plugin only works in the browser (client-side), as it uses
localStorage. - Also this plugin only uses a client plugin no need to use or register a server plugin.