1

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
    });
}

1 Answer 1

1

Right, I found a solution myself. There are 2 options;

  1. Views

    • Create a new view with the user entity, set a filter criteria for logged in user and provide a rest export. This will return the current user object. So you can make a request to this when login returns a 403 so you can retrieve the details of the user.
  2. Logout token

    • Apply this patch to the user controller. This provides a new resource to get a logout token for the logged in user. So when login returns a 403, you can get a logout token at session/logouttoken and log the user out and login again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.