1
- import { env } from './env' ;
1
+ import { env , RenderBackend } from './env' ;
2
2
import { isDev , Oauth2 } from './utils' ;
3
3
4
- const DEVTOOLS_SCOPE = 'https://api.shopify.com/auth/shop.storefront.devtools' ;
5
4
const COLLABORATORS_SCOPE =
6
5
'https://api.shopify.com/auth/partners.collaborator-relationships.readonly' ;
7
6
let shopifyEmployee = false ;
7
+ let renderBackend = RenderBackend . StorefrontRenderer ;
8
8
9
9
function getOauth2Client ( origin : string ) {
10
10
const identityDomain = isDev ( origin )
@@ -13,19 +13,18 @@ function getOauth2Client(origin: string) {
13
13
const clientId = isDev ( origin )
14
14
? env . DEV_OAUTH2_CLIENT_ID
15
15
: env . OAUTH2_CLIENT_ID ;
16
- const subjectId = isDev ( origin )
17
- ? env . DEV_OAUTH2_SUBJECT_ID
18
- : env . OAUTH2_SUBJECT_ID ;
19
16
const clientAuthParams = [
20
17
[
21
18
'scope' ,
22
19
`openid profile ${
23
20
shopifyEmployee === true ? 'employee' : ''
24
- } ${ DEVTOOLS_SCOPE } ${ COLLABORATORS_SCOPE } `,
21
+ } ${ Object . values ( env . DEVTOOLS_SCOPE ) . join ( ' ' ) } ${ COLLABORATORS_SCOPE } `,
25
22
] ,
26
23
] ;
27
24
28
- return new Oauth2 ( clientId , subjectId , identityDomain , { clientAuthParams} ) ;
25
+ return new Oauth2 ( clientId , identityDomain , {
26
+ clientAuthParams,
27
+ } ) ;
29
28
}
30
29
31
30
// Change icon from colored to greyscale depending on whether or not Shopify has
@@ -48,12 +47,17 @@ function setIconAndPopup(active: string, tabId: number) {
48
47
chrome . pageAction . show ( tabId ) ;
49
48
}
50
49
50
+ function getSubjectId ( oauth : Oauth2 , origin : string ) {
51
+ if ( isDev ( origin ) ) {
52
+ return oauth . fetchClientId ( env . DEV_OAUTH2_SUBJECT_NAME [ renderBackend ] ) ;
53
+ }
54
+ return Promise . resolve ( env . OAUTH2_SUBJECT_ID [ renderBackend ] ) ;
55
+ }
56
+
51
57
chrome . runtime . onMessage . addListener ( ( { type, origin} , _ , sendResponse ) => {
52
58
if ( type !== 'signOut' ) return false ;
53
59
54
- const oauth2 = getOauth2Client ( origin ) ;
55
-
56
- oauth2
60
+ getOauth2Client ( origin )
57
61
. logoutUser ( )
58
62
. then ( ( ) => {
59
63
sendResponse ( ) ;
@@ -85,6 +89,9 @@ chrome.runtime.onMessage.addListener((event, sender) => {
85
89
chrome . runtime . onMessage . addListener ( ( event , sender ) => {
86
90
if ( sender . tab && sender . tab . id && event . type === 'detect-shopify' ) {
87
91
setIconAndPopup ( event . hasDetectedShopify , sender . tab . id ) ;
92
+ renderBackend = event . isCore
93
+ ? RenderBackend . Core
94
+ : RenderBackend . StorefrontRenderer ;
88
95
}
89
96
} ) ;
90
97
@@ -95,9 +102,7 @@ chrome.runtime.onMessage.addListener(({type, origin}, _, sendResponse) => {
95
102
return false ;
96
103
}
97
104
98
- const oauth2 = getOauth2Client ( origin ) ;
99
-
100
- oauth2
105
+ getOauth2Client ( origin )
101
106
. authenticate ( )
102
107
. then ( ( ) => {
103
108
sendResponse ( { success : true } ) ;
@@ -111,26 +116,31 @@ chrome.runtime.onMessage.addListener(({type, origin}, _, sendResponse) => {
111
116
} ) ;
112
117
113
118
// Listen for 'request-core-access-token' event and respond to the messenger
114
- // with a valid Shopify Core access token. This may trigger a login popup window
115
- // if needed.
119
+ // with a valid access token. This may trigger a login popup window if needed.
116
120
chrome . runtime . onMessage . addListener ( ( { type, origin} , _ , sendResponse ) => {
117
121
if ( type !== 'request-core-access-token' ) {
118
122
return false ;
119
123
}
120
124
121
- const oauth2 = getOauth2Client ( origin ) ;
122
125
const params = [
123
126
[
124
127
'scope' ,
125
- `${
126
- shopifyEmployee === true ? 'employee' : ''
127
- } ${ DEVTOOLS_SCOPE } ${ COLLABORATORS_SCOPE } `,
128
+ `${ shopifyEmployee === true ? 'employee' : '' } ${
129
+ env . DEVTOOLS_SCOPE [ renderBackend ]
130
+ } ${ COLLABORATORS_SCOPE } `,
128
131
] ,
129
132
] ;
130
- const destination = `${ origin } /admin` ;
131
133
132
- oauth2
133
- . getSubjectAccessToken ( destination , params )
134
+ // SFR does not need a destination.
135
+ const destination =
136
+ renderBackend === RenderBackend . Core ? `${ origin } /admin` : '' ;
137
+
138
+ const oauth = getOauth2Client ( origin ) ;
139
+
140
+ getSubjectId ( oauth , origin )
141
+ . then ( subjectId => {
142
+ return oauth . getSubjectAccessToken ( destination , subjectId , params ) ;
143
+ } )
134
144
. then ( token => {
135
145
sendResponse ( { token} ) ;
136
146
} )
@@ -146,9 +156,7 @@ chrome.runtime.onMessage.addListener(({type, origin}, _, sendResponse) => {
146
156
chrome . runtime . onMessage . addListener ( ( { type, origin} , _ , sendResponse ) => {
147
157
if ( type !== 'request-user-name' ) return false ;
148
158
149
- const oauth2 = getOauth2Client ( origin ) ;
150
-
151
- oauth2
159
+ getOauth2Client ( origin )
152
160
. getUserInfo ( )
153
161
. then ( userInfo => {
154
162
const name = userInfo . given_name ;
@@ -166,9 +174,7 @@ chrome.runtime.onMessage.addListener(({type, origin}, _, sendResponse) => {
166
174
chrome . runtime . onMessage . addListener ( ( { type, origin} , _ , sendResponse ) => {
167
175
if ( type !== 'request-auth-status' ) return false ;
168
176
169
- const oauth2 = getOauth2Client ( origin ) ;
170
-
171
- oauth2
177
+ getOauth2Client ( origin )
172
178
. hasValidClientToken ( )
173
179
. then ( isLoggedIn => {
174
180
sendResponse ( { isLoggedIn} ) ;
@@ -179,3 +185,12 @@ chrome.runtime.onMessage.addListener(({type, origin}, _, sendResponse) => {
179
185
180
186
return true ;
181
187
} ) ;
188
+
189
+ chrome . runtime . onMessage . addListener ( ( { type} , _ , sendResponse ) => {
190
+ if ( type !== 'request-rendering-backend' ) return false ;
191
+
192
+ const name = renderBackend . toString ( ) ;
193
+ sendResponse ( { name} ) ;
194
+
195
+ return true ;
196
+ } ) ;
0 commit comments