-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
34 lines (30 loc) · 1009 Bytes
/
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
'use strict';
const express = require('express'),
passport = require('passport'),
htpasswd = require('htpasswd'),
BasicStrategy = require('passport-http').BasicStrategy,
app = express();
// Configuring passport
passport.use(new BasicStrategy(
{
// Custom message to user
realm: 'Private project, you must authenticate'
},
function(username, password, done) {
// The password is: bar
if (username === 'foo' && htpasswd.verify('$apr1$QNz1wNjZ$YFyKIPRxB0tcpOYmXvitK1', password)) {
const user = { name: username };
return done(null, user);
}
return done(null, false);
}));
// Using autentication
app.use(passport.authenticate('basic', { session: false }));
// My app
app.get('/', passport.authenticate('basic', { session: false }), (request, response) => {
response.send(`Bienvenido a mi proyecto privado, ${request.user.name} :)`);
});
// Starting server
const server = app.listen(3000, () => {
console.log('Server listening...');
});