nyk commited on
Commit
e25fbbc
·
unverified ·
2 Parent(s): ecae4471a9fe50

Merge pull request #200 from builderz-labs/fix/198-auth-ux-redirect

Browse files
playwright.config.ts CHANGED
@@ -18,9 +18,18 @@ export default defineConfig({
18
  { name: 'chromium', use: { ...devices['Desktop Chrome'] } }
19
  ],
20
  webServer: {
21
- command: 'pnpm start',
22
  url: 'http://127.0.0.1:3005',
23
  reuseExistingServer: true,
24
- timeout: 30_000,
 
 
 
 
 
 
 
 
 
25
  }
26
  })
 
18
  { name: 'chromium', use: { ...devices['Desktop Chrome'] } }
19
  ],
20
  webServer: {
21
+ command: 'node .next/standalone/server.js',
22
  url: 'http://127.0.0.1:3005',
23
  reuseExistingServer: true,
24
+ timeout: 120_000,
25
+ env: {
26
+ ...process.env,
27
+ HOSTNAME: process.env.HOSTNAME || '127.0.0.1',
28
+ PORT: process.env.PORT || '3005',
29
+ MC_DISABLE_RATE_LIMIT: process.env.MC_DISABLE_RATE_LIMIT || '1',
30
+ API_KEY: process.env.API_KEY || 'test-api-key-e2e-12345',
31
+ AUTH_USER: process.env.AUTH_USER || 'testadmin',
32
+ AUTH_PASS: process.env.AUTH_PASS || 'testpass1234!',
33
+ },
34
  }
35
  })
src/app/[[...panel]]/page.tsx CHANGED
@@ -1,7 +1,7 @@
1
  'use client'
2
 
3
  import { useEffect, useState } from 'react'
4
- import { usePathname } from 'next/navigation'
5
  import { NavRail } from '@/components/layout/nav-rail'
6
  import { HeaderBar } from '@/components/layout/header-bar'
7
  import { LiveFeed } from '@/components/layout/live-feed'
@@ -42,6 +42,7 @@ import { useServerEvents } from '@/lib/use-server-events'
42
  import { useMissionControl } from '@/store'
43
 
44
  export default function Home() {
 
45
  const { connect } = useWebSocket()
46
  const { activeTab, setActiveTab, setCurrentUser, setDashboardMode, setGatewayAvailable, setSubscription, setUpdateAvailable, liveFeedOpen, toggleLiveFeed } = useMissionControl()
47
 
@@ -62,7 +63,13 @@ export default function Home() {
62
 
63
  // Fetch current user
64
  fetch('/api/auth/me')
65
- .then(res => res.ok ? res.json() : null)
 
 
 
 
 
 
66
  .then(data => { if (data?.user) setCurrentUser(data.user) })
67
  .catch(() => {})
68
 
@@ -120,7 +127,7 @@ export default function Home() {
120
  const wsUrl = explicitWsUrl || `${gatewayProto}://${gatewayHost}:${gatewayPort}`
121
  connect(wsUrl, wsToken)
122
  })
123
- }, [connect, setCurrentUser, setDashboardMode, setGatewayAvailable, setSubscription, setUpdateAvailable])
124
 
125
  if (!isClient) {
126
  return (
 
1
  'use client'
2
 
3
  import { useEffect, useState } from 'react'
4
+ import { usePathname, useRouter } from 'next/navigation'
5
  import { NavRail } from '@/components/layout/nav-rail'
6
  import { HeaderBar } from '@/components/layout/header-bar'
7
  import { LiveFeed } from '@/components/layout/live-feed'
 
42
  import { useMissionControl } from '@/store'
43
 
44
  export default function Home() {
45
+ const router = useRouter()
46
  const { connect } = useWebSocket()
47
  const { activeTab, setActiveTab, setCurrentUser, setDashboardMode, setGatewayAvailable, setSubscription, setUpdateAvailable, liveFeedOpen, toggleLiveFeed } = useMissionControl()
48
 
 
63
 
64
  // Fetch current user
65
  fetch('/api/auth/me')
66
+ .then(async (res) => {
67
+ if (res.ok) return res.json()
68
+ if (res.status === 401) {
69
+ router.replace(`/login?next=${encodeURIComponent(pathname)}`)
70
+ }
71
+ return null
72
+ })
73
  .then(data => { if (data?.user) setCurrentUser(data.user) })
74
  .catch(() => {})
75
 
 
127
  const wsUrl = explicitWsUrl || `${gatewayProto}://${gatewayHost}:${gatewayPort}`
128
  connect(wsUrl, wsToken)
129
  })
130
+ }, [connect, pathname, router, setCurrentUser, setDashboardMode, setGatewayAvailable, setSubscription, setUpdateAvailable])
131
 
132
  if (!isClient) {
133
  return (
src/components/panels/agent-squad-panel-phase3.tsx CHANGED
@@ -96,7 +96,14 @@ export function AgentSquadPanelPhase3() {
96
  setSyncToast(null)
97
  try {
98
  const response = await fetch('/api/agents/sync', { method: 'POST' })
 
 
 
 
99
  const data = await response.json()
 
 
 
100
  if (!response.ok) throw new Error(data.error || 'Sync failed')
101
  setSyncToast(`Synced ${data.synced} agents (${data.created} new, ${data.updated} updated)`)
102
  fetchAgents()
@@ -116,7 +123,17 @@ export function AgentSquadPanelPhase3() {
116
  if (agents.length === 0) setLoading(true)
117
 
118
  const response = await fetch('/api/agents')
119
- if (!response.ok) throw new Error('Failed to fetch agents')
 
 
 
 
 
 
 
 
 
 
120
 
121
  const data = await response.json()
122
  setAgents(data.agents || [])
 
96
  setSyncToast(null)
97
  try {
98
  const response = await fetch('/api/agents/sync', { method: 'POST' })
99
+ if (response.status === 401) {
100
+ window.location.assign('/login?next=%2Fagents')
101
+ return
102
+ }
103
  const data = await response.json()
104
+ if (response.status === 403) {
105
+ throw new Error('Admin access required for agent sync')
106
+ }
107
  if (!response.ok) throw new Error(data.error || 'Sync failed')
108
  setSyncToast(`Synced ${data.synced} agents (${data.created} new, ${data.updated} updated)`)
109
  fetchAgents()
 
123
  if (agents.length === 0) setLoading(true)
124
 
125
  const response = await fetch('/api/agents')
126
+ if (response.status === 401) {
127
+ window.location.assign('/login?next=%2Fagents')
128
+ return
129
+ }
130
+ if (response.status === 403) {
131
+ throw new Error('Access denied')
132
+ }
133
+ if (!response.ok) {
134
+ const data = await response.json().catch(() => ({}))
135
+ throw new Error(data.error || 'Failed to fetch agents')
136
+ }
137
 
138
  const data = await response.json()
139
  setAgents(data.agents || [])
src/components/panels/settings-panel.tsx CHANGED
@@ -45,12 +45,17 @@ export function SettingsPanel() {
45
  const fetchSettings = useCallback(async () => {
46
  try {
47
  const res = await fetch('/api/settings')
 
 
 
 
48
  if (res.status === 403) {
49
  setError('Admin access required')
50
  return
51
  }
52
  if (!res.ok) {
53
- setError('Failed to load settings')
 
54
  return
55
  }
56
  const data = await res.json()
 
45
  const fetchSettings = useCallback(async () => {
46
  try {
47
  const res = await fetch('/api/settings')
48
+ if (res.status === 401) {
49
+ window.location.assign('/login?next=%2Fsettings')
50
+ return
51
+ }
52
  if (res.status === 403) {
53
  setError('Admin access required')
54
  return
55
  }
56
  if (!res.ok) {
57
+ const data = await res.json().catch(() => ({}))
58
+ setError(data.error || 'Failed to load settings')
59
  return
60
  }
61
  const data = await res.json()
src/lib/websocket.ts CHANGED
@@ -59,6 +59,8 @@ export function useWebSocket() {
59
  const pingCounterRef = useRef<number>(0)
60
  const pingSentTimestamps = useRef<Map<string, number>>(new Map())
61
  const missedPongsRef = useRef<number>(0)
 
 
62
 
63
  const {
64
  connection,
@@ -116,6 +118,7 @@ export function useWebSocket() {
116
 
117
  pingIntervalRef.current = setInterval(() => {
118
  if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN || !handshakeCompleteRef.current) return
 
119
 
120
  // Check missed pongs
121
  if (missedPongsRef.current >= MAX_MISSED_PONGS) {
 
59
  const pingCounterRef = useRef<number>(0)
60
  const pingSentTimestamps = useRef<Map<string, number>>(new Map())
61
  const missedPongsRef = useRef<number>(0)
62
+ // Compat flag for gateway versions that may not implement ping RPC.
63
+ const gatewaySupportsPingRef = useRef<boolean>(true)
64
 
65
  const {
66
  connection,
 
118
 
119
  pingIntervalRef.current = setInterval(() => {
120
  if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN || !handshakeCompleteRef.current) return
121
+ if (!gatewaySupportsPingRef.current) return
122
 
123
  // Check missed pongs
124
  if (missedPongsRef.current >= MAX_MISSED_PONGS) {