@@ -7,24 +7,25 @@ import global from "../ParserOpenRPC/global.module.css";
7
7
import Icon from "../Icon/Icon" ;
8
8
import {
9
9
authenticateAndAuthorize ,
10
+ AUTH_WALLET_PAIRING ,
11
+ AUTH_WALLET_SESSION_NAME ,
12
+ AUTH_WALLET_PROJECTS ,
10
13
saveTokenString ,
11
14
getUserIdFromJwtToken ,
15
+ AUTH_WALLET_USER_PLAN ,
12
16
} from "../../lib/siwsrp/auth" ;
13
- import {
14
- DASHBOARD_URL ,
15
- REQUEST_PARAMS ,
16
- AUTH_WALLET_SESSION_NAME ,
17
- AUTH_WALLET_PROJECTS ,
18
- } from "@site/src/lib/constants" ;
17
+ import { DASHBOARD_URL , REQUEST_PARAMS } from "@site/src/lib/constants" ;
19
18
import { MetamaskProviderContext } from "@site/src/theme/Root" ;
20
19
21
20
Modal . setAppElement ( "#__docusaurus" ) ;
22
21
type AuthModalProps = {
23
22
open : boolean ;
24
23
setOpen : ( arg : boolean ) => void ;
25
24
setUser : ( arg : string ) => void ;
25
+ setToken : ( arg : string ) => void ;
26
26
step : AUTH_LOGIN_STEP ;
27
27
setStep : ( arg : AUTH_LOGIN_STEP ) => void ;
28
+ setUksTier : ( arg : string ) => void ;
28
29
} ;
29
30
30
31
export enum AUTH_LOGIN_STEP {
@@ -134,7 +135,15 @@ const ConnectionErrorModal = ({
134
135
) ;
135
136
} ;
136
137
137
- const AuthModal = ( { open, setOpen, step, setStep } : AuthModalProps ) => {
138
+ const AuthModal = ( {
139
+ open,
140
+ setOpen,
141
+ step,
142
+ setStep,
143
+ setUser,
144
+ setToken,
145
+ setUksTier,
146
+ } : AuthModalProps ) => {
138
147
const { siteConfig } = useDocusaurusContext ( ) ;
139
148
const { DASHBOARD_PREVIEW_URL , VERCEL_ENV } = siteConfig ?. customFields || { } ;
140
149
const {
@@ -168,30 +177,29 @@ const AuthModal = ({ open, setOpen, step, setStep }: AuthModalProps) => {
168
177
// Call Profile SDK API to retrieve Hydra Access Token & Wallet userProfile
169
178
// Hydra Access Token will be used to fetch Infura API
170
179
const { accessToken, userProfile } = await authenticateAndAuthorize (
171
- VERCEL_ENV as string
180
+ VERCEL_ENV as string ,
172
181
) ;
173
182
174
183
const loginResponse = await (
175
184
await fetch (
176
185
`${ DASHBOARD_URL ( DASHBOARD_PREVIEW_URL , VERCEL_ENV ) } /api/wallet/login` ,
177
186
{
178
- ...REQUEST_PARAMS ( ) ,
179
- headers : {
180
- ...REQUEST_PARAMS ( ) . headers ,
187
+ ...REQUEST_PARAMS ( "POST" , {
181
188
hydra_token : accessToken ,
182
189
token : "true" ,
183
- } ,
190
+ } ) ,
184
191
body : JSON . stringify ( {
185
192
profileId : userProfile . profileId ,
186
193
redirect_to : window . location . href ,
187
194
} ) ,
188
- }
195
+ } ,
189
196
)
190
197
) . json ( ) ;
191
198
192
199
if ( ! loginResponse ) throw new Error ( "Something went wrong" ) ;
193
200
194
201
const { data, session, token } = loginResponse ;
202
+ localStorage . setItem ( AUTH_WALLET_PAIRING , JSON . stringify ( { data } ) ) ;
195
203
196
204
if ( data . step ) {
197
205
// Handling no wallet pairing or multiple pairing
@@ -201,7 +209,7 @@ const AuthModal = ({ open, setOpen, step, setStep }: AuthModalProps) => {
201
209
mmAuthSession : localStorage . getItem ( AUTH_WALLET_SESSION_NAME ) ,
202
210
walletPairing : data . pairing ,
203
211
token : true ,
204
- } )
212
+ } ) ,
205
213
) . toString ( "base64" ) ;
206
214
207
215
const walletLinkUrl = `${ DASHBOARD_URL ( DASHBOARD_PREVIEW_URL , VERCEL_ENV ) } /login?mm_auth=${ mm_auth } &redirect_to=${ session . redirect_to } ` ;
@@ -229,25 +237,43 @@ const AuthModal = ({ open, setOpen, step, setStep }: AuthModalProps) => {
229
237
}
230
238
231
239
saveTokenString ( token ) ;
240
+ if ( setToken ) {
241
+ setToken ( token ) ;
242
+ }
232
243
setStep ( AUTH_LOGIN_STEP . CONNECTION_SUCCESS ) ;
233
244
const userId = getUserIdFromJwtToken ( ) ;
245
+ if ( setUser ) {
246
+ setUser ( userId ) ;
247
+ }
234
248
235
249
// You can use Infura Access Token to fetch any Infura API endpoint
236
250
const projectsResponse = await fetch (
237
251
`${ DASHBOARD_URL ( DASHBOARD_PREVIEW_URL , VERCEL_ENV ) } /api/v1/users/${ userId } /projects` ,
238
252
{
239
- ...REQUEST_PARAMS ( "GET" ) ,
240
- headers : {
241
- ...REQUEST_PARAMS ( "GET" ) . headers ,
242
- Authorization : `Bearer ${ token } ` ,
243
- } ,
244
- }
253
+ ...REQUEST_PARAMS ( "GET" , { Authorization : `Bearer ${ token } ` } ) ,
254
+ } ,
245
255
) ;
246
256
const {
247
257
result : { projects } ,
248
258
} = await projectsResponse . json ( ) ;
249
259
sessionStorage . setItem ( AUTH_WALLET_PROJECTS , JSON . stringify ( projects ) ) ;
250
260
setProjects ( projects ) ;
261
+
262
+ const uksUserRawResp = await fetch (
263
+ `${ DASHBOARD_URL ( DASHBOARD_PREVIEW_URL , VERCEL_ENV ) } /api/v1/users/${ userId } ` ,
264
+ {
265
+ ...REQUEST_PARAMS ( "GET" , { Authorization : `Bearer ${ token } ` } ) ,
266
+ } ,
267
+ ) ;
268
+ const {
269
+ result : {
270
+ servicePlan : { tier } ,
271
+ } ,
272
+ } = await uksUserRawResp . json ( ) ;
273
+ sessionStorage . setItem ( AUTH_WALLET_USER_PLAN , JSON . stringify ( tier ) ) ;
274
+ if ( setUser ) {
275
+ setUksTier ( tier ) ;
276
+ }
251
277
setOpen ( false ) ;
252
278
} catch ( e : any ) {
253
279
if ( pathname . startsWith ( "/wallet/reference" ) ) {
0 commit comments