Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Deekshith's avatar

How to restirct only logged in user in API implementation with react

I have implemented passport in laravel and shared the API's to develop with react js.

Below are the steps, 1.User registers 2.User login to his account. 3. user clicks on his profile. 4. Update the profile

So to update the profile i have created an POST api and added auth middleware to check user logged in or not so from react js i am sending bearer token in header.

after registration i am storing the token in reactjs local storage. so i can use stored token for future api calls.

But the problem is, If user in reactjs website clicks on profile then i should redirect the user to login page if he has not logged in.

For this i have to call login verify in all reactjs routes? I want to know how to verify user logged in or not on every route of reactjs webpages.

0 likes
5 replies
martinbean's avatar
Level 80

@deekshith I’m struggling to understand just what it is you’re asking.

If you’re using token-based authentication then you use that token to make API calls. There’s no “logged in” or not. You either get a successful response from the API if the token you provided was valid, or an unauthorised-like response if the token is not valid or not included in the request at all.

1 like
Deekshith's avatar

Thank you. Now i am calling an api to verify valid token or not on every webpage.

automica's avatar

Aren’t you using react? If so what do you mean by ‘on every webpage’?

Deekshith's avatar

@automica I am working on only API part i don;t know much about react. i will you an example.

After successful login we can able to fetch all pages of profile and other login protected pages. Pages are nothing but user profile page, user notification page, user activity page etc. So whenever user goes to these pages i should verify token is valid or not. so i am thinking to call api to check the token is valid or not on every page load.

automica's avatar

APIs return json.

Your react app will interact with your api by passing a valid token in a header which will be authorised by middleware before your api returns it’s response.

Once you have logged in with your react app and got a valid token, then you would pass that to your api.

You’d use something like laravel passport to deal with issuing tokens https://laravel.com/docs/8.x/passport

Please or to participate in this conversation.