Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions apps/payments/next/app/api/auth/callback/fxa/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,50 @@ export { POST } from '../../../../../auth';
export async function GET(request: NextRequest) {
const requestSearchParams = request.nextUrl.searchParams;
const requestErrorQuery = requestSearchParams.get('error');
const url = new URL(request.url);
const newAccount = url.searchParams.get('newAccount');

if (newAccount && newAccount === 'true') {
const cookieStore = request.cookies;
const redirectUrl =
cookieStore.get('__Secure-authjs.callback-url') ||
cookieStore.get('authjs.callback-url');

if (redirectUrl?.value) {
const updatedRedirectUrl = new URL(redirectUrl.value, request.url);
updatedRedirectUrl.searchParams.set('newAccount', 'true');

const rawCookieHeader = request.headers.get('cookie') ?? '';
const cookiesMap = new Map(
rawCookieHeader
.split(';')
.map(s => s.trim())
.filter(Boolean)
.map(cookieString => {
const idx = cookieString.indexOf('=');
const k = idx >= 0 ? cookieString.slice(0, idx).trim() : cookieString;
const v = idx >= 0 ? cookieString.slice(idx + 1).trim() : '';
return [k, v];
})
);
cookiesMap.set('authjs.callback-url', encodeURIComponent(updatedRedirectUrl.toString()));
cookiesMap.set('__Secure-authjs.callback-url', encodeURIComponent(updatedRedirectUrl.toString()));

const newCookieHeader = Array.from(cookiesMap.entries())
.map(([k, v]) => `${k}=${v}`)
.join('; ');

const headers = new Headers(request.headers);
headers.set('cookie', newCookieHeader);

const newRequest = new NextRequest(request.url, {
headers,
method: request.method,
});

return AuthJsGET(newRequest);
}
}

// Support failure on prompt=none
// If fxa prompt=none login fails because the user is not logged in
Expand Down
2 changes: 1 addition & 1 deletion libs/payments/metrics/src/lib/glean/glean.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { ResultCart } from '@fxa/payments/cart';
import Stripe from 'stripe';

export const CheckoutTypes = ['with-accounts', 'without-accounts'] as const;
export const CheckoutTypes = ['with-accounts', 'without-accounts'] as const;//
export type CheckoutTypesType = (typeof CheckoutTypes)[number];
import { SubPlatPaymentMethodType } from '@fxa/payments/customer';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import { determineCheckoutType } from './determineCheckoutType';

describe('determineCheckoutType', () => {
it('should return with-accounts if uid provided', () => {
expect(determineCheckoutType('validuid')).toEqual('with-accounts');
expect(determineCheckoutType('true')).toEqual('without-accounts');
});

it('should return without-accounts if empty uid', () => {
expect(determineCheckoutType('')).toEqual('without-accounts');
});

it('should return without-accounts if undefined uid', () => {
expect(determineCheckoutType(undefined)).toEqual('without-accounts');
expect(determineCheckoutType('')).toEqual('with-accounts');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import type { CheckoutTypesType } from '../glean.types';

export function determineCheckoutType(accountsUid?: string): CheckoutTypesType {
return accountsUid ? 'with-accounts' : 'without-accounts';
export function determineCheckoutType(isNewAccount?: string): CheckoutTypesType {
// If isNewAccount, return without-accounts
// If !isNewAccount && isLoggedIn, return with-accounts
// If !isNewAccount && !isLoggedIn, return ''?
/*if (isNewAccount === 'true') {
console.log('new account is true')
return 'without-accounts';
} else if (!isNewAccount || isNewAccount !== 'true' ) {
return 'with-accounts';
} else {
return ''; ??
} */

return isNewAccount === 'true' ? 'without-accounts' : 'with-accounts';
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('mapSubscription', () => {
cmsMetricsData: mockCmsMetricsData,
});
expect(result).toEqual({
subscription_checkout_type: 'without-accounts',
subscription_checkout_type: 'with-accounts',
subscription_currency: '',
subscription_error_id: '',
subscription_interval: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function mapSubscription({
}) {
const mappedParams = mapParams(commonMetricsData.params);
return {
subscription_checkout_type: determineCheckoutType(cartMetricsData.uid),
subscription_checkout_type: determineCheckoutType(commonMetricsData.searchParams['newAccount']),
subscription_currency: normalizeGleanFalsyValues(cartMetricsData.currency),
subscription_error_id: normalizeGleanFalsyValues(
cartMetricsData.errorReasonId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function buildSignOutRedirectPath(
const allRemainingQueryParams = new URLSearchParams(searchParams.toString());
allRemainingQueryParams.delete('countryCode');
allRemainingQueryParams.delete('postalCode');
allRemainingQueryParams.delete('newAccount');
const remainingQueryParams = allRemainingQueryParams.toString();

if (remainingQueryParams) {
Expand Down