Update to make user profile auto update on authentication

This commit is contained in:
Cheng-Han, Wu 2016-05-20 03:02:07 +08:00
parent bbc7e26e77
commit 40967ce7e2

View file

@ -14,29 +14,44 @@ var models = require("./models");
function callback(accessToken, refreshToken, profile, done) { function callback(accessToken, refreshToken, profile, done) {
//logger.info(profile.displayName || profile.username); //logger.info(profile.displayName || profile.username);
var stringifiedProfile = JSON.stringify(profile);
models.User.findOrCreate({ models.User.findOrCreate({
where: { where: {
profileid: profile.id.toString() profileid: profile.id.toString()
}, },
defaults: { defaults: {
profile: JSON.stringify(profile), profile: stringifiedProfile,
accessToken: accessToken, accessToken: accessToken,
refreshToken: refreshToken refreshToken: refreshToken
} }
}).spread(function (user, created) { }).spread(function (user, created) {
if (user) { if (user) {
if (user.accessToken == accessToken) { var needSave = false;
if (config.debug) if (user.profile != stringifiedProfile) {
logger.info('user login: ' + user.id); user.profile = stringifiedProfile;
return done(null, user); needSave = true;
} }
if (user.accessToken != accessToken) {
user.accessToken = accessToken; user.accessToken = accessToken;
needSave = true;
}
if (user.refreshToken != refreshToken) {
user.refreshToken = refreshToken; user.refreshToken = refreshToken;
needSave = true;
}
if (needSave) {
user.save().then(function () { user.save().then(function () {
if (config.debug) if (config.debug)
logger.info('user login: ' + user.id); logger.info('user login: ' + user.id);
return done(null, user); return done(null, user);
}) });
} else {
if (config.debug)
logger.info('user login: ' + user.id);
return done(null, user);
}
} }
}).catch(function (err) { }).catch(function (err) {
logger.error('auth callback failed: ' + err); logger.error('auth callback failed: ' + err);