I'm using backend too. So, i don't know if i can use Codesandbox to show my issue. I'll share some codes to indicate my issue
This is my Login page. I'm using "useContext". I created a useEffect to see if user already logged in. authState.status is checking if there's a cookie in the browser in the backend.
When I click "Login button". loginUser() function is working. This function is creating new user. If there's not an error, it changes the state and creating a session. After that its navigate to profile
This is my App.js. In useEffect, I'm making a get request to /login to checking if there's a cookie in the browser in the backend. app.post and app.get are totally different things in the backend
In you App.js you aren't setting the same auth state as you do in you Login component. It seems that you have missunderstood the concept of context a bit. Or there are some important bits of code missing here. You need to create a "AuthProvider" that stores your context. And you can handle the login and the "check auth cookie" request in that Provider instead of doing it in different parts of the app.
I can't really point you in the right correction based on this. I recommend reading kentcdodds.com/blog/authentication... to get some understanding of how it can be done.
Is your app.get("/login", ...) function only returning what got passed in req.session.user or have you stripped out some logic there?
Also this useEffect needs to have authState.status in its dependency array like this:
I'm using backend too. So, i don't know if i can use Codesandbox to show my issue. I'll share some codes to indicate my issue
This is my Login page. I'm using "useContext". I created a useEffect to see if user already logged in. authState.status is checking if there's a cookie in the browser in the backend.
When I click "Login button". loginUser() function is working. This function is creating new user. If there's not an error, it changes the state and creating a session. After that its navigate to profile
This is my App.js. In useEffect, I'm making a get request to /login to checking if there's a cookie in the browser in the backend. app.post and app.get are totally different things in the backend
this is app.get
I'm sorry if it is too much ask :(
My issue is. When i go to Profile in the first time. I have to refresh the page to see the email from the context
In you App.js you aren't setting the same auth state as you do in you Login component. It seems that you have missunderstood the concept of context a bit. Or there are some important bits of code missing here. You need to create a "AuthProvider" that stores your context. And you can handle the login and the "check auth cookie" request in that Provider instead of doing it in different parts of the app.
I can't really point you in the right correction based on this. I recommend reading kentcdodds.com/blog/authentication... to get some understanding of how it can be done.
Is your
app.get("/login", ...)function only returning what got passed inreq.session.useror have you stripped out some logic there?Also this useEffect needs to have authState.status in its dependency array like this:
otherwise it will only check that status on initial render. You can read about it here reactjs.org/docs/hooks-reference.h...
Yess. I already have an AuthContext.Provider in App.js and I imported it
app.get is just returning req.session.user. I created it in app.post('/login'). Here it is
Thank you for the tips and the sources. I will check immediately