-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathserver.js
107 lines (91 loc) · 2.26 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
'use strict'
const Hapi = require('hapi')
const Good = require('good')
const Vision = require('vision')
const Users = require('./users-db')
const Handlebars = require('handlebars')
const CookieAuth = require('hapi-auth-cookie')
// create new server instance
const server = new Hapi.Server()
// add server’s connection information
server.connection({
host: 'localhost',
port: 3000
})
// register plugins to server instance
server.register([
{
register: Vision
},
{
register: Good,
options: {
ops: {
interval: 10000
},
reporters: {
console: [
{
module: 'good-squeeze',
name: 'Squeeze',
args: [ { log: '*', response: '*', request: '*' } ]
},
{
module: 'good-console'
},
'stdout'
]
}
}
},
{
register: CookieAuth
}
], function (err) {
if (err) {
server.log('error', 'failed to install plugins')
throw err
}
server.log('info', 'Plugins registered')
/**
* view configuration
*/
server.views({
engines: {
html: Handlebars
},
path: __dirname + '/views',
layout: true
})
server.log('info', 'View configuration completed')
// validation function used for hapi-auth-cookie: optional and checks if the user is still existing
const validation = function (request, session, callback) {
const username = session.username
let user = Users[ username ]
if (!user) {
return callback(null, false)
}
server.log('info', 'user authenticated')
callback(err, true, user)
}
server.auth.strategy('session', 'cookie', true, {
password: 'm!*"2/),p4:xDs%KEgVr7;e#85Ah^WYC',
cookie: 'future-studio-hapi-tutorials-cookie-auth-example',
redirectTo: '/',
isSecure: false,
validateFunc: validation
})
server.log('info', 'Registered auth strategy: cookie auth')
const routes = require('./base-routes')
server.route(routes)
server.log('info', 'Routes registered')
// start your server after plugin registration
server.start(function (err) {
if (err) {
server.log('error', 'failed to start server')
server.log('error', err)
throw err
}
server.log('info', 'Server running at: ' + server.info.uri)
})
})