【问题标题】:How do I create a user using a post request in react-chat-engine?如何在 react-chat-engine 中使用发布请求创建用户?
【发布时间】:2021-10-20 15:57:37
【问题描述】:

我希望在我的使用 react-chat-engine 的应用程序中添加注册功能,但由于 403 错误而失败。

Error: Request failed with status code 403
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.onloadend (xhr.js:66)

用户应该能够注册、填写详细信息并使用此功能登录。我在这里how to make a post request to create new users in react-chat-engine 发现了一个类似的问题,但该解决方案对我不起作用,我没有评论的声誉;)。

我的代码:

const SignupForm = () => {
    

    const [username, setUsername] = useState('');
    const [password, setPassword] = useState('');
    const [firstname, setFirst] = useState('');
    const [lastname, setLast] = useState('');
    const [error, setError] = useState('');

    // function to handle the submit
    const handleSubmit = async (e) => {
        e.preventDefault();

        //header for authentication
        const authObject = {'Private-Key': '4922e43b-xxxxx-xxxx'}
        
        // post request to create user
        try {
            await axios.post('https://api.chatengine.io/users/',
            { headers: authObject,
             body: {
                'username': username,
                'secret': password
                }
            });
            // login the user
            localStorage.setItem('username', username)
            localStorage.setItem('password', password)

            window.location.reload()
         } catch (error) {
             console.log(error)
             setError('Incorrect credentials, try again')
         }
                            
    }

出于显而易见的原因,我故意模糊了我的部分私钥。我的私钥是有效的,我直接从仪表板复制了它。另外,我可以在仪表板中手动添加用户

我可能会错过什么?

【问题讨论】:

标签: reactjs axios react-chat-engine


【解决方案1】:

我认为 Body 和 Headers 应该是两个独立的对象

axios.post(
  "https://api.chatengine.io/users/",
  {'username': username, 'secret': password}, // Body object
  {'headers': authObject} // Headers object
)
.then(r => console.log(r))

【讨论】:

  • 是的,这非常有效!谢谢你,亚当!
猜你喜欢
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
相关资源
最近更新 更多