-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introducing subsriber authentication
- Loading branch information
1 parent
4b4bb5e
commit dc022bc
Showing
10 changed files
with
1,301 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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,34 @@ | ||
const activityContainer = document.querySelector('#activity') | ||
|
||
fetch(`/api/activity`) | ||
.then(async response => { | ||
const bearer = response.headers.get('x-mercure-token') | ||
|
||
if (!bearer) return | ||
|
||
const hubUrl = response.headers.get('Link').match(/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/)[1] | ||
|
||
const hub = new URL(hubUrl, window.origin); | ||
const body = await response.json() | ||
|
||
hub.searchParams.append('topic', body.topic) | ||
|
||
const es = new EventSourcePolyfill(hub, { | ||
headers: { | ||
'Authorization': bearer, | ||
} | ||
}) | ||
|
||
es.onmessage = event => { | ||
const data = JSON.parse(event.data) | ||
|
||
const item = document.createElement('div') | ||
|
||
item.setAttribute('class', 'alert alert-primary') | ||
item.setAttribute('role', 'alert') | ||
|
||
item.innerHTML = data.message | ||
|
||
activityContainer.append(item) | ||
} | ||
}); |
This file contains 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
Oops, something went wrong.