v1 vs idx-js vs direct-auth-convenience-method

Home   »   v1 vs idx-js vs direct-auth-convenience-method

// 
// Usename + Password signIn
//
const authTransaction = authClient.idx.authenticate({
  username: 'some-username',
  password: 'some-password'
});
const tokens = authTransaction.data.tokens;

//
// 2FA (password + email)
// 

// authTransaction handler to navigate user to proper next steps for interaction
const handleAuthTransaction = authTransaction => {
  const {
    data: {
      nextStep, tokens,
    }
  } = authTransaction;
  
  if (tokens) {
   // SUCCESS STATE !!!
   // redirect back to home or protected resources ...
   return;
  }
  
  if (nextStep.name === 'select-authenticator-authenticate') {
    // Interaction Needed
    // redirect/render select authenticator view for user interaction 
    ...
  } else if (name === 'challenge-authenticator') {
    // Interaction Needed
    // redirect/render challenge authenticator view for user interaction
    ...
  }
};

// Identify the user
let authTransaction = authClient.idx.authenticate({ username: 'some-username' });
handleAuthTransaction(authTransaction);

// route to handle select password authenticator
authTransaction = authClient.idx.authenticate({ authenticators: ['password'] });
handleAuthTransaction(authTransaction);

// route to verify password
authTransaction = authClient.idx.authenticate({ password: 'some-password' });
handleAuthTransaction(authTransaction);

// route to handle select email authenticator
authTransaction = authClient.idx.authenticate({ authenticators: ['email'] });
handleAuthTransaction(authTransaction);

// route to verify email challenge
authTransaction = authClient.idx.authenticate({ verificationCode: 'some-email-verification-code' });
handleAuthTransaction(authTransaction);
// 
// Usename + Password signIn
//

// Prepare meta
let meta = await getTransactionMeta(authClient);
const {
  interactionHandle,
  codeVerifier,
  codeChallenge,
  codeChallengeMethod
} = meta;
const { issuer, clientId, redirectUri } = authClient.options;
const version = IDX_API_VERSION;
let state = options.state || authClient.options.state;
let scopes = options.scopes || authClient.options.scopes;
meta = Object.assign(meta, { state, scopes }); // save back to meta

let idxResponse = await idx.start({
  // if interactionHandle is undefined here, idx will bootstrap a new interactionHandle
  interactionHandle,
  version,

  // OAuth
  clientId, 
  issuer,
  scopes,
  state,
  redirectUri,

  // PKCE
  codeVerifier,
  codeChallenge,
  codeChallengeMethod
});

// Save transaction meta so it can be resumed
saveTransactionMeta(authClient, meta);

// Get stateHandle from idxResponse for later use
const stateHand = idxResponse.context.stateHandle;

idxResponse = await idxResponse.proceed('identify', {
  identifier: 'some-username',
  credentials: {
    passcode: 'some-password'
  }
});

// If interactionCode is available exchange it for tokens
if (idxResponse.interactionCode) {
  const meta = authClient.transactionManager.load();
  const {
    codeVerifier,
    clientId,
    redirectUri,
    scopes,
    urls,
    ignoreSignature
  } = meta;

  tokens = await authClient.token.exchangeCodeForTokens({
    interactionCode,
    codeVerifier,
    clientId,
    redirectUri,
    scopes,
    ignoreSignature
  }, urls);
} else {
  // Keep handling remediation or throw error
}

// 
// 2FA (password + email)
//

// Prepare meta
let meta = await getTransactionMeta(authClient);
const {
  interactionHandle,
  codeVerifier,
  codeChallenge,
  codeChallengeMethod
} = meta;
const { issuer, clientId, redirectUri } = authClient.options;
const version = IDX_API_VERSION;
let state = options.state || authClient.options.state;
let scopes = options.scopes || authClient.options.scopes;
meta = Object.assign(meta, { state, scopes }); // save back to meta

let idxResponse = await idx.start({
  // if interactionHandle is undefined here, idx will bootstrap a new interactionHandle
  interactionHandle,
  version,

  // OAuth
  clientId, 
  issuer,
  scopes,
  state,
  redirectUri,

  // PKCE
  codeVerifier,
  codeChallenge,
  codeChallengeMethod
});

// Save transaction meta so it can be resumed
saveTransactionMeta(authClient, meta);

// Get stateHandle from idxResponse for later use
const stateHand = idxResponse.context.stateHandle;

idxResponse = await idxResponse.proceed('identify', {
  identifier: 'some-username',
});

...

// Select password authenticator and verify
idxReponse = await idxResponse.proceed('select-authenticator-authenticate', {
  id: 'id-of-password-authenticator'
});
idxResponse = await idxResponse.proceed('challenge-authenticator', {
  credentials: {
    passcode: 'some-password'
  }
});

// Select email authenticator and verify
idxReponse = await idxResponse.proceed('select-authenticator-authenticate', {
  id: 'id-of-email-authenticator'
});
idxResponse = await idxResponse.proceed('challenge-authenticator', {
  credentials: {
    passcode: 'some-email-verification-code'
  }
});

// If interactionCode is available exchange it for tokens
if (idxResponse.interactionCode) {
  const meta = authClient.transactionManager.load();
  const {
    codeVerifier,
    clientId,
    redirectUri,
    scopes,
    urls,
    ignoreSignature
  } = meta;

  tokens = await authClient.token.exchangeCodeForTokens({
    interactionCode,
    codeVerifier,
    clientId,
    redirectUri,
    scopes,
    ignoreSignature
  }, urls);
} else {
  // Keep handling remediation or throw error
}

Leave a Reply

Your email address will not be published. Required fields are marked *