Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #873 from solocommand/idx-p1e-props
Browse files Browse the repository at this point in the history
Add IdentityX Entity prop to IdX submit events
  • Loading branch information
solocommand authored Feb 16, 2024
2 parents 868e4ab + 117cf16 commit 56ce900
Show file tree
Hide file tree
Showing 26 changed files with 97 additions and 44 deletions.
2 changes: 1 addition & 1 deletion packages/marko-web-identity-x/browser/access.vue
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export default {
contentType: content.type,
userId: this.user.id,
additionalEventData,
});
}, data.entity);
if (withReload) {
this.handleReload();
Expand Down
2 changes: 1 addition & 1 deletion packages/marko-web-identity-x/browser/authenticate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export default {
...(this.additionalEventData || {}),
...(data.additionalEventData || {}),
},
});
}, data.entity);
if (!this.showProfileForm) this.redirect();
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default {
if (!res.ok) throw new AuthenticationError(data.message, res.status);
this.emit('change-email', { data });
this.emit('change-email', { data }, data.entity);
this.redirect();
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export default {
const data = await res.json();
if (!res.ok) throw new FormError(data.message, res.status);
this.complete = true;
this.emit('change-email-link-sent', { data, email: this.email });
this.emit('change-email-link-sent', { data, email: this.email }, data.entity);
} catch (e) {
this.error = e;
this.emit('change-email-errored', { message: e.message });
Expand Down
4 changes: 2 additions & 2 deletions packages/marko-web-identity-x/browser/download.vue
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export default {
// Only emit event once when downloading
if (!this.downloaded.includes(content.id)) {
const company = content.company || {};
await post('/download', {
const { entity } = await post('/download', {
contentId: content.id,
contentType: content.type,
companyId: company.id,
Expand Down Expand Up @@ -328,7 +328,7 @@ export default {
companyId: company.id,
userId: this.user.id,
additionalEventData,
});
}, entity);
this.downloaded.push(content.id);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/marko-web-identity-x/browser/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ export default {
},
});
const data = await res.json();
const { entity } = data;
if (!res.ok) {
if (data.requiresUserInput) {
this.requiresUserInput = true;
Expand All @@ -317,7 +318,7 @@ export default {
...(this.additionalEventData || {}),
...(data.additionalEventData || {}),
},
});
}, entity);
} catch (e) {
this.error = e;
this.emit('login-errored', { message: e.message });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
},
},
methods: {
emit(name, data) {
emit(name, data, entity) {
const source = this.loginSource || this.source;
const dataActionSource = data ? data.actionSource : undefined;
const actionSource = dataActionSource || window.IdentityX.getLoginSource() || source;
Expand All @@ -18,6 +18,7 @@ export default {
actionSource,
loginSource: actionSource,
source: actionSource,
entity,
};
const { EventBus } = this;
this.$emit(name, payload);
Expand Down
2 changes: 1 addition & 1 deletion packages/marko-web-identity-x/browser/profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ export default {
...(this.additionalEventData || {}),
...(data.additionalEventData || {}),
},
});
}, data.entity);
if (this.reloadPageOnSubmit) {
this.isReloadingPage = true;
Expand Down
9 changes: 7 additions & 2 deletions packages/marko-web-identity-x/routes/access.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const mutation = gql`
`;

module.exports = asyncRoute(async (req, res) => {
const { body, apollo } = req;
/** @type {import('../middleware').IdentityXRequest} */
const { body, apollo, identityX } = req;
const { contentId, payload, cookie } = body;
const input = {
contentId,
Expand All @@ -20,5 +21,9 @@ module.exports = asyncRoute(async (req, res) => {
const { name: COOKIE_NAME, maxAge } = cookie;
await apollo.mutate({ mutation, variables: { input } });
res.cookie(COOKIE_NAME, true, { maxAge, httpOnly: false });
res.json({ ok: true });
const entity = await identityX.generateEntityId();
res.json({
ok: true,
entity,
});
});
2 changes: 2 additions & 0 deletions packages/marko-web-identity-x/routes/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ module.exports = asyncRoute(async (req, res) => {
tokenCookie.setTo(res, authToken.value);
contextCookie.setTo(res, { loginSource });
identityX.setIdentityCookie(user.id);
const entity = await identityX.generateEntityId({ userId: user.id });
res.json({
ok: true,
applicationId: identityX.config.getAppId(),
user,
loginSource,
additionalEventData,
entity,
});
});
8 changes: 7 additions & 1 deletion packages/marko-web-identity-x/routes/change-email-confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const mutation = gql`
`;

module.exports = asyncRoute(async (req, res) => {
/** @type {import('../middleware').IdentityXRequest} */
const { identityX, body } = req;
const { token } = body;
const loginSource = 'change-email';
Expand All @@ -50,5 +51,10 @@ module.exports = asyncRoute(async (req, res) => {
});
tokenCookie.setTo(res, authToken.value);
contextCookie.setTo(res, { loginSource });
res.json({ ok: true, user });
const entity = await identityX.generateEntityId({ userId: user.id });
res.json({
ok: true,
user,
entity,
});
});
7 changes: 6 additions & 1 deletion packages/marko-web-identity-x/routes/change-email-init.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const { asyncRoute } = require('@parameter1/base-cms-utils');

module.exports = asyncRoute(async (req, res) => {
/** @type {import('../middleware').IdentityXRequest} */
const { identityX, body } = req;
const { email } = body;
const entity = await identityX.generateEntityId();

// Send login link.
await identityX.sendChangeEmailLink({ email });
await identityX.logoutAppUser();
return res.json({ ok: true });
return res.json({
ok: true,
entity,
});
});
1 change: 1 addition & 0 deletions packages/marko-web-identity-x/routes/comment-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const query = gql`
`;

module.exports = asyncRoute(async (req, res) => {
/** @type {import('../middleware').IdentityXRequest} */
const { identityX } = req;
const { identifier } = req.params;
const variables = { identifier };
Expand Down
1 change: 1 addition & 0 deletions packages/marko-web-identity-x/routes/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const query = gql`
module.exports = asyncRoute(async (req, res) => {
const { identifier } = req.params;
const { after } = req.query;
/** @type {import('../middleware').IdentityXRequest} */
const { identityX } = req;
const limit = after ? 20 : 5;
const pagination = { limit, after };
Expand Down
1 change: 1 addition & 0 deletions packages/marko-web-identity-x/routes/countries.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const localeCountries = gql`
`;

module.exports = asyncRoute(async (req, res) => {
/** @type {import('../middleware').IdentityXRequest} */
const { identityX } = req;
const { data } = await identityX.client.query({ query: localeCountries });
res.json(data.localeCountries);
Expand Down
7 changes: 6 additions & 1 deletion packages/marko-web-identity-x/routes/create-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ const mutation = gql`
`;

module.exports = asyncRoute(async (req, res) => {
/** @type {import('../middleware').IdentityXRequest} */
const { identityX } = req;
const { displayName, body, stream } = req.body;

const input = { displayName, body, stream };
const variables = { input };
await identityX.client.mutate({ mutation, variables });
res.json({ ok: true });
const entity = await identityX.generateEntityId();
res.json({
ok: true,
entity,
});
});
9 changes: 7 additions & 2 deletions packages/marko-web-identity-x/routes/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ const mutation = gql`
`;

module.exports = asyncRoute(async (req, res) => {
const { body, apollo } = req;
/** @type {import('../middleware').IdentityXRequest} */
const { body, apollo, identityX } = req;
const { contentId, payload } = body;
const input = {
contentId,
payload,
ipAddress: req.ip,
};
await apollo.mutate({ mutation, variables: { input } });
res.json({ ok: true });
const entity = await identityX.generateEntityId();
res.json({
ok: true,
entity,
});
});
4 changes: 3 additions & 1 deletion packages/marko-web-identity-x/routes/flag-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const mutation = gql`
`;

module.exports = asyncRoute(async (req, res) => {
/** @type {import('../middleware').IdentityXRequest} */
const { identityX } = req;
const { id } = req.params;
const variables = { id };
await identityX.client.mutate({ mutation, variables });
res.json({ ok: true });
const entity = await identityX.generateEntityId();
res.json({ ok: true, entity });
});
4 changes: 3 additions & 1 deletion packages/marko-web-identity-x/routes/login-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const mutation = gql`
`;

module.exports = asyncRoute(async (req, res) => {
/** @type {import('../middleware').IdentityXRequest} */
const { identityX, body } = req;
const {
email,
Expand All @@ -29,5 +30,6 @@ module.exports = asyncRoute(async (req, res) => {
email,
};
await identityX.client.mutate({ mutation, variables: { input } });
return res.json({ ok: true });
const entity = await identityX.generateEntityId();
return res.json({ ok: true, entity });
});
9 changes: 8 additions & 1 deletion packages/marko-web-identity-x/routes/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const forceProfileReVerificationUser = gql`
`;

module.exports = asyncRoute(async (req, res) => {
/** @type {import('../middleware').IdentityXRequest} */
const { identityX, body } = req;
const {
email,
Expand Down Expand Up @@ -73,6 +74,7 @@ module.exports = asyncRoute(async (req, res) => {
if (additionalEventData.forceProfileReVerification || additionalEventData.createdNewUser) {
appUser = await identityX.loadAppUserByEmail(email);
}
const entity = await identityX.generateEntityId({ userId: appUser.id });

// Send login link.
await identityX.sendLoginLink({
Expand All @@ -82,5 +84,10 @@ module.exports = asyncRoute(async (req, res) => {
additionalEventData,
});
const returnedAppUser = { id: appUser.id, email: appUser.email };
return res.json({ ok: true, additionalEventData, appUser: returnedAppUser });
return res.json({
ok: true,
additionalEventData,
appUser: returnedAppUser,
entity,
});
});
5 changes: 4 additions & 1 deletion packages/marko-web-identity-x/routes/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const tokenCookie = require('../utils/token-cookie');
const callHooksFor = require('../utils/call-hooks-for');

module.exports = asyncRoute(async (req, res) => {
/** @type {import('../middleware').IdentityXRequest} */
const { identityX } = req;
contextCookie.removeFrom(res);
const token = tokenCookie.getFrom(req);
Expand All @@ -12,5 +13,7 @@ module.exports = asyncRoute(async (req, res) => {
} else {
await identityX.logoutAppUser({ token });
}
res.json({ ok: true });
res.json({
ok: true,
});
});
9 changes: 8 additions & 1 deletion packages/marko-web-identity-x/routes/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const customSelectFieldsMutation = gql`
`;

module.exports = asyncRoute(async (req, res) => {
/** @type {import('../middleware').IdentityXRequest} */
const { identityX, body } = req;
const {
givenName,
Expand Down Expand Up @@ -128,5 +129,11 @@ module.exports = asyncRoute(async (req, res) => {
req,
user,
});
res.json({ ok: true, user, additionalEventData });
const entity = await identityX.generateEntityId({ userId: user.id });
res.json({
ok: true,
user,
additionalEventData,
entity,
});
});
1 change: 1 addition & 0 deletions packages/marko-web-identity-x/routes/regions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const localeRegions = gql`
`;

module.exports = asyncRoute(async (req, res) => {
/** @type {import('../middleware').IdentityXRequest} */
const { identityX } = req;
const { data } = await identityX.client.query({ query: localeRegions });
res.json(data.localeRegions);
Expand Down
14 changes: 14 additions & 0 deletions packages/marko-web-identity-x/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,20 @@ class IdentityX {
await callHooksFor(this, 'onChangeEmailLinkSent', { email });
}

/**
* @typedef GenerateEntityIdArgs
* @prop {?string} appId The application id to generate for
* @prop {?string} userId The user id to genereate for
*
* @param {GenerateEntityIdArgs} args
* @returns {?string} The entityId of the active user/identity, if present.
*/
async generateEntityId({ appId, userId }) {
const applicationId = appId || (await this.loadActiveContext()).application.id;
const uid = userId || (await this.loadActiveContext()).user.id || await this.getIdentity();
return `identity-x.${applicationId}.app-user*${uid}`;
}

/**
* Sends a login link to an existing user
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/marko-web-p1-events/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ export default (Browser) => {
action: 'Submit',
label: 'Profile',
}],
['identity-x-authenticated', {
category: 'Identity',
action: 'Click',
label: 'Login Link',
}],
]).entries()].forEach(([event, payload]) => {
EventBus.$on(event, (args) => {
if (!window.p1events) return;
const { actionSource, newsletterSignupType, contentGatingType } = args;
window.p1events('track', {
...payload,
props: {
idxEntity: args.entity,
...(actionSource && { actionSource }),
...(newsletterSignupType && { newsletterSignupType }),
...(contentGatingType && { contentGatingType }),
Expand Down
Loading

0 comments on commit 56ce900

Please sign in to comment.