-
Notifications
You must be signed in to change notification settings - Fork 12
Session status #190
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
base: main
Are you sure you want to change the base?
Session status #190
Conversation
whoadood
left a comment
There was a problem hiding this 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 👍
noahsb
left a comment
There was a problem hiding this 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); |
There was a problem hiding this comment.
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
| if (response.session === true) { | ||
| setAuthState(true); | ||
| } else { | ||
| setAuthState(false); |
There was a problem hiding this comment.
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
| useEffect(() => { | ||
| if (!authState) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
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`); |
There was a problem hiding this comment.
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
| const [currentUser, setCurrentUser] = useState<SanitizedUser>(); | ||
| const [error, setError] = useState<any>(); | ||
| const [loading, setLoading] = useState<boolean>(true); | ||
| const [authState, setAuthState] = useState(false); |
There was a problem hiding this comment.
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
})
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
Checklist: