-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (135 loc) · 4.43 KB
/
index.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
148
149
150
151
152
153
154
155
156
157
158
159
160
const express = require('express');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
const saltRounds = 10;
const passport = require('passport');
const passportJWT = require('passport-jwt');
const Sequelize = require('sequelize');
// initialze an instance of Sequelize
const conn = new Sequelize({
database: 'mawingun_mpesa',
username: 'root',
password: '',
dialect: 'mysql',
define: {
timestamps: false // true by default. false because bydefault sequelize adds createdAt, modifiedAt columns with timestamps.if you want those columns make ths true.
}
});
// check the databse connection
conn
.authenticate()
.then(() => console.log('Connection has been established successfully.'))
.catch(err => console.error('Unable to connect to the database:', err));
//personnel and task models
const Personnel= conn.import("./models/personnel.js");
const Tasks= conn.import("./models/task.js");
console.log("wazi..things are looking good hapa.. ");
let ExtractJwt = passportJWT.ExtractJwt;
let JwtStrategy = passportJWT.Strategy;
let jwtOptions = {};
jwtOptions.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
jwtOptions.secretOrKey = 'wowwow';
// lets create our strategy for web token
let strategy = new JwtStrategy(jwtOptions, function(jwt_payload, next) {
console.log('payload received', jwt_payload);
let personnel = getPersonnel({ personnel_id: jwt_payload.personnel_id });
if (personnel) {
next(null, personnel);
} else {
next(null, false);
}
});
// use the strategy
passport.use(strategy);
const app = express();
// initialize passport with express
app.use(passport.initialize());
// parse application/json
app.use(bodyParser.json());
//parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
//return evry personnel
const getAllPersonnel = async () => {
return await Personnel.findAll();
};
//get a single personnel
const getPersonnel = async obj => {
return await Personnel.findOne({
where: obj,
});
};
//get all tasks
const getAllTasks = async () => {
return await Tasks.findAll();
};
//get a single task
const getTask = async obj => {
return await Tasks.findOne({
where: obj,
});
};
//set some basic taks routes
// get all personnel
app.get('/tasks', function(req, res) {
getAllTasks().then(task => res.json(task));
});
// set some basic personnel routes
app.get('/', function(req, res) {
res.json({ message: 'Express is up!' });
});
// get all personnel
app.get('/personnel', function(req, res) {
getAllPersonnel().then(personnel => res.json(personnel));
});
//register: storing phone, name, email and default password and redirecting to home page after signup
app.post('/personnel/create', function(req, res) {
bcrypt.hash(req.body.personnel_password, saltRounds, function(err, hash) {
Personnel.create({
personnel_onames: req.body.personnel_onames,
personnel_fname: req.body.personnel_fname,
personnel_phone: req.body.personnel_phone,
personnel_password: hash
}).then(function(data) {
if (data) {
res.redirect('/');
}
});
});
});
//login route
app.post('/personnel/login', function(req, res) {
Personnel.findOne({
where: {
personnel_phone: req.body.personnel_phone
}
}).then(function(personnel) {
if (!personnel) {
res.redirect('/');
} else {
bcrypt.compare(req.body.personnel_password, personnel.personnel_password, function(err, result) {
if (result == true) {
// from now on we'll identify the personnel by the id and the id is the
// only personalized value that goes into our token
let payload = { personnel_id: personnel.personnel_id };
let token = jwt.sign(payload, jwtOptions.secretOrKey);
res.json({
reset_password: personnel.reset_password,
accessToken: token,
expires_in: '24h'
});
} else {
res.status(401).json({ error: 'You have entered an incorrect password' });
}
});
}
});
});
// protected route
app.get('/protected', passport.authenticate('jwt', { session: false }), function(req, res) {
res.json('Success! You can now see this without a token.');
});
// start app
app.listen(3000, function() {
console.log('Express is running on port 3000');
});