Skip to content

Beautiful Open-Source Form Builder

refactor: update Sentry user context handling (#1080)

Chirag Chhatrala013fa0bd8b2e
diff --git a/client/composables/useAuthFlow.js b/client/composables/useAuthFlow.js
index 7d98368f3..83a93d26d 100644
--- a/client/composables/useAuthFlow.js
+++ b/client/composables/useAuthFlow.js
@@ -16,8 +16,8 @@ export const useIsAuthenticated = () => {
}
/**
- * Initialize service clients without requiring Vue Query context
- * Safe to call from middleware or anywhere outside setup context
+ * Initialize vendor clients (Amplitude, Crisp) from the current user.
+ * Safe to call from middleware or anywhere outside setup context.
*/
export const initServiceClients = (userData) => {
if (import.meta.server) return
@@ -62,8 +62,9 @@ export const useAuthFlow = () => {
// 1. Set token in store
authStore.setToken(tokenData.token, tokenData.expires_in)
- // 2. Initialize service clients if user data is provided
+ // 2. Sync user context if user data is provided
if (tokenData.user) {
+ authStore.updateUser(tokenData.user)
initServiceClients(tokenData.user)
}
@@ -182,7 +183,7 @@ export const useAuthFlow = () => {
authStore.clearToken()
// Clear user data
- authStore.user = null
+ authStore.updateUser(null)
// Navigate to login page
await router.push({ name: 'login' })
@@ -228,4 +229,4 @@ export const useAuthFlow = () => {
handleTwoFactorCancel,
handleTwoFactorError,
}
-}
\ No newline at end of file
+}
diff --git a/client/middleware/01.check-auth.global.js b/client/middleware/01.check-auth.global.js
index e13b295f1..1900a5dc1 100644
--- a/client/middleware/01.check-auth.global.js
+++ b/client/middleware/01.check-auth.global.js
@@ -47,6 +47,7 @@ export default defineNuxtRouteMiddleware(async (_to, _from) => {
if (error?.status === 401) {
if (import.meta.client) {
authStore.clearToken()
+ authStore.updateUser(null)
queryClient.clear()
}
}
@@ -56,6 +57,7 @@ export default defineNuxtRouteMiddleware(async (_to, _from) => {
// Initialize service clients on client side (no-op on server)
if (userData) {
+ authStore.updateUser(userData)
initServiceClients(userData)
}
})
diff --git a/client/sentry.client.config.ts b/client/sentry.client.config.ts
index ab0bb7254..c74802410 100644
--- a/client/sentry.client.config.ts
+++ b/client/sentry.client.config.ts
@@ -37,15 +37,12 @@ Sentry.init({
}
}
- const { isAuthenticated } = useIsAuthenticated()
- const { user } = useAuth()
- const { data: userData } = user()
-
- if (isAuthenticated.value) {
- const userValue = userData.value as { id?: string; email?: string } | null
+ // Avoid useAuth()/vue-query here: beforeSend runs outside Vue injection context.
+ const authStore = useAuthStore()
+ if (authStore.token && authStore.user) {
Sentry.setUser({
- id: userValue?.id,
- email: userValue?.email
+ id: authStore.user?.id,
+ email: authStore.user?.email
})
}
return event
diff --git a/client/stores/auth.js b/client/stores/auth.js
index b1be15c52..15b02816d 100644
--- a/client/stores/auth.js
+++ b/client/stores/auth.js
@@ -1,6 +1,5 @@
import { defineStore } from "pinia"
import { authApi } from "~/api"
-import { initServiceClients } from '~/composables/useAuthFlow'
const AUTH_COOKIE_NAME = "opnform_token"
const LEGACY_AUTH_COOKIE_NAME = "token"
@@ -79,19 +78,11 @@ export const useAuthStore = defineStore("auth", {
},
setUser(user) {
- if (!user) {
- console.error("No user, logging out.")
- // When logging out due to no user, clear the token with maxAge 0
- this.setToken(null, 0)
- }
-
this.user = user
- initServiceClients(user)
},
updateUser(payload) {
this.user = payload
- initServiceClients(payload)
},
logout() {