SolidJSS
SolidJSβ€’2y agoβ€’
2 replies
ZeroNP

context provider's values are not being updating after useContext gets user session info.

I am trying to use a context provider to be able to easily call my users data from any of the pages/routes. Originally, when I would try to read the data from my useAuth.user it would return "undefined" which lead me to adding a default value in AuthContextData. Now it inly shows the default data and it does not update the user or isLoading. I do not know what I am doing wrong,
const defaultAuthContextValue: AuthContextData = {
  user: () => {
    return null;
},
  isLoading: () => true, 
};
const AuthContext = createContext<AuthContextData>(defaultAuthContextValue);
export const AuthProvider: Component<{ children: JSX.Element }> = (props) => {
  const [user, setUser] = createSignal<Models.User<Models.Preferences> | null>(
    null
  );
  const [isLoading, setIsLoading] = createSignal(true);
  onMount(async () => {
    try {
      const session = await account.getSession("current");
      if (session) {
        const currentUser = await account.get();
        console.log("πŸš€ ~ onMount ~ currentUser:", currentUser);

        setUser(currentUser);
      }
    } catch (error) {
      console.log("No active session found");
    } finally {
      setIsLoading(false); // Set loading to false when done
    }
  });
  return (
    <AuthContext.Provider
      value={{
        user,
        isLoading,
      }}
    >
      {props.children}
    </AuthContext.Provider>
  );
};

testing in the login page
const Login: Component<{}> = (props) => {
  let user = useAuth.user;
  let isLoading = useAuth.isLoading;

Routes:
<Route>
        <AuthProvider>
            <Route path="/login" component={Login} />
            <Route path="/other" component={other} />
        </AuthProvider>
      </Route>
Screenshot_2024-07-12_at_9.26.36_PM.png
Was this page helpful?