-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
126 lines (114 loc) · 4.12 KB
/
index.jsx
File metadata and controls
126 lines (114 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React, {useEffect} from 'react'
import PropTypes from 'prop-types'
import {defineMessage, useIntl} from 'react-intl'
import {Button} from '@salesforce/retail-react-app/app/components/shared/ui'
import logger from '@salesforce/retail-react-app/app/utils/logger-instance'
import {useAuthHelper, AuthHelpers} from '@salesforce/commerce-sdk-react'
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
import {useAppOrigin} from '@salesforce/retail-react-app/app/hooks/use-app-origin'
import {setSessionJSONItem, buildRedirectURI} from '@salesforce/retail-react-app/app/utils/utils'
// Icons
import {AppleIcon, GoogleIcon} from '@salesforce/retail-react-app/app/components/icons'
import {
API_ERROR_MESSAGE,
FEATURE_UNAVAILABLE_ERROR_MESSAGE
} from '@salesforce/retail-react-app/app/constants'
const IDP_CONFIG = {
apple: {
icon: AppleIcon,
message: defineMessage({
id: 'login_form.button.apple',
defaultMessage: 'Apple'
})
},
google: {
icon: GoogleIcon,
message: defineMessage({
id: 'login_form.button.google',
defaultMessage: 'Google'
})
}
}
/**
* Create a stack of button for social login links
* @param {array} idps - array of known IDPs to show buttons for
* @returns
*/
const SocialLogin = ({form, idps = []}) => {
const {formatMessage} = useIntl()
const authorizeIDP = useAuthHelper(AuthHelpers.AuthorizeIDP)
// Build redirectURI from config values
const appOrigin = useAppOrigin()
const redirectPath = getConfig()?.app?.login?.social?.redirectURI || ''
const redirectURI = buildRedirectURI(appOrigin, redirectPath)
const isIdpValid = (name) => {
const idp = name.toLowerCase()
return idp in IDP_CONFIG && IDP_CONFIG[idp]
}
useEffect(() => {
idps.map((name) => {
if (!isIdpValid(name)) {
logger.error(
`IDP "${name}" is missing or has an invalid configuration in IDP_CONFIG. Valid IDPs are [${Object.keys(
IDP_CONFIG
).join(', ')}].`
)
}
})
}, [idps])
const onSocialLoginClick = async (name) => {
try {
// Save the path where the user logged in
setSessionJSONItem('returnToPage', window.location.pathname)
await authorizeIDP.mutateAsync({
hint: name,
redirectURI: redirectURI
})
} catch (error) {
const message = /redirect_uri doesn't match/.test(error.message)
? formatMessage(FEATURE_UNAVAILABLE_ERROR_MESSAGE)
: formatMessage(API_ERROR_MESSAGE)
form.setError('global', {type: 'manual', message})
}
}
return (
idps && (
<>
{idps
.filter((name) => isIdpValid(name))
.map((name) => {
const config = IDP_CONFIG[name.toLowerCase()]
const Icon = config?.icon
const message = formatMessage(config?.message)
return (
config && (
<Button
key={name}
onClick={() => {
onSocialLoginClick(name)
}}
borderColor="gray.500"
color="blue.600"
variant="outline"
>
<Icon sx={{marginRight: 2}} />
{message}
</Button>
)
)
})}
</>
)
)
}
SocialLogin.propTypes = {
form: PropTypes.object,
idps: PropTypes.arrayOf(PropTypes.string)
}
export default SocialLogin