|
| 1 | +const express = require("express"); |
| 2 | +const c = require("@liveblocks/node"); |
| 3 | +const cors = require("cors"); |
| 4 | +const { v4: uuidv4 } = require("uuid"); |
| 5 | + |
| 6 | +const liveblocks = new c.Liveblocks({ |
| 7 | + secret: "sk_dev_4oLJSBeKQJF8n3tCiEnqkC8f9mMM2gBhlIMIVZImq98FqiTNa_-SIsps6EMaQuG0", |
| 8 | +}); |
| 9 | + |
| 10 | +const port = 8080; |
| 11 | + |
| 12 | +const app = express(); |
| 13 | + |
| 14 | +// app.use((req, res, next) => { |
| 15 | +// res.header("Access-Control-Allow-Origin", "*"); |
| 16 | +// res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); |
| 17 | +// res.header("Access-Control-Allow-Headers", "Content-Type, Referer, Authorization"); |
| 18 | +// next(); |
| 19 | +// }); |
| 20 | +app.use(cors({ |
| 21 | + origin: '*', |
| 22 | + methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], |
| 23 | + allowedHeaders: ['Content-Type', 'Authorization'], |
| 24 | +})); |
| 25 | + |
| 26 | +app.use(express.json()); |
| 27 | + |
| 28 | +app.post("/api/liveblocks-auth", async (req, res) => { |
| 29 | + const name = req.query.name; |
| 30 | + const { roomId } = req.body; |
| 31 | + const userId = uuidv4(); |
| 32 | + |
| 33 | + const user = { |
| 34 | + id: userId + name, |
| 35 | + name: name, |
| 36 | + avatar: "https://example.com/marc.png", |
| 37 | + color: "purple", |
| 38 | + organization: "asrez", |
| 39 | + group: "devs", |
| 40 | + }; |
| 41 | + |
| 42 | + console.log(req); |
| 43 | + console.log(user); |
| 44 | + console.log(roomId); |
| 45 | + |
| 46 | + // const room = await liveblocks.createRoom("a32wQXid4A9", { |
| 47 | + // defaultAccesses: ["room:write"], |
| 48 | + // }); |
| 49 | + |
| 50 | + const session = liveblocks.prepareSession( |
| 51 | + user.id, |
| 52 | + { userInfo: user.metadata }, |
| 53 | + ); |
| 54 | + |
| 55 | + session.allow(`${user.organization}:*`, session.READ_ACCESS); |
| 56 | + session.allow(`${user.organization}:${user.group}:*`, session.FULL_ACCESS); |
| 57 | + |
| 58 | + const { status, body } = await session.authorize(); |
| 59 | + return res.status(status).end(body); |
| 60 | +}); |
| 61 | + |
| 62 | +app.listen(port, () => { |
| 63 | + console.log(`Server running at http://localhost:${port}`); |
| 64 | +}); |
0 commit comments