Skip to content

Commit

Permalink
🔒️ Set crisp token on v2 login, fix showCrisp event mess up
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Sep 24, 2024
1 parent 39e5285 commit b59b1b1
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 23 deletions.
19 changes: 13 additions & 6 deletions src/mixins/crisp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 1 addition & 9 deletions src/server/api/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions src/server/api/routes/users/v2/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/server/api/util/crisp.js
Original file line number Diff line number Diff line change
@@ -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,
};
11 changes: 9 additions & 2 deletions src/store/modules/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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');
Expand Down
11 changes: 7 additions & 4 deletions src/util/EventLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,18 @@ 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) {
console.error(err); // eslint-disable-line no-console
}
}

export function updateLoggerUserInfo(vue, { wallet, displayName, email }) {
export function updateLoggerUserInfo(
vue,
{ wallet, displayName, email, crispToken }
) {
if (vue.$sentry) {
const opt = {
id: wallet,
Expand All @@ -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]]);
Expand Down

0 comments on commit b59b1b1

Please sign in to comment.