I'm working on a react-native application with a clean D8 setup. I'm new to both. For some reason, my asyncstorage is being cleared on the emulator so I land on the login page even though I have an active session. So requesting a login returns a 403 - This route can only be accessed by anonymous users.
which is the normal behaviour. However, the app does not know who the user is or has any details regarding the user apart from the xcsrf
token. I've figured, posting to user?_format=json
redirects to the profile page of the user but the data returned is the html document.
How do I go about retrieving the details of the current logged in user without knowing their details? I'm ideally looking for a solution without any development on Drupal.
// Login
_authLogin = () => {
// api = axios
api({
method: 'post',
url: '/user/login?_format=json',
headers: {
'X-CSRF-Token' : this.props.xcsrf,
},
data: { "name": this.state.username, "pass": this.state.password }
})
.then(function(response) {
this.props.saveUserSession(response.data).then((data) => {
this.props.navigation.navigate('AuthLoading');
})
.catch((error) => {
this.setState({ error })
})
}.bind(this))
.catch(function(error) {
if (error.response) {
if( error.response.status == 403 ) {
this._getLoggedInDetails();
}
// other error handling etc...
} // other error handling etc...
}.bind(this));
};
_getLoggedInDetails = () => {
api({
method: 'post',
url: '/user?_format=json',
headers: {
'X-CSRF-Token' : this.props.xcsrf,
},
})
.then(function(response) {
// returns 200
// but <!DOCTYPE html><html lang="en" dir="ltr" ...
}.bind(this))
.catch(function(error) {
// handle errors
});
}