Connect to your client

Once you have setup your self-hosted parse-server, you can connect it to your client with version 1 of the Moralis JS SDK (moralis-v1). See https://github.com/MoralisWeb3/Moralis-JS-SDK-v1/). Additional you can also use react-moralis

Note that this SDK was is built for the hosted Moralis servers, and some features are currently not supported yet for self-hosted servers (see supported features).

You can connect to your app by providing the serverUrl and applicationId that you have set in the previous step

Example

For a client-side example that is compatible with the self-hosted parse-server see the parse-server-migration-react-client project:

Authentication

The authentication flow works a bit different when using the self-hosted server. This is because it is using the (Auth Api.

For this to work, we need tor request the message that needs to be signed from the server. An example on how to adjust for this is shown in the snippet below:

async function handleAuth(provider) {
  // Enable web3 to get user address and chain
  await Moralis.enableWeb3({ throwOnError: true, provider });

  if (!account) {
    throw new Error('Connecting to chain failed, as no connected account was found');
  }
  if (!chainId) {
    throw new Error('Connecting to chain failed, as no connected chain was found');
  }
  
  // Get message to sign from the auth api
  const { message } = await Moralis.Cloud.run('requestMessage', {
    address: account,
    chain: parseInt(chainId, 16),
    network: 'evm',
  });
  
  // Authenticate and login via parse
  await authenticate({
    signingMessage: message,
    throwOnError: true,
  });
}

// Example
handleAuth('metamask')
import { useMoralis } from 'react-moralis';
import Moralis from 'moralis-v1';

export const Example = () => {
  const { authenticate, enableWeb3 } = useMoralis();
  const [authError, setAuthError] = useState(null);
  const [isAuthenticating, setIsAuthenticating] = useState(false);

  const handleAuth = async (provider) => {
    try {
      setAuthError(null);
      setIsAuthenticating(true);

      // Enable web3 to get user address and chain
      await enableWeb3({ throwOnError: true, provider });
      const { account, chainId } = Moralis;

      if (!account) {
        throw new Error('Connecting to chain failed, as no connected account was found');
      }
      if (!chainId) {
        throw new Error('Connecting to chain failed, as no connected chain was found');
      }

      // Get message to sign from the auth api
      const { message } = await Moralis.Cloud.run('requestMessage', {
        address: account,
        chain: parseInt(chainId, 16),
        network: 'evm',
      });

      // Authenticate and login via parse
      await authenticate({
        signingMessage: message,
        throwOnError: true,
      });
    } catch (error) {
      setAuthError(error);
    } finally {
      setIsAuthenticating(false);
    }
  };

    return (<div>
    <button onClick={() => handleAuth("metamask")}>Authenticate via metamask</button>
  </div>
  )
}

Did this page help you?