|
|
@@ -1,23 +1,9 @@
|
|
|
-/* eslint-disable
|
|
|
- camelcase,
|
|
|
- max-len,
|
|
|
-*/
|
|
|
-// TODO: This file was created by bulk-decaffeinate.
|
|
|
-// Fix any style issues and re-enable lint.
|
|
|
-/*
|
|
|
- * decaffeinate suggestions:
|
|
|
- * DS102: Remove unnecessary code created because of implicit returns
|
|
|
- * DS207: Consider shorter variations of null checks
|
|
|
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
- */
|
|
|
-let ThirdPartyIdentityManager
|
|
|
const Errors = require('../Errors/Errors')
|
|
|
const { User } = require('../../models/User')
|
|
|
-const { UserStub } = require('../../models/UserStub')
|
|
|
const UserUpdater = require('./UserUpdater')
|
|
|
const _ = require('lodash')
|
|
|
|
|
|
-module.exports = ThirdPartyIdentityManager = {
|
|
|
+const ThirdPartyIdentityManager = (module.exports = {
|
|
|
getUser(providerId, externalUserId, callback) {
|
|
|
if (providerId == null || externalUserId == null) {
|
|
|
return callback(new Error('invalid arguments'))
|
|
|
@@ -26,85 +12,40 @@ module.exports = ThirdPartyIdentityManager = {
|
|
|
providerId,
|
|
|
externalUserId
|
|
|
)
|
|
|
- return User.findOne(query, function(err, user) {
|
|
|
+ User.findOne(query, function(err, user) {
|
|
|
if (err != null) {
|
|
|
return callback(err)
|
|
|
}
|
|
|
if (!user) {
|
|
|
return callback(new Errors.ThirdPartyUserNotFoundError())
|
|
|
}
|
|
|
- return callback(null, user)
|
|
|
+ callback(null, user)
|
|
|
})
|
|
|
},
|
|
|
|
|
|
login(providerId, externalUserId, externalData, callback) {
|
|
|
- return ThirdPartyIdentityManager.getUser(
|
|
|
- providerId,
|
|
|
- externalUserId,
|
|
|
- function(err, user) {
|
|
|
- if (err != null) {
|
|
|
- return callback(err)
|
|
|
- }
|
|
|
- if (!externalData) {
|
|
|
- return callback(null, user)
|
|
|
- }
|
|
|
- const query = ThirdPartyIdentityManager._getUserQuery(
|
|
|
- providerId,
|
|
|
- externalUserId
|
|
|
- )
|
|
|
- const update = ThirdPartyIdentityManager._thirdPartyIdentifierUpdate(
|
|
|
- user,
|
|
|
- providerId,
|
|
|
- externalUserId,
|
|
|
- externalData
|
|
|
- )
|
|
|
- return User.findOneAndUpdate(query, update, { new: true }, callback)
|
|
|
+ ThirdPartyIdentityManager.getUser(providerId, externalUserId, function(
|
|
|
+ err,
|
|
|
+ user
|
|
|
+ ) {
|
|
|
+ if (err != null) {
|
|
|
+ return callback(err)
|
|
|
}
|
|
|
- )
|
|
|
- },
|
|
|
-
|
|
|
- // attempt to login normally but check for user stub if user not found
|
|
|
- loginUserStub(providerId, externalUserId, externalData, callback) {
|
|
|
- return ThirdPartyIdentityManager.login(
|
|
|
- providerId,
|
|
|
- externalUserId,
|
|
|
- externalData,
|
|
|
- function(err, user) {
|
|
|
- if (err == null) {
|
|
|
- return callback(null, user)
|
|
|
- }
|
|
|
- if (err.name !== 'ThirdPartyUserNotFoundError') {
|
|
|
- return callback(err)
|
|
|
- }
|
|
|
- const query = ThirdPartyIdentityManager._getUserQuery(
|
|
|
- providerId,
|
|
|
- externalUserId
|
|
|
- )
|
|
|
- return UserStub.findOne(query, function(err, userStub) {
|
|
|
- if (err != null) {
|
|
|
- return callback(err)
|
|
|
- }
|
|
|
- if (!userStub) {
|
|
|
- return callback(new Errors.ThirdPartyUserNotFoundError())
|
|
|
- }
|
|
|
- if (!externalData) {
|
|
|
- return callback(null, userStub)
|
|
|
- }
|
|
|
- const update = ThirdPartyIdentityManager._thirdPartyIdentifierUpdate(
|
|
|
- userStub,
|
|
|
- providerId,
|
|
|
- externalUserId,
|
|
|
- externalData
|
|
|
- )
|
|
|
- return UserStub.findOneAndUpdate(
|
|
|
- query,
|
|
|
- update,
|
|
|
- { new: true },
|
|
|
- callback
|
|
|
- )
|
|
|
- })
|
|
|
+ if (!externalData) {
|
|
|
+ return callback(null, user)
|
|
|
}
|
|
|
- )
|
|
|
+ const query = ThirdPartyIdentityManager._getUserQuery(
|
|
|
+ providerId,
|
|
|
+ externalUserId
|
|
|
+ )
|
|
|
+ const update = ThirdPartyIdentityManager._thirdPartyIdentifierUpdate(
|
|
|
+ user,
|
|
|
+ providerId,
|
|
|
+ externalUserId,
|
|
|
+ externalData
|
|
|
+ )
|
|
|
+ User.findOneAndUpdate(query, update, { new: true }, callback)
|
|
|
+ })
|
|
|
},
|
|
|
|
|
|
_getUserQuery(providerId, externalUserId) {
|
|
|
@@ -135,9 +76,9 @@ module.exports = ThirdPartyIdentityManager = {
|
|
|
// but for now we need to register with v1 then call link once that
|
|
|
// is complete
|
|
|
|
|
|
- link(user_id, providerId, externalUserId, externalData, callback, retry) {
|
|
|
+ link(userId, providerId, externalUserId, externalData, callback, retry) {
|
|
|
const query = {
|
|
|
- _id: user_id,
|
|
|
+ _id: userId,
|
|
|
'thirdPartyIdentifiers.providerId': {
|
|
|
$ne: providerId
|
|
|
}
|
|
|
@@ -152,7 +93,7 @@ module.exports = ThirdPartyIdentityManager = {
|
|
|
}
|
|
|
}
|
|
|
// add new tpi only if an entry for the provider does not exist
|
|
|
- return UserUpdater.updateUser(query, update, function(err, res) {
|
|
|
+ UserUpdater.updateUser(query, update, function(err, res) {
|
|
|
if (err != null) {
|
|
|
return callback(err)
|
|
|
}
|
|
|
@@ -164,14 +105,12 @@ module.exports = ThirdPartyIdentityManager = {
|
|
|
return callback(new Error('update failed'))
|
|
|
}
|
|
|
// attempt to clear existing entry then retry
|
|
|
- return ThirdPartyIdentityManager.unlink(user_id, providerId, function(
|
|
|
- err
|
|
|
- ) {
|
|
|
+ ThirdPartyIdentityManager.unlink(userId, providerId, function(err) {
|
|
|
if (err != null) {
|
|
|
return callback(err)
|
|
|
}
|
|
|
- return ThirdPartyIdentityManager.link(
|
|
|
- user_id,
|
|
|
+ ThirdPartyIdentityManager.link(
|
|
|
+ userId,
|
|
|
providerId,
|
|
|
externalUserId,
|
|
|
externalData,
|
|
|
@@ -182,7 +121,7 @@ module.exports = ThirdPartyIdentityManager = {
|
|
|
})
|
|
|
},
|
|
|
|
|
|
- unlink(user_id, providerId, callback) {
|
|
|
+ unlink(userId, providerId, callback) {
|
|
|
const update = {
|
|
|
$pull: {
|
|
|
thirdPartyIdentifiers: {
|
|
|
@@ -190,29 +129,6 @@ module.exports = ThirdPartyIdentityManager = {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- return UserUpdater.updateUser(user_id, update, callback)
|
|
|
- },
|
|
|
-
|
|
|
- // attempt to unlink user but unlink user stub if not linked to user
|
|
|
- unlinkUserStub(user_id, providerId, callback) {
|
|
|
- return ThirdPartyIdentityManager.unlink(user_id, providerId, function(
|
|
|
- err,
|
|
|
- res
|
|
|
- ) {
|
|
|
- if (err != null) {
|
|
|
- return callback(err)
|
|
|
- }
|
|
|
- if (res.nModified === 1) {
|
|
|
- return callback(null, res)
|
|
|
- }
|
|
|
- const update = {
|
|
|
- $pull: {
|
|
|
- thirdPartyIdentifiers: {
|
|
|
- providerId
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return UserStub.update({ _id: user_id }, update, callback)
|
|
|
- })
|
|
|
+ UserUpdater.updateUser(userId, update, callback)
|
|
|
}
|
|
|
-}
|
|
|
+})
|