skip to Main Content

i have a registraion api that register user details to database it also genrates token using
laravel passport and saves the token into database

now i am trying to create a get api that will fetch all data from database using token generated

what i have tryed

in
api.php

Route::get('/get_user', [userDEtails::class, 'get_user']);

userDEtails.php controler

public function get_user(Reqest $req)
{

// what code must i add here
    
}

in post man i want to add token in header as Authorization and api to work

i am using Laravel Framework 9.41.0

enter image description here

enter image description here

3

Answers


  1. Since you’ve said you’re using Laravel Passport for this, try doing this to get the user (and check scopes)

    public function get_user(Request $request)
    {
        $user = $request->user();
    }
    

    This should ideally work. You will also need to make sure that your route is using the ‘auth:api’ middleware for your route like this –

    Route::get('/get_user', [userDEtails::class, 'get_user'])->middleware('auth:api');
    

    If for some reason you can’t use the ‘auth:api’ middleware for this route, and need to stick with ‘web’ or something else, you can use –

    $user = auth()->guard('api')->user()
    

    OR

    $user = $request->user('api')
    

    instead to get the user object.

    Login or Signup to reply.
  2. To fetch the data from the database using the token generated, you can follow the steps below:
    In your get_user function, first retrieve the token from the request header by using the header method on the request object.

    $token = $req->header('Authorization');
    

    Then you can use the TokenRepository provided by Laravel Passport to retrieve the authenticated user associated with the token. You can get an instance of the TokenRepository by using the app helper function:

    $tokenRepository = app(TokenRepository::class);
    $user = $tokenRepository->find($token);
    

    If the token is valid and an associated user is found, you can then use the User model to fetch the data from the database:

    $users = User::all();
    

    Lastly ,return the fetched data to the client by returning a JSON response

    return response()->json($users);
    

    But to take care of any unexpected error ,add some error handling in case the token is invalid or an associated user is not found.

    Login or Signup to reply.
  3. In Auth::check(), it will check token is valid or not. Based on that, it will return true or false.

    public function get_user(Request $request)
    {
        if (Auth::check()) {
            $users = User::all();
            return response()->json($users, 200);
        } else {
            return response()->json(['error' => 'Unauthorized'], 401);
        }
    }
    

    In addition, before you send a request to get_user(), write the login() method and get the access token. You can use passport or JWT


    If you use a Laravel passport can use bearerToken() to get the token and Auth::checkToken() to validate the token.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search