• Hello,

    I write an function to login a user from his ID via WP-API:

    public function login(WP_REST_Request $request)
    {
    $user_id = $request->get_param('user_id');
    
        if ($user_id) {
            $user = get_user_by('id', $user_id);
    
            if ($user) {
                wp_set_current_user($user->ID);
                wp_set_auth_cookie($user->ID, true);
    
                return new WP_REST_Response(true, 200);
            } else {
                return new WP_Error('user-not-found', 'User not found', ['status' => 500]);
            }
        } else {
            return new WP_Error('user-id-not-found', 'User id not found', ['status' => 500]);
        }
    }

    But when I go back to website, user is not logged, no cookies are set 🙁

    How can I do this ?

    Thanks!

    https://wordpress.org/plugins/json-rest-api/

  • The topic ‘How to auth user via WP-API?’ is closed to new replies.