javascript - React app using Laravel for API and user login. Question about retrieving currently logged in user from database

722

I'm wondering what would be the best way to retrieve the currently logged in user from the database when using Laravel strictly for it's built in user login and it's API. I've read that session does not work with API route and I'm not sure if creating a token for the user upon login is the right solution. If I understand correctly this would be easy to do with Auth but since I'm using React on the front end, it's a bit trickier.

My thought process would be to update the timestamp of that user's record in the database upon login and in the User controller, retrieve the record with the most recent timestamp, which would be that user, then return that user as a JSON object which I could access through the API URL endpoint on the frontend. I'm not sure though.

Any help or insight would be great. Thanks

720

Answer

Solution:

There's a documentation entry on this one. https://laravel.com/docs/5.8/passport

Is your requirement to a list of current logged in users? Or are you looking for an equivalent of$user = Auth::user();?

If it is the later you could simply send requests to the passport API described here:https://laravel.com/docs/5.8/passport#consuming-your-api-with-javascript

axios.get('/api/user')
.then(response => {
    console.log(response.data);
});

People are also looking for solutions to the problem: regex - Extract data between two string with PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.