User Authentication - GluedIn
Introduction
This section outlines the detailed steps required to authenticate users in the GluedIn SDK from your application. The GluedIn SDK provides two primary authentication methods: Silent Authentication and Authentication with the Auth Module (which includes full UX/UI screens).
Methods to Authenticate the App with GluedIn SDK:
1. Silent Authentication: Allows a seamless login without user interaction.
2. Authentication with the Auth Module: Provides built-in UI screens for user interaction and complete authentication.
Silent Authentication:
Below are the steps for silent authentication in the system:
1. Import the GluedIn SDK Ensure that the GluedIn SDK is imported into your project.
import gluedin from "gluedin-shorts-js";
2. Set Up User Information Create a state object to hold the user data that will be passed for authentication.

const [formData, setFormData] = useState({
    email: "",            // User's email address for login/registration
    password: "",         // User's password
    deviceId: "1234XXXXX", // Unique identifier for the device
    deviceType: "web",    // Set as "web" for browser applications
    autoCreate: true,     // Automatically create user if email is not found
    termConditionAccepted: true, // Indicates acceptance of terms & conditions
});
Explanation of Fields:
  • email: The user's email for registration or login.
  • password: The password associated with the user.
  • deviceType: Always set to "web" for browser applications.
  • deviceId: A unique identifier for the user's device.
  • autoCreate: Set to true to create a new user if the provided email ID is not available in the database. Set to false for authentication only.
  • termConditionAccepted: Indicates whether the user has accepted the terms and conditions. The default is true.
3. Perform Silent Authentication Use the GluedInAuthModule to send user authentication data and handle responses.

var authLoginInput = new gluedin.GluedInAuthModule();
var userSignInResponse = await authLoginInput.AuthRawData(formData);

if (userSignInResponse.status === 200) {
    if (userSignInResponse.data.statusCode === 2001) {
        // Successfully authenticated, retrieve user meta-IDs
        var userModuleObj = new gluedin.GluedInUserModule();
        var userConfigResponse = await userModuleObj.getUserMetaIds("");

        if (userConfigResponse.status === 200 || userConfigResponse.status === 201) {
            let following = userConfigResponse.data.result.following || [];
            localStorage.setItem("following", JSON.stringify(following)); // Store user data locally
        }

        // Navigate to the vertical player page upon successful login
        navigate("/vertical-player");
    } else {
        // Handle authentication errors
        setErrors({ loginRespError: userSignInResponse.data.statusMessage });
    }
    setIsLoading(false);
} else {
    // Handle request failure
    setIsLoading(false);
    setErrors({ loginRespError: userSignInResponse.data.statusMessage });
}
Additional Notes:
  • Error Handling: Properly handle errors and loading states to improve user experience.
  • Navigation: The navigate("/vertical-player"); function redirects users to the specific page after successful authentication.
Authentication with the Auth Module:
This section provides step-by-step instructions for integrating the GluedIn SDK's Auth Module into your application, including the necessary UI/UX components for user login.
Steps to Implement Authentication with the Auth Module
1. Navigate to the LoginForm Component: Access the LoginForm component by following the path
src/components/Auth/LoginForm
2. Add the Login Code Snippet: Use the following code to create a login form within the LoginForm component:

<div className="sign-full">
<div className="sign-body">
<div className="res-logo desk-none text-center">
    <Link to="/">
    <img
        src="./gluedin/images/Brand.svg"
        alt="Logo"
        style={{ width: "200px", height: "80px" }}
        className="m-auto"
    />
    </Link>
</div>

<h3 className="text-blk ft-600 res-none tab-res-none">
    {t("Welcome-GluedIn")}
</h3>

{errors?.loginRespError && <p className="error">{errors?.loginRespError}</p>}

<form
    id="signInForm"
    onSubmit={handleSubmit}
    className="mt-t-40"
>
    {/* Email Input */}
    <div className="input-grp first-input-box">
    <label>{t("label-email")}</label>
    <input
        type="text"
        name="email"
        id="email"
        value={formData.email}
        onChange={handleInputChange}
    />
    {errors.email && <span className="error">{errors.email}</span>}
    </div>

    {/* Password Input */}
    <div className="input-grp">
    <label>{t("label-password")}</label>
    <div className="over-icon">
        <input
        type={passwordType}
        id="password"
        name="password"
        value={formData.password}
        onChange={handleInputChange}
        />
        <span onClick={togglePassword}>
        {passwordType === "password" ? (
            <img src="./gluedin/images/eye.svg" alt="Show password" />
        ) : (
            <i className="fa fa-eye-slash" aria-hidden="true"></i>
        )}
        </span>
    </div>
    {errors?.password && <span className="error">{errors?.password}</span>}
    </div>

    {/* Submit Button */}
    <div className="input-grp">
    <button
        type="submit"
        className="user-sign-in submit-button flex items-center justify-center disabled:pointer-events-none disabled:opacity-80"
        disabled={isLoading}
    >
        {isLoading ? <LoaderDark /> : t("btn-signIn")}
    </button>
    </div>

    {/* Guest Login Link */}
    <div className="input-grp text-center">
    <div className="link-text" style={{ display: `block` }}>
        <span>{t("text-account")}</span>&nbsp;
        <a href="/vertical-player" className="link">
        {t("guest-Login")}
        </a>
    </div>
    </div>
</form>
</div>
</div>
Handling Form Submission for Authentication
The following function handles the form submission, triggers the Auth Module, and processes the user login:

const handleSubmit = async (event) => {
  event.preventDefault();
  
  if (validateForm()) {
    setIsLoading(true);

    try {
      const authLoginInput = new gluedin.GluedInAuthModule();
      const userSignInResponse = await authLoginInput.AuthRawData(formData);

      if (userSignInResponse.status === 200) {
        if (userSignInResponse.data.statusCode === 2001) {
          const userModuleObj = new gluedin.GluedInUserModule();
          const userConfigResponse = await userModuleObj.getUserMetaIds("");

          if (userConfigResponse.status === 200 || userConfigResponse.status === 201) {
            const following = userConfigResponse.data.result.following || [];
            localStorage.setItem("following", JSON.stringify(following));
          }

          navigate("/vertical-player");
        } else {
          setErrors({ loginRespError: userSignInResponse.data.statusMessage });
        }
      } else {
        setErrors({ loginRespError: userSignInResponse.data.statusMessage });
      }
    } catch (error) {
      console.error("Error during authentication:", error);
      setErrors({ loginRespError: "An unexpected error occurred." });
    } finally {
      setIsLoading(false);
    }
  }
};
Explanation:
  • handleSubmit: Validates the form and sends user data to the GluedIn SDK for authentication.
  • AuthRawData: Sends raw user data to authenticate the user.
  • getUserMetaIds: Retrieves user metadata such as the list of followed users and stores it in localStorage.
  • Error Handling: Includes client-side and server-side error handling.
  • navigate("/vertical-player"): Redirects to the specified page upon successful login.
Key Points:
  • Ensure that the formData object is populated with the correct user details before submission.
  • Properly handle loading states (isLoading) and potential errors (errors) for better user experience.
  • The togglePassword function should toggle the password visibility between plain text and masked.