|
| 1 | +const express = require("express"); |
| 2 | +const cors = require("cors"); |
| 3 | +const axios = require("axios"); |
| 4 | + |
| 5 | +const app = express(); |
| 6 | +app.use(express.json()); |
| 7 | +app.use(cors({ origin: true })); |
| 8 | + |
| 9 | +const CHAT_ENGINE_PROJECT_ID = ""; |
| 10 | +const CHAT_ENGINE_PRIVATE_KEY = ""; |
| 11 | + |
| 12 | +app.post("/signup", async (req, res) => { |
| 13 | + const { username, secret, email, first_name, last_name } = req.body; |
| 14 | + |
| 15 | + // Store a user-copy on Chat Engine! |
| 16 | + // Docs at rest.chatengine.io |
| 17 | + try { |
| 18 | + const r = await axios.post( |
| 19 | + "https://api.chatengine.io/users/", |
| 20 | + { username, secret, email, first_name, last_name }, |
| 21 | + { headers: { "Private-Key": CHAT_ENGINE_PRIVATE_KEY } } |
| 22 | + ); |
| 23 | + return res.status(r.status).json(r.data); |
| 24 | + } catch (e) { |
| 25 | + return res.status(e.response.status).json(e.response.data); |
| 26 | + } |
| 27 | +}); |
| 28 | + |
| 29 | +app.post("/login", async (req, res) => { |
| 30 | + const { username, secret } = req.body; |
| 31 | + |
| 32 | + // Fetch this user from Chat Engine in this project! |
| 33 | + // Docs at rest.chatengine.io |
| 34 | + try { |
| 35 | + const r = await axios.get("https://api.chatengine.io/users/me/", { |
| 36 | + headers: { |
| 37 | + "Project-ID": CHAT_ENGINE_PROJECT_ID, |
| 38 | + "User-Name": username, |
| 39 | + "User-Secret": secret, |
| 40 | + }, |
| 41 | + }); |
| 42 | + return res.status(r.status).json(r.data); |
| 43 | + } catch (e) { |
| 44 | + return res.status(e.response.status).json(e.response.data); |
| 45 | + } |
| 46 | +}); |
| 47 | + |
| 48 | +// vvv On port 3001! |
| 49 | +app.listen(3001); |
0 commit comments