Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserContext Hook #149

Open
timjacksonm opened this issue May 8, 2022 · 3 comments · May be fixed by #190
Open

UserContext Hook #149

timjacksonm opened this issue May 8, 2022 · 3 comments · May be fixed by #190
Assignees
Labels
bug Bug to be fixed community feedback wanted help wanted Extra attention is needed

Comments

@timjacksonm
Copy link
Collaborator

The UserContext Hook currently sends an api endpoint request to /api/user/getuser when the navbar component mounts. This action produces an error in the console anytime a user is not logged in.

Solution:
Refactor the UserContext hook to only send the get api request when a user has successfully logged in.

@whoadood
Copy link
Collaborator

whoadood commented May 8, 2022

I've been thinking about UserContext as well, I was thinking it could be beneficial to define Login and Logout functions in the context, that way the state setting function isnt exposed just functions that update state in a controlled way, would love for some feedback on this. A pattern I've been using in a side project for example:

export default function UserProvider({ children }) {
  const [user, dispatch] = useReducer(userReducer, {});
  const navigate = useNavigate();

  const logout = () => {
    localStorage.removeItem("user");
    dispatch({ type: "USER_LOGOUT" });
    navigate("/");
  };

  const login = (username, password) => {
    localStorage.setItem("user", username);
    dispatch({ type: "USER_LOGIN", user: { username, password } });
  };

  return (
    <UserContext.Provider value={{ user, login, logout }}>
      {children}
    </UserContext.Provider>
  );
}

@wil-gerard
Copy link
Owner

Thanks for the feedback on UserContext! Improving its functionality has long been on my to-do list - this gives me some great ideas to work off.

@whoadood #37 These issues are linked, and I'd like to tackle them together. I'll create a new branch for this issue and work with you on integrating your auth router solution once I've solved this. I should have something pushed by the end of the day - I'll tag you when I make the PR.

@wil-gerard wil-gerard self-assigned this May 16, 2022
@wil-gerard wil-gerard linked a pull request May 17, 2022 that will close this issue
@wil-gerard wil-gerard reopened this May 23, 2022
@github-actions github-actions bot added the triage Do not begin working on this issue until triaged by the team label May 23, 2022
@wil-gerard wil-gerard removed the triage Do not begin working on this issue until triaged by the team label May 23, 2022
@wil-gerard
Copy link
Owner

wil-gerard commented May 23, 2022

I didn't fully understand the issue when I linked #178 as a fix, so I'm reopening this. I think I have a solution here #188. I'll tag you both on the PR once it's up - I'd love feedback!

@wil-gerard wil-gerard added the bug Bug to be fixed label May 23, 2022
This was referenced May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug to be fixed community feedback wanted help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants