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

Session status #190

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Session status #190

wants to merge 5 commits into from

Conversation

wil-gerard
Copy link
Owner

@wil-gerard wil-gerard commented May 30, 2022

Description

See issue #188

Related Issue

Resolves #188 and resolves #149

Motivation and Context

See issue #188 and #149

How Has This Been Tested?

Manually tested locally - a user can still create an account, log in, connect accounts, and follow

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.

Copy link
Collaborator

@whoadood whoadood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes look good to me 👍

Copy link
Collaborator

@noahsb noahsb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall! This is pretty similar to how I solved authentication in my project. Made some minor suggestions, but feel free to implement them as you see fit.


useEffect(() => {
try {
setLoading(true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So useState is an async operation that operates lazily. It's entirely possible that, because it gets initialized as false, quickly turns to true, then shortly after returns back to false after the api value is returned, React may choose to never flip the state back and forth for performance reasons.

Because of that, although it might feel a little weird, I'd suggest starting with an initial state of true (for loading), and set it to false once the api returns a value

Comment on lines +42 to +45
if (response.session === true) {
setAuthState(true);
} else {
setAuthState(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your api only returns true/false if there isn't an error thrown. You can safely replace all of these lines with:

setAuthState(response)

because that's doing the same thing without having to do those truthiness checks

Comment on lines +58 to +61
useEffect(() => {
if (!authState) {
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what this is supposed to do? The useEffect will only run on mount and you already fetch the current session in the useEffect above

res.redirect(
`${process.env.FRONTEND_ORIGIN_URL}/profile?id=${userDetails._id}`
);
res.redirect(`${process.env.FRONTEND_ORIGIN_URL}/profile?session=yes`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You technically don't need a new slug for this. In the profile page, you can check auth status to see if a user is logged in. You can conditionally return null depending on if there is a user found. Technically, a logged out user could still hit this URL

Comment on lines 32 to +35
const [currentUser, setCurrentUser] = useState<SanitizedUser>();
const [error, setError] = useState<any>();
const [loading, setLoading] = useState<boolean>(true);
const [authState, setAuthState] = useState(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, if you're using this many states to manage something, it's advised you use useReducer instead which does a better job at managing complex state. Alternatively, you could manage this all in one state as an object:

const [user, setUser] = useState({
  currentUser: null,
  error: null,
  loading: true,
  authState: false
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Session status UserContext Hook
3 participants