diff --git a/public/electron/events/account.js b/public/electron/events/account.js index c0440d75..5d8092f1 100644 --- a/public/electron/events/account.js +++ b/public/electron/events/account.js @@ -146,8 +146,9 @@ const registerAuthEvents = (mainWindow) => { address: payload.torusRes.publicAddress, provider: payload.provider, metadata: { - email: payload.torusRes.userInfo.email, name: payload.torusRes.userInfo.name, + email: payload.torusRes.userInfo.email, + nickname: payload.torusRes.userInfo.nickname, }, }); mainWindow.webContents.send(ADD_LINKED_ADDRESS_SUCCESS_EVENT, { @@ -156,8 +157,9 @@ const registerAuthEvents = (mainWindow) => { provider: payload.provider, createdAt: new Date().toISOString(), metadata: { - email: payload.torusRes.userInfo.email, name: payload.torusRes.userInfo.name, + email: payload.torusRes.userInfo.email, + nickname: payload.torusRes.userInfo.nickname, }, }); } catch (error) { diff --git a/public/electron/events/auth.js b/public/electron/events/auth.js index 38aafca9..2a8bc611 100644 --- a/public/electron/events/auth.js +++ b/public/electron/events/auth.js @@ -123,8 +123,9 @@ const registerAuthEvents = (mainWindow) => { address: payload.torusRes.publicAddress, provider: payload.torusRes.userInfo.typeOfLogin, metadata: { - email: payload.torusRes.userInfo.email, name: payload.torusRes.userInfo.name, + email: payload.torusRes.userInfo.email, + nickname: payload.torusRes.userInfo.nickname, }, }); diff --git a/src/shared/components/Modal/Settings/views/Security/index.js b/src/shared/components/Modal/Settings/views/Security/index.js index c123592b..6fce1b5c 100644 --- a/src/shared/components/Modal/Settings/views/Security/index.js +++ b/src/shared/components/Modal/Settings/views/Security/index.js @@ -171,7 +171,7 @@ const Security = ({ t }) => { options[OPTION_IDS.TWITTER] = { id: OPTION_IDS.TWITTER, text: t('addBackupSignIn.twitter'), - text2: metadata.name, + text2: metadata.nickname ? `@${metadata.nickname}` : metadata.name, // text3: t('modals.settings.security.disconnect'), imgSrc: 'https://fleek-team-bucket.storage.fleek.co/third-party-logo/Twitter_Logo_Blue.svg', redText3: true, diff --git a/src/utils/use-torus-sdk.js b/src/utils/use-torus-sdk.js index 82d18547..3bb1c753 100644 --- a/src/utils/use-torus-sdk.js +++ b/src/utils/use-torus-sdk.js @@ -1,15 +1,45 @@ import React from 'react'; +import axios from 'axios'; import TorusSdk from '@toruslabs/torus-direct-web-sdk'; import config from '@config'; /** + * @typedef {Object} UserInfo + * @property {string?} email + * @property {string?} nickname + * @property {string} name + * @property {string} idToken + * @property {string} typeOfLogin + * @property {string} accessToken + * * @typedef {Object} TorusRes - * @property {Object} userInfo + * @property {UserInfo} userInfo * @property {string} privateKey * @property {string} publicAddress */ +/** + * @param {TorusRes} torusRes + */ +const setTwitterNickname = async (torusRes) => { + try { + const { data } = await axios.get(`${config.torus.providers.twitter.jwtParams.domain}/userinfo`, { + headers: { + authorization: `Bearer ${torusRes.userInfo.accessToken}`, + }, + }); + + if (data && data.nickname) { + // eslint-disable-next-line no-param-reassign + torusRes.userInfo.nickname = data.nickname; + } + } catch (error) { + // eslint-disable-next-line no-console + console.error(`Error when trying to get twitter nickname: ${error.message}`); + } +}; + export default function useTorusSdk() { const [state, setState] = React.useState({ torusSdk: null, @@ -47,6 +77,11 @@ export default function useTorusSdk() { if (tRes) { tRes.privateKey = `0x${tRes.privateKey}`; + + if (tRes.userInfo.typeOfLogin === 'twitter') { + await setTwitterNickname(tRes); + } + return tRes; }