-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
278 lines (246 loc) · 8.1 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
var express = require('express');
var session = require('express-session');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var config = require('./config').config;
var compress = require('compression');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var multer = require('multer')
// 路由依赖
require('express-namespace')
var routes = require('./routes');
var MongoStore = require('connect-mongo')(session);
// 使用passport完成用户认证和授权
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var GitHubStrategy = require('passport-github').Strategy;
var WeiboStrategy = require('passport-weibo').Strategy;
var db = require('./models/db');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
// uncomment after placing your favicon in /public
app.use(favicon(__dirname + '/public/favicon.png'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer({
dest: './public/uploads/',
limit: {
fileSize: 1 * 1024 * 1024,
files: 1
}
}));
app.use(require('method-override')());
app.use(cookieParser(config.session_secret));
app.use(compress());
app.use(session({
secret: config.session_secret,
key: 'sid',
store: new MongoStore({
db: config.db_name
}),
resave: true,
saveUninitialized: true,
}));
// Use connect-flash middleware. This will add a `req.flash()` function to
// all requests, matching the functionality offered in Express 2.x.
app.use(flash());
// Use passport session
app.use(passport.initialize());
app.use(passport.session());
if (!config.debug) {
app.use(csurf());
app.set('view cache', true);
}
// requires the model with Passport-Local Mongoose plugged in
var User = require('./models/user');
// passport.use(User.createStrategy());
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
function(email, password, done) {
User.findOne({ 'local.email': email }, function(err, user) {
if (err) return done(err);
if (!user) return done(null, false, { message: '账户和密码不匹配' });
user.comparePassword(password, function(err, isMatch) {
if (isMatch) {
return done(null, user);
} else {
return done(null, false, { message: '账户和密码不匹配' });
}
});
});
}
));
var WEIBO_CLIENT_ID = '43664777';
var WEIBO_CLIENT_SECRET = '80cbe41c3e9db32f806e3a603a3c9ee1';
var WEIBO_CLIENT_CALLBACK = 'http://127.0.0.1:8080/user/auth/weibo/callback';
passport.use(new WeiboStrategy({
clientID: WEIBO_CLIENT_ID,
clientSecret: WEIBO_CLIENT_SECRET,
callbackURL: WEIBO_CLIENT_CALLBACK,
passReqToCallback: true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function(req, accessToken, refreshToken, profile, done) {
// asynchronous
process.nextTick(function() {
console.log(profile);
if (!req.user) {
// Not logged-in. Authenticate based on weibo account.
User.findOne({'weibo.id': profile.id}, function (err, user) {
// 如果发生了错误,直接返回出错信息
if (err) return done(err);
// 找到了已使用weibo登录的用户,更新其第三方登录信息
if (user) {
// console.log('更新用户');
user.weibo.token = accessToken;
// user.weibo.email = profile.emails[0].value;
user.weibo.name = profile._raw.name;
user.save(function (err) {
if (err) throw err;
return done(null, user);
});
} else {
// console.log('创建新用户');
var newUser = new User({
weibo: {
id: profile.id,
token: accessToken,
// email: profile.emails[0].value,
name: profile.username
}
});
newUser.save(function (err) {
if (err) throw err;
return done(null, newUser);
});
}
});
} else {
// Logged in. Associate Weibo account with user. Preserve the login
// state by supplying the existing user after association.
// 绑定当前用户
var user = req.user;
user.weibo.id = profile.id;
user.weibo.token = accessToken;
// user.weibo.email = profile.emails[0].value;
user.weibo.name = profile._raw.name;
user.save(function (err) {
if (err) throw err;
return done(null, user);
});
}
});
}
));
/*
passport.use(new GitHubStrategy({
clientID: config.oauth.github.client_id,
clientSecret: config.oauth.github.client_secret,
callbackURL: config.oauth.github.callback,
passReqToCallback: true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function(req, accessToken, refreshToken, profile, done) {
// asynchronous
process.nextTick(function() {
if (!req.user) {
// Not logged-in. Authenticate based on GitHub account.
User.findOne({'github.id': profile.id}, function (err, user) {
// 如果发生了错误,直接返回出错信息
if (err) return done(err);
// 找到了已使用GitHub登录的用户,更新其第三方登录信息
if (user) {
// console.log('更新用户');
user.github.token = accessToken;
user.github.email = profile.emails[0].value;
user.github.name = profile.username;
user.save(function (err) {
if (err) throw err;
return done(null, user);
});
} else {
// console.log('创建新用户');
var newUser = new User({
github: {
id: profile.id,
token: accessToken,
email: profile.emails[0].value,
name: profile.username
}
});
newUser.save(function (err) {
if (err) throw err;
return done(null, newUser);
});
}
});
} else {
// Logged in. Associate GitHub account with user. Preserve the login
// state by supplying the existing user after association.
// 绑定当前用户
var user = req.user;
user.github.id = profile.id;
user.github.token = accessToken;
user.github.email = profile.emails[0].value;
user.github.name = profile.username;
user.save(function (err) {
if (err) throw err;
return done(null, user);
});
}
});
}
));
*/
// use static serialize and deserialize of model for passport session support
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
// routes
routes(app);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
app.set('port', process.env.PORT || config.port);
var server = app.listen(app.get('port'), function() {
var address = server.address()
console.log('Dililid server listening on port ' + address.port);
console.log('Open ' + address.address + ':' + address.port + ' to access services.');
});
module.exports = app;