Search code examples
laravelapioauth-2.0jwtlaravel-passport

What to return after login via API?


I'm creating an API server which will be consumed by a mobile app that I will work on later. I have yet to see any reference of API best practices related to user flow and returned data even after searching for several hours.

My question is whether the login response of an API should return the a personal access token with the refresh token along with the user info? Or should I just return the token and make another API call for getting the user info.

I could just do what I have in mind but I'm trying to learn the best practices so that I don't have to adjust a lot of things later.

I need suggestions as well as good references related to my question.

Thank you.


Solution

  • It depends on what you are using for your authentication. If you are using libraries like Laravel Passport or JWT, you can have the token endpoint which returns the access token, refresh token, validity period and the token type (Bearer). You can then have an authenticated endpoint which will be used to get a user's profile based of the token passed in the request header.

    However, if you go through the documentation for those libraries, in most there is an allowance to manually generate a token. You can use this in a custom endpoint that will return the token as well as the user profile Passport Manually Generate Token.

    If you are using JWT, you can also embed a few user properties in the token itself. The client can the get the profile info from the JWT itself without having to make a round trip to the server. Passport ADD Profile to JWT

    If you have a custom way in which you are handling authentication, you can pass the token as well as the user profile in the same response.

    In the end, it's up to you to decide what suits you best.