-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbell.js
103 lines (82 loc) · 2.33 KB
/
bell.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
// Load modules
var Hapi = require('hapi');
var Bell = require('bell');
var AuthCookie = require('hapi-auth-cookie');
// Handlers
var publicHandler = function (request, reply) {
reply('Everyone can see this...');
};
var privateHandler = function (request, reply) {
reply('Welcome ' + request.auth.credentials.name);
};
var login = function (request, reply) {
if (request.auth.isAuthenticated) {
request.auth.session.set({
name: request.auth.credentials.profile.displayName
});
return reply('Logged in...');
}
reply('Not logged in...');
}
var logout = function (request, reply) {
request.auth.session.clear();
reply('Logged out...');
}
// Create server
var server = new Hapi.Server();
server.connection({ port: 8189 });
// Load plugins
server.register([AuthCookie, Bell], function (err) {
// Configure cookie auth scheme
var authCookieOptions = {
password: 'PasswordUsedToEncryptCookie',
cookie: 'NameOfCookie',
redirectTo: '/login',
isSecure: false
};
server.auth.strategy('YourCookieAuth', 'cookie', authCookieOptions);
// Configure third party auth scheme
var bellAuthOptions = {
provider: 'github',
password: 'PasswordUsedToEncryptThirdPartyAuthCookie',
clientId: '9be920982e25e4dea143',//'YourAppId',
clientSecret: '96fb20b27af508d15302cc485638d15d7646e614',//'YourAppSecret',
isSecure: false
};
server.auth.strategy('YourThirdPartyAuth', 'bell', bellAuthOptions);
// Configure routes after plugins are loaded
server.route({
method: 'GET',
path: '/public',
handler: publicHandler
});
// Configure protected routes by setting auth
server.route({
method: 'GET',
path: '/private',
handler: privateHandler,
config: {
auth: 'YourCookieAuth'
}
});
// Login page
server.route({
method: 'GET',
path: '/login',
handler: login,
config: {
auth: 'YourThirdPartyAuth'
}
});
// Logout
server.route({
method: 'GET',
path: '/logout',
handler: logout,
config: {
auth: 'YourCookieAuth'
}
});
// Start server
server.start(function () { console.log('Started...'); });
});