maps-block-apple icon indicating copy to clipboard operation
maps-block-apple copied to clipboard

Fix block crash when Apple Maps credentials are missing.

Open elvismdev opened this issue 11 months ago • 2 comments

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

  1. Create a new post
  2. Add the Apple Maps block
  3. 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:

elvismdev avatar Jan 27 '25 23:01 elvismdev

@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?

github-actions[bot] avatar Jan 27 '25 23:01 github-actions[bot]

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
Screenshot 2025-01-29 at 11 09 42 AM Screenshot 2025-01-29 at 11 10 17 AM

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

peterwilsoncc avatar Jan 29 '25 00:01 peterwilsoncc

@faisal-alvi perhaps this is something you can help update and send back to Peter for review during your OSS week?

jeffpaul avatar May 12 '25 17:05 jeffpaul

@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 avatar May 14 '25 12:05 faisal-alvi

@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

Screenshot 2025-05-15 at 12 31 16 pm

Screenshot 2025-05-15 at 12 31 46 pm

Screenshot 2025-05-15 at 12 34 09 pm

peterwilsoncc avatar May 15 '25 02:05 peterwilsoncc

@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!

faisal-alvi avatar May 16 '25 15:05 faisal-alvi