-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
147 lines (120 loc) · 2.99 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const Hapi = require('hapi');
const config = require('config');
const vision = require('vision');
const handlebars = require('handlebars');
const inert = require('inert');
const jwt = require('jsonwebtoken');
const Boom = require('boom');
const server = new Hapi.Server();
const secretKey = config.get('secretKey');
// dummy user
// Use DB connection instead of dummyUser in production.
const dummyUser = {
id: '1',
name: 'hoge',
pass: 'hoge',
mail: '[email protected]'
};
// Connect the server
server.connection({
host: config.get('host'),
port: normalizePort(process.env.PORT || '3000')
});
// template engine
server.register(vision, err => {
if (err) {
throw err;
}
return server.views({
engines: {
html: handlebars
},
relativeTo: __dirname,
path: './public'
});
});
// serving static files and directories
server.register(inert, err => {
if (err) {
throw err;
}
});
// routing
server.route([
{
path: '/assets/{param*}',
method: 'GET',
handler: {
directory: {
path: 'public/assets'
}
}
},
{
path: '/{param*}',
method: 'GET',
handler: (request, reply) => {
reply.view('index', { title: 'jwt-react-redux-auth-boilerplate' });
}
},
{
path: '/api/login/',
method: 'POST',
handler: (request, reply) => {
const name = request.payload.name;
const pass = request.payload.pass;
// Use DB connection instead of dummyUser in production.
if (dummyUser.name === name && dummyUser.pass === pass) {
const jsonWebToken = jwt.sign({
id: dummyUser.id,
mail: dummyUser.mail
}, secretKey);
return reply([Object.assign({}, dummyUser, { jsonWebToken })]);
}
const err = Boom.badImplementation('name or password is not found', {
message: '入力されたユーザー名やパスワードが正しくありません。確認してからやりなおしてください。'
});
err.output.payload = Object.assign({}, err.output.payload, err.data);
return reply(err);
}
},
{
path: '/api/login/',
method: 'GET',
handler: (request, reply) => {
const jsonWebToken = request.headers.authorization.split(' ')[1];
jwt.verify(jsonWebToken, secretKey, (err, decode) => {
if (err) {
return reply(Boom.badImplementation(String(err)));
}
// Use DB connection instead of dummyUser in production.
if (dummyUser.id === decode.id) {
return reply(dummyUser);
}
return reply(Boom.badImplementation('User is not found'));
});
}
}
]);
// Start the server
server.start(err => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
const port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}