-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
57 lines (52 loc) · 1.59 KB
/
passport.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
module.exports = function(app){
var passport = require("passport"),
twitchStrategy = require("passport-twitch").Strategy,
User = require('./app/models/user');
app.use(passport.initialize());
app.use(passport.session());
passport.use(new twitchStrategy({
clientID: process.env.TWITCH_CLIENT_ID,
clientSecret: process.env.TWITCH_CLIENT_SECRET,
callbackURL: process.env.TWITCH_CALLBACK_URL,
scope: ["user_read", "channel_read", "channel_subscriptions"]
}, (accessToken, refreshToken, profile, done) => {
User.findOne({
twitchId: profile.id
}, (err, user) => {
if (err) return done(err);
if (!user) {
user = new User({
twitchId: profile.id,
displayName: profile.displayName,
logo: profile._json.logo,
updated_at: profile._json.updated_at,
token: accessToken
});
user.save((err) => {
if (err) console.log(err);
return done(err, user);
});
}else{
if(new Date(profile.updated_at) > new Date(user.updated_at)){
user.displayName = profile.displayName;
user.logo = profile._json.logo;
user.updated_at = profile._json.updated_at;
user.save((err) => {
if (err) console.log(err);
return done(err, user);
});
}else{
return done(err, user);
}
}
});
}));
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user);
});
});
}