-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.js
97 lines (82 loc) · 2.38 KB
/
app.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
'use strict'
const BodyParser = require('body-parser')
const Express = require('express')
const Passport = require('passport')
const Crypto = require('crypto')
const JSONWebToken = require('jsonwebtoken')
const LocalStrategy = require('passport-local').Strategy
const users = {
foo: {
username: 'foo',
password: 'bar',
id: 1
},
bar: {
username: 'bar',
password: 'foo',
id: 2
}
}
const localStrategy = new LocalStrategy({
usernameField: 'username',
passwordField: 'password'
},
function (username, password, done) {
const user = users[username]
if (user == null) {
return done(null, false, { message: 'Invalid user' })
}
if (user.password !== password) {
return done(null, false, { message: 'Invalid password' })
}
done(null, user)
})
Passport.use('local', localStrategy)
const generateToken = function (user) {
// The payload just contains the id of the user
// and their username, we can verify whether the
// claim is correct using JSONWebToken.verify
const payload = {
id: user.id,
username: user.username
}
// Generate a random string
// Usually this would be an app wide constant
// But can be done both ways
const secret = Crypto.randomBytes(128).toString('base64')
// Create the token with a payload and secret
const token = JSONWebToken.sign(payload, secret)
// The user is still referencing the same object
// in users, so no need to set it again
// If we were using a database, we would save
// it here
user.secret = secret
return token
}
const generateTokenHandler = function (request, response) {
const user = request.user
// Generate our token
const token = generateToken(user)
// Return the user a token to use
response.send(token)
}
const app = Express()
app.use(BodyParser.urlencoded({ extended: false }))
app.use(BodyParser.json())
app.use(Passport.initialize())
app.post(
'/login',
Passport.authenticate('local', { session: false }),
generateTokenHandler
)
const port = require('../config.json').port
app.listen(port, function () {
logInfo('Listening on port ' + port)
})
// ///////////////////////////////////////////////////////////////////////////
// Logging
const basename = require('path').basename(__filename)
const logInfo = function (msg) {
const timestamp = require('moment')().format('YYYY-MM-DD HH:mm:ss')
console.log('[' + timestamp + ' INFO] (' + basename + ') ' + msg)
}