1
1
const BASE_URL = process . env . REACT_APP_EASYAUTH_APP_URL + '/tenantbackend' ;
2
2
3
+
4
+ const tokenLocalStorage = ( ) => {
5
+ let result ;
6
+ const keys = Object . keys ( localStorage ) ;
7
+ keys . forEach ( ( key ) => {
8
+ if ( key . startsWith ( 'oidc.user' ) ) {
9
+ result = key ;
10
+ }
11
+ } ) ;
12
+ const value = localStorage . getItem ( result ) ;
13
+ return JSON . parse ( value ) ?. id_token ;
14
+ } ;
15
+
3
16
const commonAPICall = async (
4
17
PATH ,
5
18
METHOD = 'GET' ,
@@ -13,7 +26,10 @@ const commonAPICall = async (
13
26
const response = await fetch ( FULLPATH , {
14
27
method : METHOD ,
15
28
body : BODY ,
16
- headers : headers ,
29
+ headers : {
30
+ ...headers ,
31
+ Authorization : `Bearer ${ tokenLocalStorage ( ) } ` ,
32
+ } ,
17
33
} ) ;
18
34
19
35
return response ;
@@ -36,13 +52,29 @@ const postAPI = async (PATH, DATA) => {
36
52
return response ;
37
53
} ;
38
54
39
- const getProfile = async ( token ) => {
40
- const headers = { Authorization : `Bearer ${ token } ` } ;
41
- const response = await getAPI ( '/api/profile' , headers ) ;
42
- if ( ! response . ok ) {
43
- throw new Error ( `Request failed with status ${ response . status } ` ) ;
44
- }
55
+ const getProfile = async ( ) => {
56
+ const response = await getAPI ( '/api/profile' ) ;
57
+ return response ;
58
+ } ;
59
+ const getStripeCreatePortalSessionUrl = async ( ) => {
60
+ const response = await getAPI ( '/api/stripe/create-portal-session' ) ;
61
+ return response ;
62
+ } ;
63
+ const getStripeCheckoutUrl = async ( priceId ) => {
64
+ const response = await getAPI ( `/api/stripe/checkout/${ priceId } ` ) ;
65
+ return response ;
66
+ } ;
67
+ const getStripeSubscriptions = async ( ) => {
68
+ const response = await getAPI ( '/api/stripe/subscriptions' ) ;
45
69
return response ;
46
70
} ;
47
71
48
- export { commonAPICall , getAPI , postAPI , getProfile } ;
72
+ export {
73
+ commonAPICall ,
74
+ getAPI ,
75
+ postAPI ,
76
+ getProfile ,
77
+ getStripeCreatePortalSessionUrl ,
78
+ getStripeCheckoutUrl ,
79
+ getStripeSubscriptions ,
80
+ } ;
0 commit comments