-
Notifications
You must be signed in to change notification settings - Fork 0
Add notifications settings #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
isaistvo
wants to merge
5
commits into
main
Choose a base branch
from
feature/notifications-preferences
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
13e6511
feat: add notifications preferences endpoints
isaistvo d393092
feat: add notifications preferences UI
isaistvo dff62b9
feat: add notifications sound effect
isaistvo 2f20211
Merge branch 'main' of https://github.com/ua-academy-projects/share-bite
isaistvo de2f1ca
fix
isaistvo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { useEffect, useRef, useState } from 'react'; | ||
|
|
||
| export interface LabNotification { | ||
| id: string; | ||
| type: string; | ||
| entityID: string; | ||
| metadata?: any; | ||
| isRead: boolean; | ||
| createdAt: string; | ||
| readAt?: string; | ||
| } | ||
|
|
||
| export type ConnectionStatus = 'disconnected' | 'connecting' | 'connected'; | ||
|
|
||
| export function useRealtimeNotifications(token: string | null) { | ||
| const [notifications, setNotifications] = useState<LabNotification[]>([]); | ||
| const [status, setStatus] = useState<ConnectionStatus>('disconnected'); | ||
| const eventSourceRef = useRef<EventSource | null>(null); | ||
| const reconnectTimeoutRef = useRef<number | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (!token) { | ||
| setStatus('disconnected'); | ||
| setNotifications([]); | ||
| return; | ||
| } | ||
|
|
||
| const connect = () => { | ||
| if (eventSourceRef.current) { | ||
| eventSourceRef.current.close(); | ||
| } | ||
|
|
||
| setStatus('connecting'); | ||
| const url = `/api/notifications/stream?access_token=${encodeURIComponent(token)}`; | ||
| const es = new EventSource(url); | ||
|
|
||
| es.onopen = () => { | ||
| console.log("[SSE] Connection established"); | ||
| setStatus('connected'); | ||
| }; | ||
|
|
||
| es.onmessage = (event) => { | ||
| try { | ||
| const data = JSON.parse(event.data) as LabNotification; | ||
| setNotifications((prev) => [data, ...prev].slice(0, 100)); | ||
| } catch (err) { | ||
| console.error("[SSE] Failed to parse event data", err); | ||
| } | ||
| }; | ||
|
|
||
| es.onerror = () => { | ||
| console.error("[SSE] Connection error, retrying in 5s"); | ||
| setStatus('connecting'); | ||
| es.close(); | ||
|
|
||
| if (reconnectTimeoutRef.current) { | ||
| clearTimeout(reconnectTimeoutRef.current); | ||
| } | ||
| reconnectTimeoutRef.current = window.setTimeout(connect, 5000); | ||
| }; | ||
|
|
||
| eventSourceRef.current = es; | ||
| }; | ||
|
|
||
| connect(); | ||
|
|
||
| return () => { | ||
| if (reconnectTimeoutRef.current) { | ||
| clearTimeout(reconnectTimeoutRef.current); | ||
| } | ||
| if (eventSourceRef.current) { | ||
| eventSourceRef.current.close(); | ||
| } | ||
| }; | ||
| }, [token]); | ||
|
|
||
| const clearNotifications = () => setNotifications([]); | ||
|
|
||
| return { | ||
| notifications, | ||
| setNotifications, | ||
| status, | ||
| clearNotifications | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major
🧩 Analysis chain
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 305
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 1603
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 399
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 168
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 1845
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 168
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 168
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 168
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 168
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 1459
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 2560
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 5038
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 6475
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 6569
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 6144
🏁 Script executed:
Repository: ua-academy-projects/share-bite
Length of output: 2647
Remove bearer token from SSE URL and logging; implement cookie-based or session-ticket authentication.
Line 34 embeds the access token in the query string, and lines 38/52 log token fragments. This increases token exposure through browser logs, proxy logs, and server access logs. While the backend's
OptionalAuthmiddleware currently accepts query parameter tokens as a fallback, this pattern should be phased out in favor of more secure authentication.Implementation note: The browser's
EventSourceAPI cannot send custom headers, so the suggested approach requires backend support for cookie-based authentication. The backend would need to:new EventSource(url, { withCredentials: true })Alternatively, consider using short-lived session tickets or a WebSocket-based approach.
Also applies to: 38-39, 52, and the
buildNotificationsStreamUrlfunction inui/src/api/business/notifications.ts🤖 Prompt for AI Agents