如何将访问令牌从 API 存储到异步存储
how to store accesstoken from API to asyncstorage
AsyncStorage 错误:未定义访问令牌
如何定义 accesstoken 我正在使用 php 作为后端 api 并使用 accesstoken 进行注册以存储在 Asyncstorage
async storeToken(accesstoken) {
try {
await AsyncStorage.setItem(AccessToken, accesstoken);
} catch (error) {
console.log('AsyncStorage error: ' + error.message);
}
}
userRegister = () =>{
const {username} = this.state;
const {password} = this.state;
if(username=="" || password==""){
alert("Please fill out all fields");
}
// else if (password!=cpassword) {
// alert("Passwords do not match.");
// }
else{
fetch('http://192.168.01.1/test/register.php', {
method: 'post',
header:{
'Accept': 'application/json',
'Content-type': 'application/json'
},
body:JSON.stringify({
username: username,
password: password,
})
})
.then((response) => response.json())
.then((responseData) =>{
// how to define accesstoken
//this.storeToken('accessToken',JSON.stringify(results));
//if using in this then same error
this.storeToken('accesstoken',responseData.accesstoken)
if(responseData == 'User Registered Successfully'){
Alert.alert(
'Success',
'User Registered Successfully',
[
// {text:'ok', onPress: () => this.props.navigation.goBack() }
]
);
//this.props.navigation.goBack()
}else{
alert(responseJson);
}
})
.catch((error)=>{
console.log(error);
});
}
}
如何调用 accesstoken 在 function.how 中定义来解决这个问题
如果使用 await 函数访问令牌然后它显示错误 await is not an function
userRegister = () =>{
const {username, password } = this.state;
if(username=="" || password==""){
alert("Please fill out all fields");
}
// else if (password!=cpassword) {
// alert("Passwords do not match.");
// }
else{
fetch('http://192.168.01.1/test/register.php', {
method: 'post',
header:{
'Accept': 'application/json',
'Content-type': 'application/json'
},
body:JSON.stringify({
username: username,
password: password,
})
})
.then((response) => {
// console the response here, if it's containing accesstoken then pass
it in your storeToken function like below
this.storeToken(response.accesstoken)
//after this do rest of your steps
})
.catch((error)=>{
console.log(error);
});
}
}
AsyncStorage 错误:未定义访问令牌
如何定义 accesstoken 我正在使用 php 作为后端 api 并使用 accesstoken 进行注册以存储在 Asyncstorage
async storeToken(accesstoken) {
try {
await AsyncStorage.setItem(AccessToken, accesstoken);
} catch (error) {
console.log('AsyncStorage error: ' + error.message);
}
}
userRegister = () =>{
const {username} = this.state;
const {password} = this.state;
if(username=="" || password==""){
alert("Please fill out all fields");
}
// else if (password!=cpassword) {
// alert("Passwords do not match.");
// }
else{
fetch('http://192.168.01.1/test/register.php', {
method: 'post',
header:{
'Accept': 'application/json',
'Content-type': 'application/json'
},
body:JSON.stringify({
username: username,
password: password,
})
})
.then((response) => response.json())
.then((responseData) =>{
// how to define accesstoken
//this.storeToken('accessToken',JSON.stringify(results));
//if using in this then same error
this.storeToken('accesstoken',responseData.accesstoken)
if(responseData == 'User Registered Successfully'){
Alert.alert(
'Success',
'User Registered Successfully',
[
// {text:'ok', onPress: () => this.props.navigation.goBack() }
]
);
//this.props.navigation.goBack()
}else{
alert(responseJson);
}
})
.catch((error)=>{
console.log(error);
});
}
}
如何调用 accesstoken 在 function.how 中定义来解决这个问题 如果使用 await 函数访问令牌然后它显示错误 await is not an function
userRegister = () =>{
const {username, password } = this.state;
if(username=="" || password==""){
alert("Please fill out all fields");
}
// else if (password!=cpassword) {
// alert("Passwords do not match.");
// }
else{
fetch('http://192.168.01.1/test/register.php', {
method: 'post',
header:{
'Accept': 'application/json',
'Content-type': 'application/json'
},
body:JSON.stringify({
username: username,
password: password,
})
})
.then((response) => {
// console the response here, if it's containing accesstoken then pass
it in your storeToken function like below
this.storeToken(response.accesstoken)
//after this do rest of your steps
})
.catch((error)=>{
console.log(error);
});
}
}