1

I just did the Auth system through Laravel and I'm facing a problem : how to check in ReactJS the logged user? I need to get his ID to check if this user can modify the page. I don't know where to go at all.

Any tips would be welcome!

2
  • 1
    You don't actually need to do that. Just make the request (using an API token from Laravel's built-in API authentication system and let Laravel do the checks (using Laravel's authorization system). If it fails the checks, React will get a 403 Unauthorized error and act accordingly. Commented Feb 23, 2019 at 22:52
  • But what about if I'm making some other things like profile modification that has nothing to do with ReactJS? Commented Feb 23, 2019 at 23:17

1 Answer 1

1

you can't use the default authentication (using sessions) with a react app.

what you need is a system that creates an authentication token ( search for JWT or Laravel passport ) which identifies users, and since the HTTP protocol is stateless, in each request you must send that token to you backend service / API.

you can of course add a value representing the authentication ( true if you got a valid token ) to your global state just for UI or routing purpose.

check this medium article where the author explains how to create a token based auth with laravel using JWT.

you can also search about REST and oauth for more information.

Sign up to request clarification or add additional context in comments.

7 Comments

The fact is my application can be used for other things that only the React part (which represents 50% of the whole application), and I don't know how to manage that with authentication token since I'm not going to use sessions
i've edited my answer by adding a link to a good medium article.
Thanks for the link. This is still kind of messy in my head: would it significate that I would have to log through React only?
you"re saying that your app is half HTML, half react right ?
you should use the token based auth in all your application, i'll try to give you some links on how to do it when you"re fetching HTML files. edit : actually you can also return html files directly using api routes, just like web routes. laravel.com/docs/5.7/responses#view-responses
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.