@@ -7,6 +7,9 @@ import { useAppDispatch } from "caches/hooks"
7
7
import { useSnackbar } from "notistack"
8
8
import parseError from "utils/ErrorUtils"
9
9
10
+ const AUTH0_AUDIENCE = process . env . REACT_APP_AUTH0_AUDIENCE as string
11
+ const AUTH0_SCOPE = process . env . REACT_APP_AUTH0_SCOPE as string
12
+
10
13
const MyProfileSync : React . FC = ( ) => {
11
14
const dispatch = useAppDispatch ( )
12
15
const { user, error, isAuthenticated, getAccessTokenSilently } = useAuth0 ( )
@@ -16,31 +19,39 @@ const MyProfileSync: React.FC = () => {
16
19
if ( error ) enqueueSnackbar ( error . message , { variant : "error" } )
17
20
} , [ error ] )
18
21
22
+ const updateProfile = async ( name : string , picture : string ) => {
23
+ console . log ( `DAITHI: Attempting to update profile: ${ name } ` )
24
+ const accessToken = await getAccessTokenSilently ( {
25
+ authorizationParams : {
26
+ audience : AUTH0_AUDIENCE ,
27
+ redirect_uri : window . location . origin ,
28
+ scope : AUTH0_SCOPE ,
29
+ } ,
30
+ } )
31
+
32
+ console . log ( `DAITHI: Got access token: ${ accessToken } ` )
33
+
34
+ dispatch (
35
+ ProfileService . updateProfile (
36
+ {
37
+ name,
38
+ picture,
39
+ } ,
40
+ accessToken ,
41
+ ) ,
42
+ ) . catch ( ( e : Error ) =>
43
+ enqueueSnackbar ( parseError ( e ) , {
44
+ variant : "error" ,
45
+ } ) ,
46
+ )
47
+ }
48
+
19
49
useEffect ( ( ) => {
50
+ console . log ( `DAITHI: isAuthenticated: ${ isAuthenticated } user: ${ user } ` )
20
51
if ( isAuthenticated && user && user . name && user . picture ) {
21
- getAccessTokenSilently ( )
22
- . then ( accessToken =>
23
- dispatch (
24
- ProfileService . updateProfile (
25
- {
26
- name : user . name ! ,
27
- picture : user . picture ! ,
28
- } ,
29
- accessToken ,
30
- ) ,
31
- ) . catch ( ( e : Error ) =>
32
- enqueueSnackbar ( parseError ( e ) , {
33
- variant : "error" ,
34
- } ) ,
35
- ) ,
36
- )
37
- . catch ( ( e : Error ) =>
38
- enqueueSnackbar ( parseError ( e ) , {
39
- variant : "error" ,
40
- } ) ,
41
- )
52
+ updateProfile ( user . name , user . picture )
42
53
}
43
- } , [ user , isAuthenticated ] )
54
+ } , [ user , isAuthenticated , getAccessTokenSilently ] )
44
55
45
56
return < > </ >
46
57
}
0 commit comments