Answers for "react native cognito passwordless"

0

react native cognito passwordless

export function sendAuthCode(email) {
  const loginDetails = new AuthenticationDetails({ Username: email });
  const preAuthCognitoUser = createCognitoUser(email);
  preAuthCognitoUser.setAuthenticationFlowType("CUSTOM_AUTH");

  return new Promise((resolve, reject) =>
    preAuthCognitoUser.initiateAuth(loginDetails, {
      onSuccess: resolve,
      onFailure: reject,
      customChallenge: async function() {
        await savePreAuthSession({
          email: preAuthCognitoUser.username,
          session: preAuthCognitoUser.Session
        }).catch(e =>
          console.warn(
            "Could not store session remotely. Access from an other browser tab won't be allowed",
            e
          )
        );
        resolve();
      }
    })
  );
}

export async function checkAuthCode(code, email) {
  const sessionData = await getPreAuthSession(email);
  const preAuthCognitoUser = createCognitoUser(email, sessionData.session);
  return new Promise((resolve, reject) =>
    preAuthCognitoUser.sendCustomChallengeAnswer(code, {
      onSuccess: resolve,
      onFailure: reject,
      customChallenge: async () => {
        await savePreAuthSession({
          email: preAuthCognitoUser.username,
          session: preAuthCognitoUser.session
        });
        const error = new Error("Code not valid, try again");
        error.code = "CodeValidationFail";
        reject(error);
      }
    })
  );
}

function createCognitoUser(email, session) {
  const cognitoUser = new CognitoUser({ Username: email, Pool: userPool });
  if (session) {
    cognitoUser.Session = session;
  }
  return cognitoUser;
}
Posted by: Guest on July-23-2020

Code answers related to "react native cognito passwordless"

Code answers related to "Javascript"

Browse Popular Code Answers by Language