-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
164 lines (132 loc) · 4.23 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
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
161
162
163
164
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var cors = require('cors');
var passport = require('passport');
var bcrypt = require('bcrypt');
var mongojs = require('mongojs');
var LocalStrategy = require('passport-local').Strategy;
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
var jwt = require('jsonwebtoken');
var passportJWT = require('passport-jwt');
var ExtractJwt = passportJWT.ExtractJwt;
var JwtStrategy = passportJWT.Strategy;
var jwtOptions = {}
jwtOptions.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme("jwt");
jwtOptions.secretOrKey = 'mycode11';
jwtOptions.passReqToCallback=true;
var strategy = new JwtStrategy(jwtOptions, function(req,jwt_payload, next) {
var originalUrl=req.originalUrl;
//console.log('url',originalUrl);
//var user = users[_.findIndex(users, {id: jwt_payload.id})];
db.signup_data.findOne({_id:mongojs.ObjectId(jwt_payload._id)},function(err,user){
if (user) {
next(null, user);
} else {
next(null, false);
}
});
})
passport.use(strategy);
app.use(passport.initialize());
app.use (express.static(__dirname+"/public"));
app.use('/app/*',express.static(__dirname+'/public/index.html'));
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', "http://localhost:8080");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept");
next();
});
var db = mongojs('employee',['signup_data']);
app.post('/signup',function(req,res){
db.signup_data.find({email:req.body.email},function(err,doc){
if(!err)
{
if(doc.length===0)
{
bcrypt.hash(req.body.password, 10, function(err, hash) {
if(err){ res.json(false);};
req.body.password=hash;
db.signup_data.insert(req.body,function (err, docs) {
if(!err)
{
res.json(true);
}
else
{
res.json(false);
}
});
});
}
else
{
res.json('duplicate');
}
}
else
{
res.json(false);
}
})
});
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
db.signup_data.findOne({_id: mongojs.ObjectId(id)}, function(err, user) {
done(err, user);
});
});
passport.use('local',new LocalStrategy({
usernameField:'email',
passwordField:'password'
},
function (username, password, done) {
db.signup_data.findOne({ email: username }, function (err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
var hash=user.password;
bcrypt.compare(password, hash, function(err, ress) {
//
if(ress === true)
{
return done(null, user);
}
else
{
return done(null, false, { message: 'Incorrect password.' });
}
}) ;
});
}
));
app.post('/login',function(req,res,next){
passport.authenticate('local', function(err, user, info) {
if (err) { return res.status(404).json(err); }
if (!user) { return res.status(501).json(info); }
req.logIn(user, function(err) {
if (err) { return res.status(501).json(err); }
var token = jwt.sign(user, jwtOptions.secretOrKey, {expiresIn: 12000});
return res.status(200).json({"token" : token,message:'Login Success'});
});
})(req, res, next);
});
app.get('/getData',passport.authenticate('jwt', { session: false }),function(req, res) {
db.signup_data.find(function(err, docs) {
if(docs.length===0)
{
res.json("nodata")
}
else
{
res.json(docs);
}
});
});
app.listen(3000);
console.log('server is running on 3000');