Skip to content

Commit e707a98

Browse files
committed
Added actions to submit OTC from portal and sign in
1 parent 3b84549 commit e707a98

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

apps/portal/src/actions.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,55 @@ async function signin({data, api, state}) {
116116
}
117117
}
118118

119+
async function verifyOTC({data, api, state}) {
120+
const {t} = state;
121+
122+
try {
123+
const response = await api.member.verifyOTC(data);
124+
125+
if (response.valid && response.success) {
126+
// Enhance the redirect URL with current page context
127+
let finalRedirectUrl;
128+
129+
if (response.redirectUrl) {
130+
// Parse the provided URL and add our referrer and action
131+
const redirectUrl = new URL(response.redirectUrl);
132+
redirectUrl.searchParams.set('r', window.location.href);
133+
redirectUrl.searchParams.set('action', 'signin');
134+
finalRedirectUrl = redirectUrl.href;
135+
} else {
136+
// Fallback: redirect to current page with success params
137+
const fallbackUrl = new URL(window.location.href);
138+
fallbackUrl.searchParams.set('success', 'true');
139+
fallbackUrl.searchParams.set('action', 'signin');
140+
finalRedirectUrl = fallbackUrl.href;
141+
}
142+
143+
window.location.href = finalRedirectUrl;
144+
145+
return {
146+
action: 'verifyOTC:success'
147+
};
148+
} else {
149+
return {
150+
action: 'verifyOTC:failed',
151+
popupNotification: createPopupNotification({
152+
type: 'verifyOTC:failed', autoHide: false, closeable: true, state, status: 'error',
153+
message: response.message || t('Invalid verification code')
154+
})
155+
};
156+
}
157+
} catch (e) {
158+
return {
159+
action: 'verifyOTC:failed',
160+
popupNotification: createPopupNotification({
161+
type: 'verifyOTC:failed', autoHide: false, closeable: true, state, status: 'error',
162+
message: chooseBestErrorMessage(e, t('Failed to verify code, please try again'), t)
163+
})
164+
};
165+
}
166+
}
167+
119168
async function signup({data, state, api}) {
120169
try {
121170
let {plan, tierId, cadence, email, name, newsletters, offerId} = data;
@@ -579,6 +628,7 @@ const Actions = {
579628
back,
580629
signout,
581630
signin,
631+
verifyOTC,
582632
signup,
583633
updateSubscription,
584634
cancelSubscription,

apps/portal/src/components/pages/MagicLinkPage.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,11 @@ export default class MagicLinkPage extends React.Component {
118118
}], t: this.context.t})
119119
};
120120
}, () => {
121-
// eslint-disable-next-line no-unused-vars
122121
const {otc, errors} = this.state;
123122
const {otcRef} = this.context;
124123
const hasFormErrors = (errors && Object.values(errors).filter(d => !!d).length > 0);
125124
if (!hasFormErrors && otcRef) {
126-
// @TODO: replace with verifyOTC action
127-
// For now, just log for development
128-
// eslint-disable-next-line no-console
129-
console.log('otc_ref and otc submitted');
125+
this.context.onAction('verifyOTC', {otc, otcRef});
130126
}
131127
}
132128
);

apps/portal/src/utils/api.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,33 @@ function setupGhostApi({siteUrl = window.location.origin, apiUrl, apiKey}) {
316316
}
317317
},
318318

319+
async verifyOTC({otc, otcRef}) {
320+
const url = endpointFor({type: 'members', resource: 'verify-otc'});
321+
const body = {
322+
otc,
323+
otcRef
324+
};
325+
326+
const res = await makeRequest({
327+
url,
328+
method: 'POST',
329+
headers: {
330+
'Content-Type': 'application/json'
331+
},
332+
body: JSON.stringify(body)
333+
});
334+
335+
if (res.ok) {
336+
return await res.json();
337+
} else {
338+
const humanError = await HumanReadableError.fromApiResponse(res);
339+
if (humanError) {
340+
throw humanError;
341+
}
342+
throw new Error('Failed to verify code');
343+
}
344+
},
345+
319346
signout(all = false) {
320347
const url = endpointFor({type: 'members', resource: 'session'});
321348
return makeRequest({

0 commit comments

Comments
 (0)