Skip to content

Commit ec15311

Browse files
committed
create getTokenBasedAuthErrorMessage util and use it for authorizePasswordlessLogin and getPasswordResetToken errors
1 parent ceb2b79 commit ec15311

File tree

6 files changed

+58
-59
lines changed

6 files changed

+58
-59
lines changed

packages/template-retail-react-app/app/constants.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ export const FEATURE_UNAVAILABLE_ERROR_MESSAGE = defineMessage({
105105
defaultMessage: 'This feature is not currently available.',
106106
id: 'global.error.feature_unavailable'
107107
})
108-
export const TOO_MANY_REQUESTS_ERROR_MESSAGE = defineMessage({
109-
defaultMessage:
110-
'Too many requests. For your security, please wait 10 minutes before trying again.',
111-
id: 'global.error.too_many_requests'
112-
})
113108

114109
export const HOME_HREF = '/'
115110

@@ -256,17 +251,6 @@ export const LOGIN_TYPES = {
256251
SOCIAL: 'social'
257252
}
258253

259-
// Error messages that indicate the passwordless feature is unavailable
260-
export const AUTH_FEATURE_UNAVAILABLE_ERRORS = [
261-
/no callback_uri is registered/i,
262-
/callback_uri doesn't match/i,
263-
/passwordless permissions error/i,
264-
/client secret is not provided/i,
265-
/monthly quota/i
266-
]
267-
268-
export const TOO_MANY_REQUESTS_ERROR = /too many .* requests/i
269-
270254
export const INVALID_TOKEN_ERROR = /invalid token/i
271255

272256
/**

packages/template-retail-react-app/app/hooks/use-auth-modal.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,8 @@ import ResetPasswordForm from '@salesforce/retail-react-app/app/components/reset
3131
import RegisterForm from '@salesforce/retail-react-app/app/components/register'
3232
import PasswordlessEmailConfirmation from '@salesforce/retail-react-app/app/components/email-confirmation/index'
3333
import {noop} from '@salesforce/retail-react-app/app/utils/utils'
34-
import {
35-
API_ERROR_MESSAGE,
36-
FEATURE_UNAVAILABLE_ERROR_MESSAGE,
37-
AUTH_FEATURE_UNAVAILABLE_ERRORS,
38-
TOO_MANY_REQUESTS_ERROR,
39-
TOO_MANY_REQUESTS_ERROR_MESSAGE
40-
} from '@salesforce/retail-react-app/app/constants'
34+
import {getTokenBasedAuthErrorMessage} from '@salesforce/retail-react-app/app/utils/auth-utils'
35+
import {API_ERROR_MESSAGE} from '@salesforce/retail-react-app/app/constants'
4136
import useNavigation from '@salesforce/retail-react-app/app/hooks/use-navigation'
4237
import {usePrevious} from '@salesforce/retail-react-app/app/hooks/use-previous'
4338
import {usePasswordReset} from '@salesforce/retail-react-app/app/hooks/use-password-reset'
@@ -110,9 +105,7 @@ export const AuthModal = ({
110105
})
111106
setCurrentView(EMAIL_VIEW)
112107
} catch (error) {
113-
const message = AUTH_FEATURE_UNAVAILABLE_ERRORS.some((msg) => msg.test(error.message))
114-
? formatMessage(FEATURE_UNAVAILABLE_ERROR_MESSAGE)
115-
: formatMessage(API_ERROR_MESSAGE)
108+
const message = formatMessage(getTokenBasedAuthErrorMessage(error.message))
116109
form.setError('global', {type: 'manual', message})
117110
}
118111
}
@@ -187,13 +180,7 @@ export const AuthModal = ({
187180
try {
188181
await getPasswordResetToken(data.email)
189182
} catch (e) {
190-
const message = AUTH_FEATURE_UNAVAILABLE_ERRORS.some((msg) =>
191-
msg.test(e.message)
192-
)
193-
? formatMessage(FEATURE_UNAVAILABLE_ERROR_MESSAGE)
194-
: TOO_MANY_REQUESTS_ERROR.test(e.message)
195-
? formatMessage(TOO_MANY_REQUESTS_ERROR_MESSAGE)
196-
: formatMessage(API_ERROR_MESSAGE)
183+
const message = formatMessage(getTokenBasedAuthErrorMessage(e.message))
197184
form.setError('global', {type: 'manual', message})
198185
}
199186
},

packages/template-retail-react-app/app/pages/checkout/partials/contact-info.jsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ import {useAppOrigin} from '@salesforce/retail-react-app/app/hooks/use-app-origi
4545
import {AuthHelpers, useAuthHelper, useShopperBasketsMutation} from '@salesforce/commerce-sdk-react'
4646
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
4747
import {buildAbsoluteUrl} from '@salesforce/retail-react-app/app/utils/url'
48-
import {
49-
API_ERROR_MESSAGE,
50-
FEATURE_UNAVAILABLE_ERROR_MESSAGE,
51-
PASSWORDLESS_ERROR_MESSAGES
52-
} from '@salesforce/retail-react-app/app/constants'
48+
import {getTokenBasedAuthErrorMessage} from '@salesforce/retail-react-app/app/utils/auth-utils'
5349

5450
const ContactInfo = ({isSocialEnabled = false, isPasswordlessEnabled = false, idps = []}) => {
5551
const {formatMessage} = useIntl()
@@ -94,9 +90,7 @@ const ContactInfo = ({isSocialEnabled = false, isPasswordlessEnabled = false, id
9490
setAuthModalView(EMAIL_VIEW)
9591
authModal.onOpen()
9692
} catch (error) {
97-
const message = PASSWORDLESS_ERROR_MESSAGES.some((msg) => msg.test(error.message))
98-
? formatMessage(FEATURE_UNAVAILABLE_ERROR_MESSAGE)
99-
: formatMessage(API_ERROR_MESSAGE)
93+
const message = formatMessage(getTokenBasedAuthErrorMessage(error.message))
10094
setError(message)
10195
}
10296
}

packages/template-retail-react-app/app/pages/login/index.jsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ import PasswordlessEmailConfirmation from '@salesforce/retail-react-app/app/comp
2929
import {
3030
API_ERROR_MESSAGE,
3131
INVALID_TOKEN_ERROR,
32-
INVALID_TOKEN_ERROR_MESSAGE,
33-
FEATURE_UNAVAILABLE_ERROR_MESSAGE,
34-
PASSWORDLESS_ERROR_MESSAGES
32+
INVALID_TOKEN_ERROR_MESSAGE
3533
} from '@salesforce/retail-react-app/app/constants'
3634
import {usePrevious} from '@salesforce/retail-react-app/app/hooks/use-previous'
3735
import {isServer, noop} from '@salesforce/retail-react-app/app/utils/utils'
36+
import {getTokenBasedAuthErrorMessage} from '@salesforce/retail-react-app/app/utils/auth-utils'
3837
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
3938

4039
const LOGIN_ERROR_MESSAGE = defineMessage({
@@ -113,9 +112,7 @@ const Login = ({initialView = LOGIN_VIEW}) => {
113112
setPasswordlessLoginEmail(email)
114113
setCurrentView(EMAIL_VIEW)
115114
} catch (error) {
116-
const message = PASSWORDLESS_ERROR_MESSAGES.some((msg) => msg.test(error.message))
117-
? formatMessage(FEATURE_UNAVAILABLE_ERROR_MESSAGE)
118-
: formatMessage(API_ERROR_MESSAGE)
115+
const message = formatMessage(getTokenBasedAuthErrorMessage(error.message))
119116
form.setError('global', {type: 'manual', message})
120117
}
121118
}

packages/template-retail-react-app/app/pages/reset-password/index.jsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@ import useDataCloud from '@salesforce/retail-react-app/app/hooks/use-datacloud'
1919
import {useLocation} from 'react-router-dom'
2020
import {useRouteMatch} from 'react-router'
2121
import {usePasswordReset} from '@salesforce/retail-react-app/app/hooks/use-password-reset'
22-
import {
23-
API_ERROR_MESSAGE,
24-
FEATURE_UNAVAILABLE_ERROR_MESSAGE,
25-
AUTH_FEATURE_UNAVAILABLE_ERRORS,
26-
TOO_MANY_REQUESTS_ERROR,
27-
TOO_MANY_REQUESTS_ERROR_MESSAGE
28-
} from '@salesforce/retail-react-app/app/constants'
22+
import {getTokenBasedAuthErrorMessage} from '@salesforce/retail-react-app/app/utils/auth-utils'
2923

3024
const ResetPassword = () => {
3125
const {formatMessage} = useIntl()
@@ -41,11 +35,7 @@ const ResetPassword = () => {
4135
try {
4236
await getPasswordResetToken(email)
4337
} catch (e) {
44-
const message = AUTH_FEATURE_UNAVAILABLE_ERRORS.some((msg) => msg.test(e.message))
45-
? formatMessage(FEATURE_UNAVAILABLE_ERROR_MESSAGE)
46-
: TOO_MANY_REQUESTS_ERROR.test(e.message)
47-
? formatMessage(TOO_MANY_REQUESTS_ERROR_MESSAGE)
48-
: formatMessage(API_ERROR_MESSAGE)
38+
const message = formatMessage(getTokenBasedAuthErrorMessage(e.message))
4939
form.setError('global', {type: 'manual', message})
5040
}
5141
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2021, salesforce.com, inc.
3+
* All rights reserved.
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
8+
import {defineMessage} from 'react-intl'
9+
import {
10+
API_ERROR_MESSAGE,
11+
FEATURE_UNAVAILABLE_ERROR_MESSAGE
12+
} from '@salesforce/retail-react-app/app/constants'
13+
14+
// Error patterns for token-based auth features (passwordless login, password reset)
15+
const TOKEN_BASED_AUTH_FEATURE_UNAVAILABLE_ERRORS = [
16+
/no callback_uri is registered/i,
17+
/callback_uri doesn't match/i,
18+
/passwordless permissions error/i,
19+
/client secret is not provided/i,
20+
/monthly quota/i
21+
]
22+
23+
const TOO_MANY_REQUESTS_ERROR = /too many .* requests/i
24+
25+
const TOO_MANY_REQUESTS_ERROR_MESSAGE = defineMessage({
26+
defaultMessage:
27+
'Too many requests. For your security, please wait 10 minutes before trying again.',
28+
id: 'global.error.too_many_requests'
29+
})
30+
31+
/**
32+
* Maps an error message to the appropriate user-friendly error message descriptor
33+
* for token-based authentication features that send tokens via email (passwordless login, password reset).
34+
* Checks for auth feature unavailable errors and too many requests errors.
35+
*
36+
* @param {string} errorMessage - The error message from the API
37+
* @returns {Object} - The message descriptor object (from defineMessage) that can be passed to formatMessage
38+
*/
39+
export const getTokenBasedAuthErrorMessage = (errorMessage) => {
40+
if (TOKEN_BASED_AUTH_FEATURE_UNAVAILABLE_ERRORS.some((msg) => msg.test(errorMessage))) {
41+
return FEATURE_UNAVAILABLE_ERROR_MESSAGE
42+
}
43+
if (TOO_MANY_REQUESTS_ERROR.test(errorMessage)) {
44+
return TOO_MANY_REQUESTS_ERROR_MESSAGE
45+
}
46+
return API_ERROR_MESSAGE
47+
}

0 commit comments

Comments
 (0)