-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
85 lines (65 loc) · 2.13 KB
/
server.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const socket = require('socket.io');
const express = require('express');
const axios = require('axios');
const jwt = require('jsonwebtoken');
const dotenv = require('dotenv');
const app = express();
const bodyParser = require('body-parser');
const LocalStorage = require('node-localstorage').LocalStorage;
const localStorage = new LocalStorage('./localStorage');
//middleware required
const JWTCheck = require('./middleware/JWTCheck');
dotenv.config();
const env = process.env;
const PORT = env.PORT || 3000;
const server = app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
//cors *
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
app.use(express.static('public'));
app.use(bodyParser.json({ limit: '50mb' }));
app.get('/', (req, res) => {
res.redirect('/chat');
});
app.get('/chat', JWTCheck, (req, res) => {
res.sendFile(__dirname + '/public/pages/chat/index.html');
});
app.get('/auth', (req, res) => {
res.sendFile(__dirname + '/public/pages/auth/index.html');
});
app.post('/api/auth', (req, res) => {
const { email, password } = req.body;
console.log('email', email);
let config = {
method: 'POST',
maxBodyLength: Infinity,
url: `${env.APP_API}/v1/login`,
headers: {
'Content-Type': 'application/json'
},
data : JSON.stringify({
"email": email,
"password": password
})
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response));
//locastorage user?.device
localStorage.setItem('device', JSON.stringify(response.data?.user.devices));
localStorage.setItem('user', JSON.stringify(response.data?.user));
res.cookie('token', response?.data?.authorization?.token, { maxAge: response?.data?.authorization?.expires_in * 1000, httpOnly: true });
res.send(response.data);
})
.catch(function (error) {
console.log(error);
res.send(JSON.stringify({
"error": true,
"message": error?.response?.data?.message
}), 401);
});
});