-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92f5a02
commit 1ced0c5
Showing
2 changed files
with
51 additions
and
0 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,50 @@ | ||
import { randomUUID } from 'crypto'; | ||
|
||
import { WebApp } from 'meteor/webapp'; | ||
|
||
const response = (nonce: string) => ` | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Web Page with Message Event</title> | ||
</head> | ||
<body> | ||
<script nonce="${nonce}"> | ||
// Function to send a message event to the parent window when this page loads | ||
function sendInitialMessage() { | ||
// Check if the page has a parent window (to avoid cross-origin issues) | ||
if (window.parent) { | ||
// Send a message event to the parent window | ||
window.parent.postMessage({ type: 'pageLoad' }, '*'); | ||
} | ||
} | ||
// Listen for the 'login' message event from the parent window | ||
window.addEventListener('message', function (event) { | ||
const data = event.data; | ||
// Check if the event type is 'login' | ||
if (data.event === 'login' && data.loginToken) { | ||
// Store the provided string in local storage with key 'Meteor.loginToken' | ||
localStorage.setItem('Meteor.loginToken', data.loginToken); | ||
window.location.href = window.location.href.replace('/iframeLogin', data.location ?? '/home'); | ||
} | ||
}); | ||
// Call the function to send the initial message to the parent window when this page loads | ||
sendInitialMessage(); | ||
</script> | ||
</body> | ||
</html>`; | ||
|
||
WebApp.rawConnectHandlers.use('/iframeLogin', async (_req, res) => { | ||
res.setHeader('Cache-Control', 'public, max-age=31536000'); | ||
res.setHeader('Content-Type', 'text/html; charset=utf-8'); | ||
|
||
const nonce = randomUUID(); | ||
|
||
res.setHeader('Content-Security-Policy', `script-src 'nonce-${nonce}'`); | ||
|
||
res.writeHead(200); | ||
res.end(response(nonce)); | ||
}); |