diff --git a/src/mixins/crisp.js b/src/mixins/crisp.js index a2aa65758..917986b8a 100644 --- a/src/mixins/crisp.js +++ b/src/mixins/crisp.js @@ -19,14 +19,21 @@ export const CrispMixinFactory = (options = { isBootAtMounted: true }) => ({ showCrisp() { if (!this.$crisp) return false; try { - const email = this.walletEmail; - const wallet = this.loginAddress || this.getAddress; - const displayName = this.getLikerInfo?.displayName || wallet; const { $crisp } = this; - if (email) $crisp.push(['set', 'user:email', [email]]); - if (displayName) $crisp.push(['set', 'user:nickname', [displayName]]); - if (wallet) $crisp.push(['set', 'chat:show']); if ($crisp.is('chat:hidden')) { + const email = this.walletEmail; + const wallet = this.loginAddress || this.getAddress; + const displayName = this.getLikerInfo?.displayName || wallet; + if (email && !$crisp.get('user:email')) { + $crisp.push(['set', 'user:email', [email]]); + } + if (displayName) { + $crisp.push(['set', 'user:nickname', [displayName]]); + } + if (wallet) { + $crisp.push(['set', 'session:data', [[['like_wallet', wallet]]]]); + } + $crisp.push(['do', 'chat:show']); return true; } } catch (err) { diff --git a/src/server/api/routes/users.js b/src/server/api/routes/users.js index 43349c9f5..b4a48742a 100644 --- a/src/server/api/routes/users.js +++ b/src/server/api/routes/users.js @@ -5,6 +5,7 @@ const jwt = require('jsonwebtoken'); const { userCollection } = require('../../modules/firebase'); const { setPrivateCacheHeader } = require('../middleware/cache'); const axios = require('../../modules/axios'); +const { getCrispUserHash } = require('../util/crisp'); const { apiFetchUserProfile, apiFetchUserSuperLikeStatus, @@ -18,18 +19,9 @@ const { AUTH_COOKIE_OPTION, OAUTH_SCOPE_REQUIRED, } = require('../constant'); -const { CRISP_USER_HASH_SECRET } = require('../../config/config'); const CLEAR_AUTH_COOKIE_OPTION = { ...AUTH_COOKIE_OPTION, maxAge: 0 }; -function getCrispUserHash(email) { - if (!CRISP_USER_HASH_SECRET) return undefined; - return crypto - .createHmac('sha256', CRISP_USER_HASH_SECRET) - .update(email) - .digest('hex'); -} - function setSessionOAuthState(req) { const state = crypto.randomBytes(8).toString('hex'); req.session.state = state; diff --git a/src/server/api/routes/users/v2/auth.js b/src/server/api/routes/users/v2/auth.js index 11a59be12..f54fff0e2 100644 --- a/src/server/api/routes/users/v2/auth.js +++ b/src/server/api/routes/users/v2/auth.js @@ -15,6 +15,7 @@ const { isValidAddress, checkCosmosSignPayload, } = require('../../../util/cosmos'); +const { getCrispUserHash } = require('../../../util/crisp'); const CLEAR_AUTH_COOKIE_OPTION = { ...AUTH_COOKIE_OPTION, maxAge: 0 }; @@ -38,6 +39,7 @@ router.get('/self', authenticateV2Login, async (req, res, next) => { emailUnconfirmed, eventLastSeenTs: eventLastSeenTs ? eventLastSeenTs.toMillis() : 1000, locale, + crispToken: email ? getCrispUserHash(email) : undefined, }); } catch (err) { if (req.session) req.session = null; @@ -86,6 +88,7 @@ router.post('/login', async (req, res, next) => { const userRef = walletUserCollection.doc(userId); const userDoc = await t.get(userRef); const isNew = !userDoc.exists; + const userDocData = userDoc.data(); const payload = { lastLoginTs: FieldValue.serverTimestamp(), lastLoginMethod: loginMethod, @@ -99,8 +102,9 @@ router.post('/login', async (req, res, next) => { } else { await t.update(userRef, payload); } - return { isNew }; + return { ...userDocData, isNew }; }); + const { isNew, email, displayName } = result; if (result.isNew) { publisher.publish(PUBSUB_TOPIC_MISC, req, { logType: 'UserSignUp', @@ -116,7 +120,13 @@ router.post('/login', async (req, res, next) => { user: userId, }); } - res.json(result); + const payload = { + user: userId, + displayName, + isNew, + crispToken: email ? getCrispUserHash(email) : undefined, + }; + res.json(payload); return; } catch (error) { // eslint-disable-next-line no-console diff --git a/src/server/api/util/crisp.js b/src/server/api/util/crisp.js new file mode 100644 index 000000000..e836095df --- /dev/null +++ b/src/server/api/util/crisp.js @@ -0,0 +1,16 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +const crypto = require('crypto'); + +const { CRISP_USER_HASH_SECRET } = require('../../config/config'); + +function getCrispUserHash(email) { + if (!CRISP_USER_HASH_SECRET || !email) return undefined; + return crypto + .createHmac('sha256', CRISP_USER_HASH_SECRET) + .update(email) + .digest('hex'); +} + +module.exports = { + getCrispUserHash, +}; diff --git a/src/store/modules/wallet.js b/src/store/modules/wallet.js index 1bda1cd50..1e2d59338 100644 --- a/src/store/modules/wallet.js +++ b/src/store/modules/wallet.js @@ -917,8 +917,13 @@ const actions = { if (!checkIsLikeCoinAppInAppBrowser(this.$router.app.$route)) { await dispatch('setLocale', userInfo.locale); } - const { displayName, email } = userInfo; - updateLoggerUserInfo(this, { email, displayName, wallet: state.address }); + const { displayName, email, crispToken } = userInfo; + updateLoggerUserInfo(this, { + email, + displayName, + wallet: state.address, + crispToken, + }); return userInfo; }, async walletFetchSessionUserData( @@ -951,6 +956,8 @@ const actions = { await setLoggerUser(this, { wallet: address, method: methodType, + email: result.email, + crispToken: result.crispToken, event: result.isNew ? 'signup' : 'login', }); await dispatch('walletFetchSessionUserData'); diff --git a/src/util/EventLogger.js b/src/util/EventLogger.js index 7d99ed1d7..dc527a739 100644 --- a/src/util/EventLogger.js +++ b/src/util/EventLogger.js @@ -58,7 +58,7 @@ export async function setLoggerUser( } } if (vue.$crisp) { - vue.$crisp.push(['set', 'session:data', [[['wallet', wallet]]]]); + vue.$crisp.push(['set', 'session:data', [[['like_wallet', wallet]]]]); vue.$crisp.push(['set', 'session:data', [[['login_method', method]]]]); } } catch (err) { @@ -66,7 +66,10 @@ export async function setLoggerUser( } } -export function updateLoggerUserInfo(vue, { wallet, displayName, email }) { +export function updateLoggerUserInfo( + vue, + { wallet, displayName, email, crispToken } +) { if (vue.$sentry) { const opt = { id: wallet, @@ -77,10 +80,10 @@ export function updateLoggerUserInfo(vue, { wallet, displayName, email }) { } if (vue.$crisp) { if (email) { - vue.$crisp.push(['set', 'user:email', [email]]); + vue.$crisp.push(['set', 'user:email', [email, crispToken]]); } if (wallet) { - vue.$crisp.push(['set', 'session:data', [[['wallet', wallet]]]]); + vue.$crisp.push(['set', 'session:data', [[['like_wallet', wallet]]]]); } if (displayName) { vue.$crisp.push(['set', 'user:nickname', [displayName || wallet]]);