【发布时间】:2021-07-27 08:47:29
【问题描述】:
我正在使用 react-chat-engine 创建一个聊天应用程序。一切正常。除了我不知道如何发出帖子请求以创建新用户这一事实。请任何人都可以帮助我。我真的很需要这个。
这是我的代码
const handelSubmit = async(e) => {
e.preventDefault();
if (!(username.length > 0 && password === confirmPassword && password.length > 0)) return;
const authObject = {
'Private-Key': '2f389292-d5e1-4799-bd31-b456e7e94845',
// 'Project-ID': '25a91c10-8623-4a0d-a48a-3de096d44b54',
// 'User-Name': username,
// 'User-Secret': password
}
const authHeader = {
'Private-Key': '2f389292-d5e1-4799-bd31-b456e7e94845',
'Project-ID': '25a91c10-8623-4a0d-a48a-3de096d44b54',
}
// const authBody = {
// 'username': username,
// 'secret': password
// }
try {
//fetch the current user if it exists
const userExists = await axios.get('https://api.chatengine.io/users', {
headers: authObject
});
console.log('worked');
console.log(userExists);
// if user already exists and in localStorage redirect them to the chat room
if (userExists && (localStorage.getItem('username') && localStorage.getItem('username') === username)) {
setTimeout(() => {
history.push('/chat')
}, 1000)
return;
}
console.log('worked2');
// if user don't exists, create a new user
await axios.post('https://api.chatengine.io/users', {
headers: authHeader,
body: {
'username': username,
'secret': password
}
});
// //saving user's username and password to localStorage
// localStorage.setItem('username', username);
// localStorage.setItem('password', password);
//redirecting them to the login page to login
console.log('worked25');
history.push('/login')
} catch (error) {
console.log(error);
setError(`oops, something went wrong`)
}
}
请。忽略其他行并专注于 try/catch 路径 我正在尝试向用户添加新用户。但它不断向控制台抛出此错误。
Error: Request failed with status code 403
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:62)
我能做什么
【问题讨论】:
标签: javascript reactjs api web react-chat-engine