Skip to content

Commit

Permalink
Fix TikTok auth + improve association (#4511)
Browse files Browse the repository at this point in the history
* Add logs

* Fix tiktok auth

* Error if tiktok user is already associated
  • Loading branch information
sliptype authored Dec 20, 2022
1 parent 2edcf1d commit 89991fd
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions identity-service/src/routes/tiktok.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down

0 comments on commit 89991fd

Please sign in to comment.