Skip to content

[BUG] Authentication Loading State Issue with Vite #1009

@nack43

Description

@nack43

Describe the bug
I'm currently developing a sample app with Frontendegg's authentication, utilizing the React integration guide. However, I am using Vite instead of Create React App (CRA) for this project.

The authentication process works as expected, but I encountered an issue when trying to display a loading component during this process. It seems that the isLoading state from useAuth() consistently returns false.

I discovered the isLoading property by inspecting the return type of useAuth(). However, I found no mention of it in the existing documentation.

This leads me to two questions:

  1. Is there an API reference for the React SDK that might explain this isLoading property?
  2. How can I correctly obtain the loading state during the authentication process?

To Reproduce
Steps to reproduce the behavior:

  1. Go to integration guide for React
  2. Follow the instruction and build the app with Vite
  3. Change const { user, isAuthenticated } = useAuth() with const { user, isAuthenticated, isLoading } = useAuth()
  4. Return loading component when isLoading is true e.g. if (isLoading) return <div>Loading...</div>
  5. You can see it's not rendered.

Expected behavior
I expect the 'isLoading' state to consistently return 'true' during the entire authentication process.

Screenshots
n/a

import { useEffect } from "react";
import "./App.css";
import { ContextHolder, useAuth, useLoginWithRedirect } from "@frontegg/react";

function App() {
  const { user, isAuthenticated, isLoading } = useAuth();

  const loginWithRedirect = useLoginWithRedirect();

  useEffect(() => {
    if (!isAuthenticated) {
      loginWithRedirect();
    }
  }, [isAuthenticated, loginWithRedirect]);

  const logout = () => {
    const baseUrl = ContextHolder.getContext().baseUrl;
    window.location.href = `${baseUrl}/oauth/logout?post_logout_redirect_uri=${window.location}`;
  };

  // HERE: This div should be rendered but the 'Click me to login' button will be rendered 
  if (isLoading) return <div>Loading...</div>;

  return (
    <div className="App">
      {isAuthenticated ? (
        <div>
          <div>
            <img src={user?.profilePictureUrl || undefined} alt={user?.name} />
          </div>
          <div>
            <span>Logged in as: {user?.name}</span>
          </div>
          <div>
            <button onClick={() => alert(user?.accessToken)}>
              What is my access token?
            </button>
          </div>
          <button onClick={() => logout()}>Click to logout</button>
        </div>
      ) : (
        <div>
          <button onClick={() => loginWithRedirect()}>Click me to login</button>
        </div>
      )}
    </div>
  );
}

export default App;

Additional context
I am using

Vite: 4.4.5
React: 18.2.0
@frontegg/react: 5.0.46

Feel free to ask me anything if you guys need more information. Thanks.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions