简体   繁体   中英

Get token to call post to firebase database in react-native

I have a firebase database and i need to do a classic post call, but i have a problem with token.

For example, in firebase for get user i use app().auth().currentUser.uid , for a classic get list i use app().firestore().collection('prizes') and it work.

so, to get user token i do app().auth().currentUser.getIdToken() and for post i do

 export function postData(endpoint: string, data: any, token) { return fetchJson(ENDPOINT_API + endpoint}, { method: 'POST', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token ? token : USER_TOKEN}`, }, }) } 

and result is

{"error":"Token invalid","message":"Cannot destructure property type of 'undefined' or 'null'."}

the token comes back to me, but I don't know if it's the correct way

This line:

 Authorization: `Bearer ${token ? token : USER_TOKEN}`, 

Replace it by:

 'Authorization': `Bearer ${token ? token : USER_TOKEN}`, 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM