From 0831632074035ff31b4b0d35d96b2efe5b2caad7 Mon Sep 17 00:00:00 2001 From: rovertrack <160643895+rovertrack@users.noreply.github.com> Date: Mon, 17 Nov 2025 03:37:50 +0530 Subject: [PATCH] feat(last-login-method): add support for 'siwe' as a last login method and added tests (#6027) --- .../src/plugins/last-login-method/index.ts | 1 + .../last-login-method.test.ts | 85 ++++++++++++++++++- 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/packages/better-auth/src/plugins/last-login-method/index.ts b/packages/better-auth/src/plugins/last-login-method/index.ts index 1d1b2e1e84..05cc61db43 100644 --- a/packages/better-auth/src/plugins/last-login-method/index.ts +++ b/packages/better-auth/src/plugins/last-login-method/index.ts @@ -63,6 +63,7 @@ export const lastLoginMethod = ( ctx.params?.id || ctx.params?.providerId || ctx.path.split("/").pop() ); } + if (ctx.path.includes("siwe")) return "siwe"; return null; }; diff --git a/packages/better-auth/src/plugins/last-login-method/last-login-method.test.ts b/packages/better-auth/src/plugins/last-login-method/last-login-method.test.ts index 33eed6439e..9cb8493c44 100644 --- a/packages/better-auth/src/plugins/last-login-method/last-login-method.test.ts +++ b/packages/better-auth/src/plugins/last-login-method/last-login-method.test.ts @@ -8,6 +8,8 @@ import { getTestInstance } from "../../test-utils/test-instance"; import { DEFAULT_SECRET } from "../../utils/constants"; import { genericOAuthClient } from "../generic-oauth/client"; import { genericOAuth } from "../generic-oauth/index"; +import { siwe } from "../siwe"; +import { siweClient } from "../siwe/client"; import { lastLoginMethod } from "."; import { lastLoginMethodClient } from "./client"; @@ -63,11 +65,24 @@ afterAll(() => server.close()); describe("lastLoginMethod", async () => { const { client, cookieSetter, testUser } = await getTestInstance( { - plugins: [lastLoginMethod()], + plugins: [ + lastLoginMethod(), + siwe({ + domain: "example.com", + async getNonce() { + return "A1b2C3d4E5f6G7h8J"; + }, + async verifyMessage({ message, signature }) { + return ( + signature === "valid_signature" && message === "valid_message" + ); + }, + }), + ], }, { clientOptions: { - plugins: [lastLoginMethodClient()], + plugins: [lastLoginMethodClient(), siweClient()], }, }, ); @@ -89,6 +104,30 @@ describe("lastLoginMethod", async () => { expect(cookies.get("better-auth.last_used_login_method")).toBe("email"); }); + it("should set the last login method cookie for siwe", async () => { + const headers = new Headers(); + const walletAddress = "0x000000000000000000000000000000000000dEaD"; + const chainId = 1; + await client.siwe.nonce({ walletAddress, chainId }); + await client.siwe.verify( + { + message: "valid_message", + signature: "valid_signature", + walletAddress, + chainId, + email: "user@example.com", + }, + { + onSuccess(context) { + cookieSetter(headers)(context); + }, + }, + ); + const cookies = parseCookies(headers.get("cookie") || ""); + console.log("rans" + cookies); + expect(cookies.get("better-auth.last_used_login_method")).toBe("siwe"); + }); + it("should set the last login method in the database", async () => { const { client, auth } = await getTestInstance({ plugins: [lastLoginMethod({ storeInDatabase: true })], @@ -108,6 +147,48 @@ describe("lastLoginMethod", async () => { expect(session?.user.lastLoginMethod).toBe("email"); }); + it("should set the last login method for siwe in the database", async () => { + const walletAddress = "0x000000000000000000000000000000000000dEaD"; + const chainId = 1; + const { client, auth } = await getTestInstance( + { + plugins: [ + lastLoginMethod({ storeInDatabase: true }), + siwe({ + domain: "example.com", + async getNonce() { + return "A1b2C3d4E5f6G7h8J"; + }, + async verifyMessage({ message, signature }) { + return ( + signature === "valid_signature" && message === "valid_message" + ); + }, + }), + ], + }, + { + clientOptions: { + plugins: [siweClient()], + }, + }, + ); + await client.siwe.nonce({ walletAddress, chainId }); + const { data } = await client.siwe.verify({ + message: "valid_message", + signature: "valid_signature", + walletAddress, + chainId, + email: "user@example.com", + }); + const session = await auth.api.getSession({ + headers: new Headers({ + authorization: `Bearer ${data?.token}`, + }), + }); + expect(session?.user.lastLoginMethod).toBe("siwe"); + }); + it("should NOT set the last login method cookie on failed authentication", async () => { const headers = new Headers(); const response = await client.signIn.email(