Skip to content

Commit dc022bc

Browse files
committed
feat: introducing subsriber authentication
1 parent 4b4bb5e commit dc022bc

File tree

10 files changed

+1301
-7
lines changed

10 files changed

+1301
-7
lines changed

.env.dist

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ MYSQL_DATABASE=databasorus
88
MYSQL_USER=user
99
MYSQL_PASSWORD=password
1010

11-
MERCURE_URL=http://localhost:81/mercure
12-
MERCURE_PUBLIC_URL=http://localhost:81/mercure
11+
MERCURE_SERVER_NAME=:80
12+
MERCURE_URL=http://mercure/.well-known/mercure
13+
MERCURE_PUBLIC_URL=http://localhost:81/.well-known/mercure
1314
MERCURE_PUBLISHER_JWT_KEY=!ChangeThisMercureHubJWTSecretKey!
1415
MERCURE_SUBSCRIBER_JWT_KEY=!ChangeThisMercureHubJWTSecretKey!
16+
MERCURE_CORS_ALLOW_ORIGIN="cors_origin http://localhost:80"
17+

public/js/activity.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const activityContainer = document.querySelector('#activity')
2+
3+
fetch(`/api/activity`)
4+
.then(async response => {
5+
const bearer = response.headers.get('x-mercure-token')
6+
7+
if (!bearer) return
8+
9+
const hubUrl = response.headers.get('Link').match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1]
10+
11+
const hub = new URL(hubUrl, window.origin);
12+
const body = await response.json()
13+
14+
hub.searchParams.append('topic', body.topic)
15+
16+
const es = new EventSourcePolyfill(hub, {
17+
headers: {
18+
'Authorization': bearer,
19+
}
20+
})
21+
22+
es.onmessage = event => {
23+
const data = JSON.parse(event.data)
24+
25+
const item = document.createElement('div')
26+
27+
item.setAttribute('class', 'alert alert-primary')
28+
item.setAttribute('role', 'alert')
29+
30+
item.innerHTML = data.message
31+
32+
activityContainer.append(item)
33+
}
34+
});

public/js/dinosaur.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,26 @@ const dinosaurId = dinosaurSection.dataset.id
66

77
fetch(`/api/dinosaurs/${dinosaurId}`)
88
.then(async response => {
9-
// Extract the hub URL from the Link header
9+
const bearer = response.headers.get('x-mercure-token')
10+
11+
/*
12+
* If no JWT is provided, it means that the user won't
13+
* be able to subscribe to the updates.
14+
*/
15+
if (!bearer) return
16+
1017
const hubUrl = response.headers.get('Link').match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1]
1118

1219
const hub = new URL(hubUrl, window.origin);
1320
const body = await response.json()
1421

15-
console.log(body)
1622
hub.searchParams.append('topic', body.topic)
1723

18-
// Subscribe to updates
19-
const eventSource = new EventSource(hub);
20-
eventSource.onmessage = event => window.location.reload()
24+
const es = new EventSourcePolyfill(hub, {
25+
headers: {
26+
'Authorization': bearer,
27+
}
28+
})
29+
30+
es.onmessage = event => console.log(event)
2131
});

0 commit comments

Comments
 (0)