diff --git a/identity-service/src/routes/tiktok.js b/identity-service/src/routes/tiktok.js index 819cd6cd987..3f10de69532 100644 --- a/identity-service/src/routes/tiktok.js +++ b/identity-service/src/routes/tiktok.js @@ -67,7 +67,9 @@ module.exports = function (app) { try { // Fetch user's accessToken const accessTokenResponse = await axios.post(urlAccessToken) - const { access_token: accessToken } = accessTokenResponse.data + const { + data: { access_token: accessToken } + } = accessTokenResponse.data // Fetch TikTok user from the TikTok API const userResponse = await axios.post( @@ -90,15 +92,25 @@ module.exports = function (app) { const { user: tikTokUser } = data - // Store the user id, and current profile for user in db - await models.TikTokUser.findOrCreate({ + const existingTikTokUser = await models.TikTokUser.findOne({ where: { uuid: tikTokUser.open_id }, - defaults: { + blockchainUserId: { + [models.Sequelize.Op.not]: null + } + }) + + if (existingTikTokUser) { + return errorResponseBadRequest( + `Another Audius profile has already been authenticated with TikTok user @${tikTokUser.display_name}!` + ) + } else { + // Store the user id, and current profile for user in db + await models.TikTokUser.upsert({ uuid: tikTokUser.open_id, profile: tikTokUser, verified: tikTokUser.is_verified - } - }) + }) + } return successResponse(accessTokenResponse.data) } catch (err) {