chatengine-io / react-chat-engine

React component for a cheap and easy chat API
138 stars 35 forks source link

How to create a new user programmatically? #174

Open lisprecovery opened 7 months ago

lisprecovery commented 7 months ago

does API support that? I'm tring out the chat engine and would like to create a user for the chat automatically.

KaziNizamul commented 7 months ago
import axios from 'axios';
/* service */
import {
  chatService,
} from '../../../services/urls';

export const getOrCreateMeAsUser = ({
  userName = '',
  phone_number = '',
  email = '',
}) => axios.get(chatService.getMeAsUser, {
  headers: {
    'project-id': import.meta.env.VITE_CHAT_PROJECT_ID,
    'user-name': userName || '',
    'user-secret': phone_number || '',
  },
})
  .catch(() => {
    const formdata = new FormData();
    formdata.append('email', email);
    formdata.append('username', userName);
    formdata.append('secret', phone_number);
    axios.post(chatService.createMeAsUser, formdata, {
      headers: {
        'private-key': import.meta.env.VITE_CHAT_PVT_KEY,
      },
    });
  });

Try doing something like this, and then use this "getOrCreateMeAsUser" in your useEffect mount