diff options
author | Cheng-Han, Wu | 2016-05-20 03:02:07 +0800 |
---|---|---|
committer | Cheng-Han, Wu | 2016-05-20 03:02:07 +0800 |
commit | 40967ce7e2dffa85b8749d4fb9b869b91e427f70 (patch) | |
tree | d33c649a77ed0b0af048f93139764bf7f8670b17 | |
parent | bbc7e26e7719eb5cea2ae00a1431a147de024251 (diff) |
Update to make user profile auto update on authentication
-rw-r--r-- | lib/auth.js | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/lib/auth.js b/lib/auth.js index 087937b7..2915de11 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -14,29 +14,44 @@ var models = require("./models"); function callback(accessToken, refreshToken, profile, done) { //logger.info(profile.displayName || profile.username); + var stringifiedProfile = JSON.stringify(profile); models.User.findOrCreate({ where: { profileid: profile.id.toString() }, defaults: { - profile: JSON.stringify(profile), + profile: stringifiedProfile, accessToken: accessToken, refreshToken: refreshToken } }).spread(function (user, created) { if (user) { - if (user.accessToken == accessToken) { - if (config.debug) - logger.info('user login: ' + user.id); - return done(null, user); + var needSave = false; + if (user.profile != stringifiedProfile) { + user.profile = stringifiedProfile; + needSave = true; } - user.accessToken = accessToken; - user.refreshToken = refreshToken; - user.save().then(function () { + if (user.accessToken != accessToken) { + user.accessToken = accessToken; + needSave = true; + + } + if (user.refreshToken != refreshToken) { + user.refreshToken = refreshToken; + needSave = true; + + } + if (needSave) { + user.save().then(function () { + if (config.debug) + logger.info('user login: ' + user.id); + return done(null, user); + }); + } else { if (config.debug) logger.info('user login: ' + user.id); return done(null, user); - }) + } } }).catch(function (err) { logger.error('auth callback failed: ' + err); |