Fix block crash when Apple Maps credentials are missing.
Description of the Change
This PR fixes the issue where the Apple Maps block would crash when no credentials were configured instead of showing the credentials form.
Key changes:
- Added proper credentials check before MapKit initialization
- Ensured block transitions from loading state to credentials form when no credentials exist
Closes #232
How to test the Change
- Create a new post
- Add the Apple Maps block
- Verify that instead of crashing, you see the credentials form with instructions
Changelog Entry
Fixed - Block crash when no Apple Maps credentials are configured, now properly shows credentials form
Credits
Props @elvismdev
Checklist:
- [x] I agree to follow this project's Code of Conduct.
- [ ] I have updated the documentation accordingly.
- [ ] I have added Critical Flows, Test Cases, and/or End-to-End Tests to cover my change.
- [ ] All new and existing tests pass.
@elvismdev thanks for the PR! Could you please fill out the PR template with description, changelog, and credits information so that we can properly review and merge this?
Thanks for the PR, @elvismdev
While testing the code, it appears that using the settings endpoint prevents contributors and other low privileged users from using the maps block when credentials are saved.
| Develop branch | PR Branch |
|---|---|
I think the check needs to be against the MapsBlockApple/v1/GetJWT endpoint API endpoint and if that returns an error we display the unauthenticated version of the block. If it returns a valid response, the authenticated version is shown.
The endpoint is registered in the file includes/rest-routes.php
@faisal-alvi perhaps this is something you can help update and send back to Peter for review during your OSS week?
@peterwilsoncc 👋🏻
I’ve updated the block’s authentication logic as suggested. Instead of checking the settings endpoint, the block now checks the MapsBlockApple/v1/GetJWT REST API endpoint to determine authentication status.
- If the endpoint returns a valid response, the block shows the map.
- If it returns an error, the block displays the credentials form (unauthenticated state).
This should resolve the issue where contributors and other low-privileged users were unable to use the block when credentials were saved.
Please let me know if you have any feedback or if further adjustments are needed!
@faisal-alvi I'm seeing a bug once credentials are saved in that the authentication screen is shown to admins and the error message to contributors rather than the map input. At this point of the execution isAuthenticated is always false
https://github.com/10up/maps-block-apple/blob/8b7821eb96b07ca4b0b4e5317beee16a2f814d5d/src/edit.js#L235
In the console, I am seeing an error when I run the "confirm mapkit credentials button" but that may be related to the issue described above.
TypeError: setting getter-only property "target"
dispatchEvent https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js?ver=5:2
ue https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js?ver=5:2
dispatchEvent https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js?ver=5:2
onClick http://xu-osp-plugins.local/content/plugins/maps-block-apple/build/index.js?ver=b2cbbe953f00ade16470:1
@peterwilsoncc Thanks for the feedback! The issue was caused by an unnecessary check in the authentication logic; specifically, I was checking for response && response.jwt after calling the MapsBlockApple/v1/GetJWT endpoint. However, the endpoint already returns a valid response when credentials are present, so the extra .jwt check was preventing proper authentication.
I updated the logic to simply check for a valid response (i.e., if (response) { ... }), which resolves the issue. Now, after saving credentials, the block correctly authenticates and displays the map for all user roles as expected.
Let me know if you have any further feedback or spot anything else!