2

I am looking to add a Registration feature to my application that uses react-chat-engine but it's failing with a 403 error.

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

A user should be able to sign up, fill in their details and get logged in with this feature. I found a similar issue here how to make a post request to create new users in react-chat-engine but the solution does not work for me and I don't have the reputation to comment ;).

My code:

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')
         }
                            
    }

I have intentionally blurred out parts of my private key for obvious reasons. My Private key is valid, I have copied it directly from the dashboard. Also, I am able to add a user manually in the dashboard

What could I be missing?

damian-sketch
  • 151
  • 1
  • 14
  • Private-Key needs to be write in upper case. https://rest.chatengine.io/#6775ec07-601e-4730-ba08-d87bc81d019a users section. – JB_DELR Oct 20 '21 at 16:26
  • This makes no difference, I have tried a couple of times. Also, from the question I referenced above, the accepted solution is in lowercase so this can't be the problem – damian-sketch Oct 20 '21 at 18:56

1 Answers1

2

I think it should be two separate objects for Body and Headers

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