SMS authentication in self hosted servers?

How can I authenticate with SMS in React with a Moralis self hosted server?

Previously I was able to create my own connector, as it was recommended in these forums, but with the new auth api it doesn’t look like it’s extensible – is there a way to write my own connector or extend the magicLink implementation?

Does the new auth api include custom connectors?

For reference, this was my connector which worked to login users with SMS back in Moralis v1:

import { ethers } from 'ethers';
import AbstractWeb3Connector from './AbstractWeb3Connector';

export default class MagicWeb3Connector extends AbstractWeb3Connector {
  type = 'MagicLink';
  async activate({
    apiKey,
    newSession,

    // Options passed to loginWithMagicLink
    email,
    phone,
    showUI,
    redirectURI,
    loginCredential, // required for loginWithCredential after an email login callback

    // Options passed to new Magic creation
    network,
    locale,
    extensions,
    testMode,
    endpoint,
  } = {}) {
    let magic = null;
    let ether = null;

    if (!apiKey) {
      throw new Error('"apiKey" not provided, please provide Api Key');
    }
    if (!network) {
      throw new Error('"network" not provided, please provide network');
    }

    let Magic;
    try {
      Magic = require('magic-sdk')?.Magic;
    } catch (error) {
      // Do nothing. User might not need walletconnect
    }

    if (!Magic) {
      Magic = window?.Magic;
    }

    if (!Magic) {
      throw new Error('Cannot enable via MagicLink: dependency "magic-sdk" is missing');
    }

    try {
      magic = new Magic(apiKey, {
        network: network,
        ...(locale && { locale }),
        ...(extensions && { extensions }),
        ...(testMode && { testMode }),
        ...(endpoint && { endpoint }),
      });

      if (newSession) {
        if (magic?.user) {
          try {
            await magic?.user?.logout();
          } catch (error) {
            // Do nothing
          }
        }
      }

      ether = new ethers.providers.Web3Provider(magic.rpcProvider);
      if (loginCredential) {
        console.log("attempting login with credential")
        await magic.auth.loginWithCredential('loginCredential');
      } else if (email) {
        await magic.auth.loginWithMagicLink({
          email: email,
          ...(showUI && { showUI }),
          ...(redirectURI && { redirectURI }),
        });
      } else if (phone) {
        await magic.auth.loginWithSMS({
          phoneNumber: phone,
          ...(showUI && { showUI }),
          ...(redirectURI && { redirectURI }),
        });
      } else {
        throw new Error("Neither phone nor email were provided.")
      }
    } catch (err) {
      throw new Error('Error during enable via MagicLink, please double check network and apikey');
    }

    const loggedIn = await magic.user.isLoggedIn();
    if (loggedIn) {
      const signer = ether.getSigner();
      const { chainId } = await ether.getNetwork();
      const address = (await signer.getAddress()).toLowerCase();
      // Assign Constants
      this.account = address;
      this.provider = ether.provider;
      this.chainId = `0x${chainId.toString(16)}`;
      // Assign magic user for deactivation
      this.magicUser = magic;
      this.subscribeToEvents(this.provider);
      return {
        provider: this.provider,
        account: this.account,
        chainId: this.chainId,
      };
    }
    throw new Error('Error during enable via MagicLink, login to magic failed');
  }

  deactivate = async () => {
    this.unsubscribeToEvents(this.provider);
    if (this.magicUser) {
      await this.magicUser.user.logout();
    }
    this.account = null;
    this.chainId = null;
    this.provider = null;
  };
}

Hey @snazzx,

I recommend that you do not use the Moralis self-hosted server if you’d like to include custom authentication flow.

And yes, using the Auth API it is going to be more extensible and you will be able to integrate the Magic.link SMS authentication feature.

You can checkout how to integrate Auth API with Magic.link here, there will be some modifications since you’re using SMS authentication instead of passwordless login as demonstrated in the docs tutorial.

Hope this helps :grinning_face_with_smiling_eyes: Let me know if you have any other questions.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.