-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathserver.js
72 lines (63 loc) · 1.96 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
import express from 'express';
import helmet from 'helmet';
import jwt from 'jsonwebtoken';
import cookieParser from 'cookie-parser';
import expressJwt from 'express-jwt';
import {graphqlHTTP} from 'express-graphql';
import schema from '../src/data/schema'
import dotenv from 'dotenv';
import config from './config';
const app = express();
app.use(helmet());
app.use(cookieParser());
app.use(express.json())
app.use(express.urlencoded({extended: true}))
dotenv.config();
app.use(
expressJwt({
secret: config.auth.jwt.secret,
credentialsRequired: false,
getToken: req => req.cookies.id_token,
}),
);
app.post('/login', (req, res) => {
// replace with real database check in production
// const user = graphql.find(req.login, req.password);
let user = false;
const login = req.body.login; // eslint-disable-line
const password = req.body.password; // eslint-disable-line
if (login === 'user' && password === 'password') {
user = { user, login };
}
if (user) {
const expiresIn = 60 * 60 * 24 * 180; // 180 days
const token = jwt.sign(user, config.auth.jwt.secret, { expiresIn });
res.cookie('id_token', token, {
maxAge: 1000 * expiresIn,
httpOnly: false,
});
res.json({ id_token: token });
} else {
res.status(401).json({ message: 'To login use user: "user", password: "password".' });
}
});
app.use(
'/graphql',
// expressJwt({
// //secret: config.auth.jwt.secret,
// getToken: req => req.cookies.id_token,
// }),
graphqlHTTP(req => ({
schema,
graphiql: process.env.REACT_APP_NODE_ENV,
rootValue: { request: req },
pretty: process.env.REACT_APP_NODE_ENV,
})),
);
const PORT = process.env.REACT_APP_PORT || 5000;
const server = app.listen(PORT, console.log(`Server running in ${process.env.REACT_APP_NODE_ENV} mode on port ${PORT}`))
process.on('unhandledRejection', (err) => {
console.log(`Unhandled Rejection: ${err.message}`)
// Close server
server.close(() => process.exit(1))
})