nfn
nfn

Reputation: 633

How can I check username and password of an existing user in WordPress with react-native/

I'm writing a mobile application for a WordPress site. I want to check username and password with WPAPI and JSON Basic Authentication plugin. I have tried this code so far:

  const wp = new WPAPI({
    endpoint: 'https://example.com/wp-json',
    username: this.state.username,
    password: this.state.password
  });
  console.log(wp.users().me());

it returns

_options:{auth:true, ...}

no matter what is the username and password value.

Upvotes: 0

Views: 648

Answers (2)

nfn
nfn

Reputation: 633

I have finally solved this problem:

 validate() {
  let username = this.state.username;
  let password = this.state.password;
  let userPassCombination = username + ':' + password;
  let encodedPass = base64.encode(userPassCombination);

  let headers = {
    'Content-Type': 'text/json',
    Authorization: 'Basic ' + encodedPass,
  };

  fetch(myConstants.URL + '/wp-json/', {
    method: 'GET',
    headers: headers,
  }).then(responseData => {
    //console.log(JSON.stringify(responseData));
    if (responseData.ok) {
      this.props.navigation.navigate('Profile', {
        username: this.state.username,
      });
    } 
    else {
      alert('wrong information.');
    }
  });
} 

}

Upvotes: 1

suther
suther

Reputation: 13578

Maybe show your react native implementation of the WPAPI.

Documentation:

As an example, wp.users().me() will automatically enable authentication to permit access to the /users/me endpoint.

Sounds like it's more an API-Call which set a flag instead of returning your userprofile (I guess you expected to get your user-profile?!)

Maybe give it a try to request a page (like described in the examples (see: Documentation)): wp.pages().slug( 'about' )

Upvotes: 1

Related Questions