'How to check if a user is logged in or not in NextJS

I am using Codeigniter 4 as backend and have completed the login page but am now stuck at how to check if the user has logged in or not I am using Next JS as frontend. What I have thought is to use useState hook to set the value to true and pass it as props then use ternary operators to check whether the user has logged in or not but I do not know how to pass it as a prop and do not know if this is a good way to do it.



Solution 1:[1]

Honour of the post edited: react.js to next.js in the question.

Since, I am not into next.js development I can't continue to answer this post.

Thanks.

Below is my answer for react.js based auth control.

Its good enough to protect the Router.

{userLoggedIn
      && (
        <Route
          path="/home"
          component={() => <Home />}
        />
      )}

Brief:

Here userLoggedIn is a state where we toggle after the login API call

PS: Because, anyway you have to pass Tokens (may be jwt) in every API calls (API calls you wants to safe guard).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1