Skip to content

Commit d584c53

Browse files
wip
1 parent 02cb13d commit d584c53

File tree

1 file changed

+37
-54
lines changed
  • BFF/v3/Websocket/Websocket.react/src

1 file changed

+37
-54
lines changed

BFF/v3/Websocket/Websocket.react/src/App.tsx

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { BookManager } from './BookManager'
55

66
type TabType = 'testing' | 'books';
77

8-
interface UserClaims {
9-
[key: string]: any;
10-
name?: string;
11-
sub?: string;
8+
interface ClaimRecord {
9+
[type: string]: any;
10+
value?: string;
11+
valueType?: string;
1212
}
1313

1414
function App() {
@@ -19,10 +19,10 @@ function App() {
1919

2020
// Authentication state
2121
const [isLoggedIn, setIsLoggedIn] = useState<boolean>(false)
22-
const [userClaims, setUserClaims] = useState<UserClaims | null>(null)
22+
const [userClaims, setUserClaims] = useState<ClaimRecord[]>([])
2323
const [authLoading, setAuthLoading] = useState<boolean>(true)
2424

25-
const serverUrl = 'http://localhost:5197'
25+
const serverUrl = 'http://localhost:5059'
2626
const { isConnected, error, connect, disconnect, query, subscribe } = useGraphQL({
2727
url: serverUrl,
2828
autoConnect: false // Don't auto-connect until we're authenticated
@@ -52,22 +52,23 @@ function App() {
5252

5353
if (response.ok) {
5454
const claims = await response.json()
55+
console.log(claims);
5556
if (claims && Object.keys(claims).length > 0) {
5657
setIsLoggedIn(true)
5758
setUserClaims(claims)
5859
setMessages(prev => [...prev, 'User authenticated successfully'])
5960
} else {
6061
setIsLoggedIn(false)
61-
setUserClaims(null)
62+
setUserClaims([])
6263
}
6364
} else {
6465
setIsLoggedIn(false)
65-
setUserClaims(null)
66+
setUserClaims([])
6667
setMessages(prev => [...prev, 'User not authenticated'])
6768
}
6869
} catch (err) {
6970
setIsLoggedIn(false)
70-
setUserClaims(null)
71+
setUserClaims([])
7172
setMessages(prev => [...prev, `Auth check error: ${err instanceof Error ? err.message : 'Unknown error'}`])
7273
} finally {
7374
setAuthLoading(false)
@@ -80,37 +81,19 @@ function App() {
8081
}
8182

8283
const handleLogout = async () => {
83-
try {
84-
const response = await fetch('/bff/logout', {
85-
method: 'POST',
86-
credentials: 'include'
87-
})
88-
89-
if (response.ok) {
90-
setIsLoggedIn(false)
91-
setUserClaims(null)
92-
disconnect() // Disconnect from GraphQL
93-
setMessages(prev => [...prev, 'Logged out successfully'])
94-
// Optionally redirect to home or login page
95-
window.location.href = '/'
96-
} else {
97-
setMessages(prev => [...prev, 'Logout failed'])
98-
}
99-
} catch (err) {
100-
setMessages(prev => [...prev, `Logout error: ${err instanceof Error ? err.message : 'Unknown error'}`])
101-
}
84+
window.location.href = userClaims.find(claim => claim.type === 'bff:logout_url' && claim.value)?.value || '/bff/logout'
10285
}
10386

10487
const getUserDisplayName = (): string => {
105-
if (!userClaims) return 'Unknown User'
106-
107-
// Try different claim types for the user's name
108-
return userClaims.name ||
109-
userClaims.given_name ||
110-
userClaims.preferred_username ||
111-
userClaims.email ||
112-
userClaims.sub ||
113-
'Unknown User'
88+
if (!userClaims.length) return 'Unknown User'
89+
90+
var name = userClaims.find(claim => claim.type === 'name' && claim.value)?.value;
91+
92+
if (name)
93+
{
94+
return name;
95+
}
96+
return 'Unknown User';
11497
}
11598

11699
// Example GraphQL query
@@ -211,23 +194,23 @@ function App() {
211194
)
212195
}
213196

214-
// Show login screen if not authenticated
215-
if (!isLoggedIn) {
216-
return (
217-
<div className="app">
218-
<div className="auth-container">
219-
<h1>React WebSocket GraphQL Client</h1>
220-
<div className="login-section">
221-
<h2>Authentication Required</h2>
222-
<p>Please log in to access the GraphQL client.</p>
223-
<button onClick={handleLogin} className="login-button">
224-
Login
225-
</button>
226-
</div>
227-
</div>
228-
</div>
229-
)
230-
}
197+
// // Show login screen if not authenticated
198+
// if (!isLoggedIn) {
199+
// return (
200+
// <div className="app">
201+
// <div className="auth-container">
202+
// <h1>React WebSocket GraphQL Client</h1>
203+
// <div className="login-section">
204+
// <h2>Authentication Required</h2>
205+
// <p>Please log in to access the GraphQL client.</p>
206+
// <button onClick={handleLogin} className="login-button">
207+
// Login
208+
// </button>
209+
// </div>
210+
// </div>
211+
// </div>
212+
// )
213+
// }
231214

232215
return (
233216
<div className="app">

0 commit comments

Comments
 (0)