Skip to content

react-sdk: isUserLoading becomes false before user and is Authenticated are set as expected #1132

Description

@jeff-scifin

Here is a reproduction:
https://mx3nlt.csb.app/

Upon successful login, the AuthProvider will set isSessionLoading to false, then set isUserLoading to false, and then there is a delay before isAuthenticated becomes true and the user object is set. This make it very difficult to reliably use these values for redirection to an authentication route.

Image

Code is based on example from the sdk page:

import { Descope, useDescope, useSession, useUser } from "@descope/react-sdk";
import { useCallback } from "react";

const AppContent = () => {
  // NOTE - `useDescope`, `useSession`, `useUser` should be used inside `AuthProvider` context,
  // and will throw an exception if this requirement is not met
  // useSession retrieves authentication state, session loading status, and session token
  // If the session token is managed in cookies in project settings, sessionToken will be empty.
  const { isAuthenticated, isSessionLoading, sessionToken } = useSession();
  // useUser retrieves the logged in user information

  const { user, isUserLoading } = useUser();
  // useDescope retrieves Descope SDK for further operations related to authentication
  // such as logout

  const sdk = useDescope();

  const handleLogout = useCallback(() => {
    sdk.logout();
  }, [sdk]);

  console.log(
    "isSessionLoading",
    isSessionLoading,
    "isUserLoading",
    isUserLoading,
    "isAuthenticated",
    isAuthenticated
  );

  console.log("user", user);

  if (isSessionLoading || isUserLoading) {
    return <p>Loading...</p>;
  }

  if (isAuthenticated) {
    return (
      <>
        <p>Hello {user.name}</p>
        <button onClick={handleLogout}>Logout</button>
      </>
    );
  }

  return (
    <>
      <p>You are not logged in</p>
      <Descope flowId="sign-in" />
    </>
  );
};

export { AppContent };

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions