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