@@ -13,6 +13,7 @@ import {
1313 store as blockEditorStore ,
1414} from '@wordpress/block-editor' ;
1515import { useSelect , useDispatch } from '@wordpress/data' ;
16+ import apiFetch from '@wordpress/api-fetch' ;
1617
1718import { AppleMapEdit } from './components/AppleMap' ;
1819import EditAuthForm from './components/EditAuthForm' ;
@@ -75,6 +76,28 @@ export default function MapsBlockAppleEdit(props) {
7576 const { updateAuthenticationStatus } = useDispatch ( mapsBlockAppleStore ) ;
7677 const { toggleSelection } = useDispatch ( blockEditorStore ) ;
7778
79+ /**
80+ * Check if the credentials exist.
81+ */
82+ const checkCredentials = async ( ) => {
83+ try {
84+ const response = await apiFetch ( {
85+ path : 'MapsBlockApple/v1/GetJWT' ,
86+ } ) ;
87+ if ( response ) {
88+ return true ;
89+ }
90+
91+ setIsLoading ( false ) ;
92+ updateAuthenticationStatus ( false ) ;
93+ return false ;
94+ } catch ( error ) {
95+ setIsLoading ( false ) ;
96+ updateAuthenticationStatus ( false ) ;
97+ return false ;
98+ }
99+ } ;
100+
78101 /**
79102 * setup the initial authentication of mapkit and setup all the event listeners
80103 *
@@ -144,8 +167,11 @@ export default function MapsBlockAppleEdit(props) {
144167 /**
145168 * handleMapkitReInitialization
146169 */
147- const InitializeMapkit = ( ) => {
148- AppleMapEdit . authenticateMap ( localMapkit ) ;
170+ const InitializeMapkit = async ( ) => {
171+ const hasValidCredentials = await checkCredentials ( ) ;
172+ if ( hasValidCredentials ) {
173+ AppleMapEdit . authenticateMap ( localMapkit ) ;
174+ }
149175 } ;
150176 localMapkit . addEventListener ( 'reinitialize' , InitializeMapkit ) ;
151177
0 commit comments