dcCustomAuthentication/dcCustomAuthentication.js

/*************************************************************************
 *
 * VLOCITY, INC. CONFIDENTIAL
 * __________________
 *
 *  [2014] - [2020] Vlocity, Inc.
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Vlocity, Inc. and its suppliers,
 * if any. The intellectual and technical concepts contained
 * herein are proprietary to Vlocity, Inc. and its suppliers and may be
 * covered by U.S. and Foreign Patents, patents in process, and are
 * protected by trade secret or copyright law. Dissemination of this
 * information and reproduction, modification or reverse-engineering
 * of this material, is prohibited unless prior written permission
 * is obtained from Vlocity, Inc.
 *
 */
import { LightningElement, track, api } from "lwc";
import dcSignIn from "c/dcSignIn"
import template from "./dcCustomAuthentication.html"

/**
 * @class DCCustomAuthentication
 * @extends {dcSignIn} Extends the dcSignIn class
 *
 * @classdesc
 * DCCustomAuthentication is the component created as a reference implementation for sign-in or sign-up done using redirect mechanism..<br/><br/>
 *
 * <b>Features</b> <br>
 *  1. Login/signup. <br/>
 * 
 * @example
 * Sample Usage -
 *
 * Include the component in your template by adding <c-dc-custom-authentication> custom tag
 *    
 * <c-dc-custom-authentication></c-dc-custom-authentication>
 * 
 * You could also use this component inside a slot for example-
 * 
 * <c-dc-check-out>
 *  <div slot="dc-signIn">
 *   <c-dc-custom-authentication></vlocity-dc-custom-authentication>
 *  </div>
 * </c-dc-checkout>
 * 
 * KEY INFO -
 *
 *  events fired : "vlocity-dc-sign-in"
 *
 *  events registered : none
 *
 *  Dependency - c-dc-custom-authentication is an independent component and has no dependency on any parent component.
 * 
 * Sample with slots -
 *
 * <c-dc-custom-authentication>
 *    <span slot="{slot_name}">
 *      Custom HTML elements goes here
 *    </span>
 * </c-dc-custom-authentication>
 *
 * List of available slots -
 *
 * | Slot names                       | Dynamic     | Description                               | Impact                              |
 * |----------------------------------|-------------|-------------------------------------------|-------------------------------------|
 * | dc-custom-auth-redirect-wrapper  | -           | Wrapper for custom auth redirect template | Replaces component template         |
 * | dc-custom-auth-redirect-content  | -           | Wrapper for custom auth redirect content  | Replaces template content           |
 */

export default class DCCustomAuthentication extends dcSignIn {
  @track loading = false;
  digitalCommerceSDK = null;
  constructor() {
    super();
    this.statusMessage = "";
    this.serverUrl = "https://firebase-redirect-auth.herokuapp.com/login";
    this.websocketUrl = "ws://firebase-redirect-auth.herokuapp.com";
  }
   // your properties and methods here
   connectedCallback() {
     super.connectedCallback();
   }

      /**
   * Method called to redirect user to authentication server.
   * @memberof DCCustomAuthentication
   */
  serverRedirect() {
    this.loading = true;
    this.statusMessage = "Waiting for user to login...";
    window.open(this.serverUrl);
    let webSocket = new WebSocket(this.websocketUrl);
    webSocket.onmessage = event => {
      this.loading = false;
      if (this.digitalCommerceSDK) {
        this.statusMessage = "Authorizing user to secure server...";
        this.digitalCommerceSDK.fire(
          "vlocity-dc-sign-in",
          "result",
          JSON.parse(event.data)
        );
      }
    };
  }

   /**
   * doSignIn method of <c-dc-sign-in> component overrided for custom signIn logic.
   * @memberof DCCustomAuthentication
   */
  async doSignIn(response) {
    let input = this.signInGetInput(response);
    let udpatedInput = await this.signInPreHook(input);
    this.signInPreSDKCalls();
    this.signInSDKCall(udpatedInput);
  }

  /**
   * signInGetInput method of VlocityDCSignin component overrided to create custom input.
   * @memberof DCCustomAuthentication
   */
  signInGetInput(input) {
    return Object.assign(
      this.digitalCommerceSDK.createAuthenticateUserInput(),
      {
        firstName: input.firstName,
        lastName: input.lastName,
        email: input.email,
        phoneNumber: input.phoneNumber ? input.userDetail.phoneNumber : " ",
        sessionToken: input.sessionToken
      }
    );
  }

   render()
     {
       return template;
     }
}