-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (57 loc) · 1.68 KB
/
server.js
File metadata and controls
71 lines (57 loc) · 1.68 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require('dotenv').config();
const express = require('express');
const path = require('path');
const axios = require('axios');
const db = require('./database');
const app = express();
const port = process.env.PORT || 3000;
app.use(express.static('assets'));
app.use(express.json());
app.get('/', (_req, res) => {
res.sendFile(path.join(__dirname, 'assets', 'view', 'index.html'));
});
app.get('/add', (_req, res) => {
res.sendFile(path.join(__dirname, 'assets', 'view', 'add.html'));
});
app.get('/login', (_req, res) => {
res.sendFile(path.join(__dirname, 'assets', 'view', 'login.html'));
});
app.post('/api/change-issue', async (req, res) => {
try {
const {
email,
password,
server,
branch,
} = req.body;
const { data } = await axios.post(
`https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${process.env.API_KEY}`,
{
email,
password,
returnSecureToken: true,
},
);
db.ref('/issues').once('value', (snapshot) => {
const issues = snapshot.val();
let toBeUpdatedIssueId = '';
for (const [issueId, { branch: issueBranch }] of Object.entries(issues)) {
if (issueBranch === branch) {
toBeUpdatedIssueId = issueId;
break;
}
}
db.ref(`/users/${data.localId}/${server}`).update({
issue: toBeUpdatedIssueId,
});
});
res.status(200).send('Successfully change branch on Branches Manager');
} catch ({ response }) {
const { error } = response.data;
console.error(error.message);
res.status(400).send(error.message);
}
});
app.listen(port, () => {
console.info(`Example app listening at http://localhost:${port}`);
});