Skip to content

Commit

Permalink
feat: add embed iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
darlanalves authored Jul 29, 2024
1 parent 3925d19 commit 5a9a920
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,26 @@ function makeProfile(user: User) {
);
}

async function makeEmbedPage(req) {
async function makeEmbedPage(req, res) {
const user = await findByUserId(req.user.id);
const allowedOrigins = (process.env.EMBED_ALLOWED_ORIGINS || "")
.split(",")
.map((s) => s.trim());

return `<script type="module">
res.send(`<script type="module">
const allowedOrigins = ${JSON.stringify(allowedOrigins)};
const profile = ${user ? JSON.stringify(user) : 'null'};
const profile = ${user ? JSON.stringify(user) : "null"};
window.addEventListener("message", function (event) {
console.log(event);
if (!allowedOrigins.includes(event.origin)) return;
if (!allowedOrigins.includes(event.origin)) {
console.log('Origin not allowed: ' + event.origin);
return;
}
event.source.postMessage({ source: 'auth', type: 'signin', payload: profile }, event.origin);
window.reload()
console.log(event);
event.source.postMessage({ source: 'auth', type: 'state', payload: profile }, event.origin);
}, false);
setTimeout(() => window.reload(), 1000 * 60)
</script>`;
setTimeout(() => window.reload(), 1000 * 60);
</script>`);
}

const scopes = {
Expand All @@ -146,7 +148,7 @@ app.get("/", protectedRouteWithRedirect, async (req, res) => {
app.head("/", protectedRoute, (_req, res) => res.status(204).send(""));
app.delete("/", protectedRoute, logout);
app.get("/login", (_, res) => res.send(makeLoginPage()));
app.get("/embed", (req, res) => res.send(makeEmbedPage(req)));
app.get("/embed", makeEmbedPage);
app.get("/me", protectedRoute, getProfile);
app.get("/auth/google", passport.authenticate("google", scopes));
app.get(callback, passport.authenticate("google", scopes));
Expand Down

0 comments on commit 5a9a920

Please sign in to comment.