WP REST API GET Requests require authentication

I’ve been working on a similar issue today. Here’s what I’ve done:

add_filter('rest_dispatch_request', function($dispatch_result, $request, 
$route, $handler) {
    if (!is_user_logged_in()) {
        $dispatch_result = new WP_Error(
            'rest_not_logged_in',
            __( 'You are not currently logged in.' ),
            array( 'status' => 401 )
        );
    }

    return $dispatch_result;
}, 10, 4);

You may want to use the $handler to determine which requests you want to restrict.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)