To block the access to the admin page to users, but allowing the access to ajax at the same time:
add_action( 'admin_init', 'dev__block_admin_access', 100 );
function dev__block_admin_access() {
if ( ! defined( 'DOING_AJAX' ) && ! current_user_can( 'edit_pages' ) ) {
exit( wp_redirect( esc_url( home_url( '/' ) ) ) );
}
}
source: https://developer.wordpress.org/reference/functions/wp_doing_ajax/