Search code examples
reactjsreact-nativexauth

How to access x-auth from headers in react native?


I've tried to access x-auth token from server.But I'm not getting the expected result.The consoled output of response.headers from server is as follows

  Headers { 
    map: {
    connection: "keep-alive"
    content-type: "application/json; charset=utf-8"
    x-auth: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiIxaXBpNG1laiIsImFjY2VzcyI6ImF1dGgiLCJpYXQiOjE1NTY3MDg1NTR9.cOXtbQO_n2vf-HwLmKwyzSi9QcFgh4SghfgCamcLyw4"
    x-powered-by: "Express"
}
    }

I've tried to console x-auth but I got error auth is not defined.But when I consoled response.headers.map.connection I got the value.Following is the code I've tried so far

fetch(GLOBAL.LOGIN, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                "requestHeader": {
                    type: "login"
                },
                "loginData": {
                    email_id: this.state.email_id,
                    password: this.state.password
                }
            })
        }).then((response) => {
           console.log(response.headers.map.x-auth);
        })

I don't know how to access the token.Please help.


Solution

  • In your .then handler you should be able to access header elements like the following:

    console.log(response.headers.get("x-auth"));