Skip to content

Commit cd50c54

Browse files
add session cookie auth on cloud dist (#6295)
## Summary Implemented cookie-based session authentication for cloud distribution, replacing service worker approach with extension-based lifecycle hooks. ## Changes - **What**: Added session cookie management via [extension hooks](https://docs.comfy.org/comfyui/extensions) for login, token refresh, and logout events - **Architecture**: DDD-compliant structure with platform layer (`src/platform/auth/session/`) and cloud-gated extension - **New Extension Hooks**: `onAuthTokenRefreshed()` and `onAuthUserLogout()` in [ComfyExtension interface](src/types/comfy.ts:220-232) ```mermaid sequenceDiagram participant User participant Firebase participant Extension participant Backend User->>Firebase: Login Firebase->>Extension: onAuthUserResolved Extension->>Backend: POST /auth/session (with JWT) Backend-->>Extension: Set-Cookie Firebase->>Firebase: Token Refresh Firebase->>Extension: onAuthTokenRefreshed Extension->>Backend: POST /auth/session (with new JWT) Backend-->>Extension: Update Cookie User->>Firebase: Logout Firebase->>Extension: onAuthUserLogout (user null) Extension->>Backend: DELETE /auth/session Backend-->>Extension: Clear Cookie ``` ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-6295-add-session-cookie-auth-on-cloud-dist-2986d73d365081868c56e5be1ad0d0d4) by [Unito](https://www.unito.io)
1 parent 3db1b15 commit cd50c54

File tree

8 files changed

+143
-2
lines changed

8 files changed

+143
-2
lines changed

src/composables/auth/useCurrentUser.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { whenever } from '@vueuse/core'
2-
import { computed } from 'vue'
2+
import { computed, watch } from 'vue'
33

44
import { useFirebaseAuthActions } from '@/composables/auth/useFirebaseAuthActions'
55
import { t } from '@/i18n'
@@ -37,6 +37,15 @@ export const useCurrentUser = () => {
3737
const onUserResolved = (callback: (user: AuthUserInfo) => void) =>
3838
whenever(resolvedUserInfo, callback, { immediate: true })
3939

40+
const onTokenRefreshed = (callback: () => void) =>
41+
whenever(() => authStore.tokenRefreshTrigger, callback)
42+
43+
const onUserLogout = (callback: () => void) => {
44+
watch(resolvedUserInfo, (user) => {
45+
if (!user) callback()
46+
})
47+
}
48+
4049
const userDisplayName = computed(() => {
4150
if (isApiKeyLogin.value) {
4251
return apiKeyStore.currentUser?.name
@@ -133,6 +142,8 @@ export const useCurrentUser = () => {
133142
handleSignOut,
134143
handleSignIn,
135144
handleDeleteAccount,
136-
onUserResolved
145+
onUserResolved,
146+
onTokenRefreshed,
147+
onUserLogout
137148
}
138149
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useSessionCookie } from '@/platform/auth/session/useSessionCookie'
2+
import { useExtensionService } from '@/services/extensionService'
3+
4+
/**
5+
* Cloud-only extension that manages session cookies for authentication.
6+
* Creates session cookie on login, refreshes it when token refreshes, and deletes on logout.
7+
*/
8+
useExtensionService().registerExtension({
9+
name: 'Comfy.Cloud.SessionCookie',
10+
11+
onAuthUserResolved: async () => {
12+
const { createSession } = useSessionCookie()
13+
await createSession()
14+
},
15+
16+
onAuthTokenRefreshed: async () => {
17+
const { createSession } = useSessionCookie()
18+
await createSession()
19+
},
20+
21+
onAuthUserLogout: async () => {
22+
const { deleteSession } = useSessionCookie()
23+
await deleteSession()
24+
}
25+
})

src/extensions/core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import './widgetInputs'
2828
if (isCloud) {
2929
await import('./cloudRemoteConfig')
3030
await import('./cloudBadges')
31+
await import('./cloudSessionCookie')
3132

3233
if (window.__CONFIG__?.subscription_required) {
3334
await import('./cloudSubscription')
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { api } from '@/scripts/api'
2+
import { isCloud } from '@/platform/distribution/types'
3+
import { useFirebaseAuthStore } from '@/stores/firebaseAuthStore'
4+
5+
/**
6+
* Session cookie management for cloud authentication.
7+
* Creates and deletes session cookies on the ComfyUI server.
8+
*/
9+
export const useSessionCookie = () => {
10+
/**
11+
* Creates or refreshes the session cookie.
12+
* Called after login and on token refresh.
13+
*/
14+
const createSession = async (): Promise<void> => {
15+
if (!isCloud) return
16+
17+
const authStore = useFirebaseAuthStore()
18+
const authHeader = await authStore.getAuthHeader()
19+
20+
if (!authHeader) {
21+
throw new Error('No auth header available for session creation')
22+
}
23+
24+
const response = await fetch(api.apiURL('/auth/session'), {
25+
method: 'POST',
26+
credentials: 'include',
27+
headers: {
28+
...authHeader,
29+
'Content-Type': 'application/json'
30+
}
31+
})
32+
33+
if (!response.ok) {
34+
const errorData = await response.json().catch(() => ({}))
35+
throw new Error(
36+
`Failed to create session: ${errorData.message || response.statusText}`
37+
)
38+
}
39+
}
40+
41+
/**
42+
* Deletes the session cookie.
43+
* Called on logout.
44+
*/
45+
const deleteSession = async (): Promise<void> => {
46+
if (!isCloud) return
47+
48+
const response = await fetch(api.apiURL('/auth/session'), {
49+
method: 'DELETE',
50+
credentials: 'include'
51+
})
52+
53+
if (!response.ok) {
54+
const errorData = await response.json().catch(() => ({}))
55+
throw new Error(
56+
`Failed to delete session: ${errorData.message || response.statusText}`
57+
)
58+
}
59+
}
60+
61+
return {
62+
createSession,
63+
deleteSession
64+
}
65+
}

src/services/extensionService.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ export const useExtensionService = () => {
8080
void extension.onAuthUserResolved?.(user, app)
8181
})
8282
}
83+
84+
if (extension.onAuthTokenRefreshed) {
85+
const { onTokenRefreshed } = useCurrentUser()
86+
onTokenRefreshed(() => {
87+
void extension.onAuthTokenRefreshed?.()
88+
})
89+
}
90+
91+
if (extension.onAuthUserLogout) {
92+
const { onUserLogout } = useCurrentUser()
93+
onUserLogout(() => {
94+
void extension.onAuthUserLogout?.()
95+
})
96+
}
8397
}
8498

8599
/**

src/stores/firebaseAuthStore.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
deleteUser,
99
getAdditionalUserInfo,
1010
onAuthStateChanged,
11+
onIdTokenChanged,
1112
sendPasswordResetEmail,
1213
setPersistence,
1314
signInWithEmailAndPassword,
@@ -61,6 +62,9 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
6162
const balance = ref<GetCustomerBalanceResponse | null>(null)
6263
const lastBalanceUpdateTime = ref<Date | null>(null)
6364

65+
// Token refresh trigger - increments when token is refreshed
66+
const tokenRefreshTrigger = ref(0)
67+
6468
// Providers
6569
const googleProvider = new GoogleAuthProvider()
6670
googleProvider.addScope('email')
@@ -95,6 +99,13 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
9599
lastBalanceUpdateTime.value = null
96100
})
97101

102+
// Listen for token refresh events
103+
onIdTokenChanged(auth, (user) => {
104+
if (user && isCloud) {
105+
tokenRefreshTrigger.value++
106+
}
107+
})
108+
98109
const getIdToken = async (): Promise<string | undefined> => {
99110
if (!currentUser.value) return
100111
try {
@@ -421,6 +432,7 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => {
421432
balance,
422433
lastBalanceUpdateTime,
423434
isFetchingBalance,
435+
tokenRefreshTrigger,
424436

425437
// Getters
426438
isAuthenticated,

src/types/comfy.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,5 +219,17 @@ export interface ComfyExtension {
219219
*/
220220
onAuthUserResolved?(user: AuthUserInfo, app: ComfyApp): Promise<void> | void
221221

222+
/**
223+
* Fired whenever the auth token is refreshed.
224+
* This is an experimental API and may be changed or removed in the future.
225+
*/
226+
onAuthTokenRefreshed?(): Promise<void> | void
227+
228+
/**
229+
* Fired when user logs out.
230+
* This is an experimental API and may be changed or removed in the future.
231+
*/
232+
onAuthUserLogout?(): Promise<void> | void
233+
222234
[key: string]: any
223235
}

tests-ui/tests/store/firebaseAuthStore.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ vi.mock('firebase/auth', async (importOriginal) => {
5656
createUserWithEmailAndPassword: vi.fn(),
5757
signOut: vi.fn(),
5858
onAuthStateChanged: vi.fn(),
59+
onIdTokenChanged: vi.fn(),
5960
signInWithPopup: vi.fn(),
6061
GoogleAuthProvider: class {
6162
addScope = vi.fn()

0 commit comments

Comments
 (0)