|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { getTestInstance } from "../../test-utils/test-instance"; |
| 3 | +import { lastLoginMethod } from "."; |
| 4 | +import { lastLoginMethodClient } from "./client"; |
| 5 | +import { parseCookies } from "../../cookies"; |
| 6 | + |
| 7 | +describe("lastLoginMethod custom cookie prefix", async () => { |
| 8 | +it("should work with default cookie name regardless of custom prefix", async () => { |
| 9 | +const { client, cookieSetter, testUser } = await getTestInstance( |
| 10 | +{ |
| 11 | +advanced: { |
| 12 | +cookiePrefix: "custom-auth", |
| 13 | +}, |
| 14 | +plugins: [lastLoginMethod()], |
| 15 | +}, |
| 16 | +{ |
| 17 | +clientOptions: { |
| 18 | +plugins: [lastLoginMethodClient()], |
| 19 | +}, |
| 20 | +}, |
| 21 | +); |
| 22 | + |
| 23 | +const headers = new Headers(); |
| 24 | +await client.signIn.email( |
| 25 | +{ |
| 26 | +email: testUser.email, |
| 27 | +password: testUser.password, |
| 28 | +}, |
| 29 | +{ |
| 30 | +onSuccess(context) { |
| 31 | +cookieSetter(headers)(context); |
| 32 | +}, |
| 33 | +}, |
| 34 | +); |
| 35 | +const cookies = parseCookies(headers.get("cookie") || ""); |
| 36 | +// Uses exact cookie name from config, not affected by cookiePrefix |
| 37 | +expect(cookies.get("better-auth.last_used_login_method")).toBe("email"); |
| 38 | +}); |
| 39 | + |
| 40 | +it("should work with custom cookie name and prefix", async () => { |
| 41 | +const { client, cookieSetter, testUser } = await getTestInstance( |
| 42 | +{ |
| 43 | +advanced: { |
| 44 | +cookiePrefix: "my-app", |
| 45 | +}, |
| 46 | +plugins: [lastLoginMethod({ cookieName: "my-app.last_method" })], |
| 47 | +}, |
| 48 | +{ |
| 49 | +clientOptions: { |
| 50 | +plugins: [lastLoginMethodClient()], |
| 51 | +}, |
| 52 | +}, |
| 53 | +); |
| 54 | + |
| 55 | +const headers = new Headers(); |
| 56 | +await client.signIn.email( |
| 57 | +{ |
| 58 | +email: testUser.email, |
| 59 | +password: testUser.password, |
| 60 | +}, |
| 61 | +{ |
| 62 | +onSuccess(context) { |
| 63 | +cookieSetter(headers)(context); |
| 64 | +}, |
| 65 | +}, |
| 66 | +); |
| 67 | +const cookies = parseCookies(headers.get("cookie") || ""); |
| 68 | +expect(cookies.get("my-app.last_method")).toBe("email"); |
| 69 | +}); |
| 70 | + |
| 71 | +it("should work with custom cookie name regardless of prefix", async () => { |
| 72 | +const { client, cookieSetter, testUser } = await getTestInstance( |
| 73 | +{ |
| 74 | +advanced: { |
| 75 | +cookiePrefix: "my-app", |
| 76 | +}, |
| 77 | +plugins: [lastLoginMethod({ cookieName: "last_login_method" })], |
| 78 | +}, |
| 79 | +{ |
| 80 | +clientOptions: { |
| 81 | +plugins: [lastLoginMethodClient()], |
| 82 | +}, |
| 83 | +}, |
| 84 | +); |
| 85 | + |
| 86 | +const headers = new Headers(); |
| 87 | +await client.signIn.email( |
| 88 | +{ |
| 89 | +email: testUser.email, |
| 90 | +password: testUser.password, |
| 91 | +}, |
| 92 | +{ |
| 93 | +onSuccess(context) { |
| 94 | +cookieSetter(headers)(context); |
| 95 | +}, |
| 96 | +}, |
| 97 | +); |
| 98 | +const cookies = parseCookies(headers.get("cookie") || ""); |
| 99 | +// Uses exact cookie name from config, not affected by cookiePrefix |
| 100 | +expect(cookies.get("last_login_method")).toBe("email"); |
| 101 | +}); |
| 102 | + |
| 103 | +it("should work with cross-subdomain and custom prefix", async () => { |
| 104 | +const { client, testUser } = await getTestInstance( |
| 105 | +{ |
| 106 | +baseURL: "https://auth.example.com", |
| 107 | +advanced: { |
| 108 | +cookiePrefix: "custom-auth", |
| 109 | +crossSubDomainCookies: { |
| 110 | +enabled: true, |
| 111 | +domain: "example.com", |
| 112 | +}, |
| 113 | +}, |
| 114 | +plugins: [lastLoginMethod()], |
| 115 | +}, |
| 116 | +{ |
| 117 | +clientOptions: { |
| 118 | +plugins: [lastLoginMethodClient()], |
| 119 | +}, |
| 120 | +}, |
| 121 | +); |
| 122 | + |
| 123 | +await client.signIn.email( |
| 124 | +{ |
| 125 | +email: testUser.email, |
| 126 | +password: testUser.password, |
| 127 | +}, |
| 128 | +{ |
| 129 | +onResponse(context) { |
| 130 | +const setCookie = context.response.headers.get("set-cookie"); |
| 131 | +expect(setCookie).toContain("Domain=example.com"); |
| 132 | +expect(setCookie).toContain("SameSite=Lax"); |
| 133 | +// Uses exact cookie name from config, not affected by cookiePrefix |
| 134 | +expect(setCookie).toContain( |
| 135 | +"better-auth.last_used_login_method=email", |
| 136 | +); |
| 137 | +}, |
| 138 | +}, |
| 139 | +); |
| 140 | +}); |
| 141 | + |
| 142 | +it("should work with cross-origin cookies", async () => { |
| 143 | +const { client, testUser } = await getTestInstance( |
| 144 | +{ |
| 145 | +baseURL: "https://api.example.com", |
| 146 | +advanced: { |
| 147 | +crossOriginCookies: { |
| 148 | +enabled: true, |
| 149 | +}, |
| 150 | +defaultCookieAttributes: { |
| 151 | +sameSite: "none", |
| 152 | +secure: true, |
| 153 | +}, |
| 154 | +}, |
| 155 | +plugins: [lastLoginMethod()], |
| 156 | +}, |
| 157 | +{ |
| 158 | +clientOptions: { |
| 159 | +plugins: [lastLoginMethodClient()], |
| 160 | +}, |
| 161 | +}, |
| 162 | +); |
| 163 | + |
| 164 | +await client.signIn.email( |
| 165 | +{ |
| 166 | +email: testUser.email, |
| 167 | +password: testUser.password, |
| 168 | +}, |
| 169 | +{ |
| 170 | +onResponse(context) { |
| 171 | +const setCookie = context.response.headers.get("set-cookie"); |
| 172 | +expect(setCookie).toContain("SameSite=None"); |
| 173 | +expect(setCookie).toContain("Secure"); |
| 174 | +// Should not contain Domain attribute for cross-origin |
| 175 | +expect(setCookie).not.toContain("Domain="); |
| 176 | +expect(setCookie).toContain( |
| 177 | +"better-auth.last_used_login_method=email", |
| 178 | +); |
| 179 | +}, |
| 180 | +}, |
| 181 | +); |
| 182 | +}); |
| 183 | + |
| 184 | +it("should handle cross-origin on localhost for development", async () => { |
| 185 | +const { client, testUser } = await getTestInstance( |
| 186 | +{ |
| 187 | +baseURL: "http://localhost:3000", |
| 188 | +advanced: { |
| 189 | +crossOriginCookies: { |
| 190 | +enabled: true, |
| 191 | +allowLocalhostUnsecure: true, |
| 192 | +}, |
| 193 | +defaultCookieAttributes: { |
| 194 | +sameSite: "none", |
| 195 | +secure: false, |
| 196 | +}, |
| 197 | +}, |
| 198 | +plugins: [lastLoginMethod()], |
| 199 | +}, |
| 200 | +{ |
| 201 | +clientOptions: { |
| 202 | +plugins: [lastLoginMethodClient()], |
| 203 | +}, |
| 204 | +}, |
| 205 | +); |
| 206 | + |
| 207 | +await client.signIn.email( |
| 208 | +{ |
| 209 | +email: testUser.email, |
| 210 | +password: testUser.password, |
| 211 | +}, |
| 212 | +{ |
| 213 | +onResponse(context) { |
| 214 | +const setCookie = context.response.headers.get("set-cookie"); |
| 215 | +expect(setCookie).toContain("SameSite=None"); |
| 216 | +// Should not contain Secure on localhost when allowLocalhostUnsecure is true |
| 217 | +expect(setCookie).not.toContain("Secure"); |
| 218 | +expect(setCookie).toContain( |
| 219 | +"better-auth.last_used_login_method=email", |
| 220 | +); |
| 221 | +}, |
| 222 | +}, |
| 223 | +); |
| 224 | +}); |
| 225 | +}); |
0 commit comments