1+ import {
2+ buildBetterAuthSessionTokenCookieHeader ,
3+ getBetterAuthSessionTokenCookieName ,
4+ } from "@follow/shared/auth-cookie"
15import { env } from "@follow/shared/env.desktop"
26import { createAuthRequestOriginHeaders , createDesktopAPIHeaders } from "@follow/utils/headers"
37import PKG from "@pkg"
@@ -10,8 +14,10 @@ import { WindowManager } from "~/manager/window"
1014import {
1115 buildManagedAuthCookieHeader ,
1216 buildManagedAuthCookieHeaderFromSetCookieHeader ,
17+ dedupeManagedAuthCookies ,
1318 getManagedAuthCookies ,
1419 persistManagedAuthCookiesFromSetCookieHeader ,
20+ removeManagedAuthCookies ,
1521} from "../../lib/auth-cookies"
1622import { getCliSessionToken , syncSessionToCliConfig } from "../../lib/cli-session-sync"
1723import { deleteNotificationsToken , updateNotificationsToken } from "../../lib/user"
@@ -40,27 +46,25 @@ export class AuthService extends IpcService {
4046 const url = new URL ( apiURL )
4147 const isSecure =
4248 url . protocol === "https:" || url . hostname === "localhost" || url . hostname === "127.0.0.1"
43- const isLocalhost = url . hostname === "localhost" || url . hostname === "127.0.0.1"
44- const cookieNames = [
45- BETTER_AUTH_COOKIE_NAME_SESSION_TOKEN ,
46- ...( isSecure && ! isLocalhost ? [ "__Secure-better-auth.session_token" ] : [ ] ) ,
47- ]
48-
49- await Promise . all (
50- cookieNames . map ( ( name ) =>
51- mainWindow . webContents . session . cookies . set ( {
52- url : apiURL ,
53- name,
54- value : token ,
55- ...( isLocalhost ? { } : { domain : url . hostname } ) ,
56- path : "/" ,
57- httpOnly : true ,
58- secure : isSecure ,
59- sameSite : "no_restriction" ,
60- expirationDate : new Date ( ) . setDate ( new Date ( ) . getDate ( ) + 30 ) ,
61- } ) ,
62- ) ,
63- )
49+ const cookieName = getBetterAuthSessionTokenCookieName ( apiURL )
50+ const cookieSession = mainWindow . webContents . session
51+
52+ await removeManagedAuthCookies ( {
53+ apiURL,
54+ session : cookieSession ,
55+ names : [ BETTER_AUTH_COOKIE_NAME_SESSION_TOKEN , "__Secure-better-auth.session_token" ] ,
56+ } )
57+ await cookieSession . cookies . set ( {
58+ url : apiURL ,
59+ name : cookieName ,
60+ value : token ,
61+ path : "/" ,
62+ httpOnly : true ,
63+ secure : isSecure ,
64+ sameSite : "no_restriction" ,
65+ expirationDate : Math . floor ( Date . now ( ) / 1000 ) + 60 * 60 * 24 * 30 ,
66+ } )
67+ await dedupeManagedAuthCookies ( { apiURL, session : cookieSession } )
6468 }
6569
6670 private async clearSessionToken ( ) : Promise < void > {
@@ -72,11 +76,7 @@ export class AuthService extends IpcService {
7276
7377 const { session } = mainWindow . webContents
7478 const apiURL = env . VITE_API_URL
75- await Promise . allSettled ( [
76- session . cookies . remove ( apiURL , BETTER_AUTH_COOKIE_NAME_SESSION_TOKEN ) ,
77- session . cookies . remove ( apiURL , "__Secure-better-auth.session_token" ) ,
78- session . cookies . remove ( apiURL , "better-auth.last_used_login_method" ) ,
79- ] )
79+ await removeManagedAuthCookies ( { apiURL, session } )
8080 }
8181
8282 private async requestCredentialAuth (
@@ -171,7 +171,7 @@ export class AuthService extends IpcService {
171171 headers : this . getAuthRequestHeaders (
172172 token
173173 ? {
174- Cookie : `__Secure-better-auth.session_token= ${ token } ; better-auth.session_token= ${ token } ` ,
174+ Cookie : buildBetterAuthSessionTokenCookieHeader ( env . VITE_API_URL , token ) ,
175175 }
176176 : undefined ,
177177 ) ,
0 commit comments