-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
178 lines (151 loc) · 4.84 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var express = require('express'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
expressSession = require('express-session'),
mongoose = require('mongoose'),
hash = require('bcrypt-nodejs'),
path = require('path'),
passport = require('passport'),
localStrategy = require('passport-local' ).Strategy;
FacebookStrategy = require('passport-facebook').Strategy
cors = require('cors');
User = require('./app/models/fbuser.js');
// mongoose
mongoose.connect('localhost:27017/sample31');
// user schema/model
//var User = require('./models/user.js');
// console.log("whyyyyyy");
// create instance of express
var app = express();
app.use(cors());
// require routes
var routes = require('./app/routes/api.js');
// define middleware
app.use(express.static(path.join(__dirname, '../client')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(require('express-session')({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false
}));
// app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
// configure passport
passport.use(new localStrategy(function(username, password, done) {
User.findOne({'local.username': username}, function(err, user) {
if (user) {
//console.log(user);
return done(null, user);
}
if (err)
return done(err);
if (!user)
return done(null, false, { message: 'Username or password incorrect' });
})
}));
passport.use(new FacebookStrategy({
clientID: "1536158780034198",
clientSecret: "f63e8b528f6b51d4e71abab0466d7eda",
callbackURL: "http://localhost:3000/auth/facebook/callback",
profileFields: ['emails', 'id','name']
},
function(accessToken, refreshToken, profile, done) {
console.log(profile);
User.findOne({'facebook.id': profile.id}, function(err, user) {
if (err) {
// console.log(err);
return done(err);
}
if (user) {
//console.log(user);
return done(null, user);
}
else {
var newUser = new User();
console.log(newUser);
newUser.facebook.id = profile.id;
newUser.facebook.token = accessToken;
newUser.facebook.email = profile.emails[0].value;
newUser.facebook.name = profile.displayName;
newUser.save(function(err) {
if (err)
throw err;
return done (err,user);
})
}
});
}
));
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
console.log("JJJJJJJJJJJJJJ");
//console.log(user);
console.log("KKKK");
done(err, user);
});
});
app.get('/loggedin', function(req, res)
{
//console.log(req);
res.send(req.isAuthenticated() ? req.user : '0');
});
// routes
app.use('/user/', routes);
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.redirect('/index.html');
});
app.get('/logout', function(req, res) {
console.log("logggggggggggoutttttt");
req.session.destroy();
res.redirect('/');
});
//app.use(routes);
// app.get('/', function(req, res) {
// res.sendFile(path.join(__dirname, '../client', 'index.html'));
// });
app.get('/auth/facebook', function(req, res, next){
passport.authenticate('facebook', {scope: ['email'], authType: 'reauthenticate', authNonce: 'foo123'})(req, res, next);
});
//app.get('/auth/facebook',passport.authenticate('facebook', { scope: 'email'}));
// Facebook will redirect the user to this URL after approval. Finish the
// authentication process by attempting to obtain an access token. If
// access was granted, the user will be logged in. Otherwise,
// authentication has failed.
// app.get('/auth/facebook/callback',
// //console.log(req.user);
// passport.authenticate('facebook', { successRedirect: '/#/home1',
// failureRedirect: '/#/login' }));
app.get('/auth/facebook/callback', function(req, res, next){
console.log(req);
passport.authenticate('facebook', {
successRedirect: '/#/upc',
failureRedirect: '/#/login' }
)(req, res, next); // missing function call
});
// router.post('/logoutFromFacebook', function(req, res) {
// res.redirect('https://www.facebook.com/logout.php?next='+'http://localhost:3000'+'/logout&access_token='+req.body['accessToken']);
// });
// error hndlers
app.use(function(req, res, next) {
var err = new Error('Not ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Found');
err.status = 404;
next(err);
});
app.use(function(err, req, res) {
res.status(err.status || 500);
res.end(JSON.stringify({
message: err.message,
error: {}
}));
});
module.exports = app;