Skip to content

Commit

Permalink
Merge pull request #343 from FleekHQ/giancarlo/19954
Browse files Browse the repository at this point in the history
Add nickname for twitter
  • Loading branch information
giankotarola authored Nov 16, 2020
2 parents 60d072a + bcd6a63 commit 2d13c8e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
6 changes: 4 additions & 2 deletions public/electron/events/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion public/electron/events/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 36 additions & 1 deletion src/utils/use-torus-sdk.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 2d13c8e

Please sign in to comment.