• Hi,

    I’ve configured a self-hosted Rocket Chat server to allow logins via authentication by WP Oauth Server. However, using that login method overwrites the name of the user with the username from the WordPress site. Is there a way to avoid this?

    Thanks,
    Kevin

Viewing 1 replies (of 1 total)
  • Thread Starter kc1024

    (@kc1024)

    If anyone is interested, the solution was to add the following modification of the code for extending endpoints (https://wp-oauth.com/docs/how-to/extending-endpoints/) as a plugin, and putting in “name” for the Name field in the settings for the Custom OAuth settings in Rocket Chat:

    <?php
    /*
    Plugin Name: WP OAuth Extended Endpoint
    */
    add_filter('wo_endpoints','wo_extend_resource_api', 2);
    function wo_extend_resource_api ($methods){
     $methods['me'] = array('func'=>'_wo_me_1');
     return $methods;
    }
     
    /**
    * Replaces the default me enpoint
    * @param [type] $token [description]
    * @return [type] [description]
    */
    function _wo_me_1 ( $token=null ){
     $user_id = &$token['user_id'];
     
     global $wpdb;
     $me_data = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}users WHERE ID=$user_id", ARRAY_A);
     
     /** prevent sensative data - makes me happy ;) */
     unset( $me_data['user_pass'] );
     unset( $me_data['user_activation_key'] );
     unset( $me_data['user_url'] );
     unset( $me_data['session_tokens'] );
     
     // add user metadata
     $infometa = $wpdb->get_results("SELECT meta_key, meta_value FROM {$wpdb->prefix}usermeta WHERE user_id = ".$user_id."");
     foreach ($infometa as $metarow) {
     
     $key = $metarow->meta_key;
     if( is_serialized( $metarow->meta_value ) ){
     $me_data[$key] = unserialize( $metarow->meta_value );
     }else{
     $me_data[$key] = $metarow->meta_value;
     }
     }
     $me_data['name'] = $me_data['first_name'] . " " . $me_data['last_name'];
     $response = new WPOAuth2\Response($me_data);
     $response->send();
     exit;
    }
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘WP Oauth Server overwrites user name in Rocket Chat’ is closed to new replies.