3

I have Completely assigned the Passport REST API and i get token and tested on PostMan enter image description here

and it retrieve the data with this Auth

this is normal js html with laravel blade view (NOT VUE.JS)

<script>
$.ajax({
  method: 'POST',
  dataType: 'json',
  url: "**********/api/User/GetPost",
  headers: {
      'Authorization':'Bearer Tokenblablabla',
      'Content-Type':'application/json'
  },
  data: null,
  success: function(data){
  console.log('succes: '+data);
  }

});

this is the controller function

    public function GetPosts(Request $request){
         $data3="Test"
         return response()->json([
            'Success'=> true,
            'Message'=>'8',
            'Data' => $data3,
         ], 200);
    }

this is api.php // this will return a json with posts details

Route::group(['middleware'=>'auth:api'],function(){
      Route::post('/GetPosts','Wall\PostsController@GetPosts');
});

this is web.php

Route::get('/GetPost', function () {
     return view('getpostview');
})->middleware('auth:api');

i get confused i cannot send Auth beside i cannt retrieve the json from API iam 3 days STUCK in this problem what iam need to connect the normal laravel blade to another laravel passport REST API

1 Answer 1

0

The Blade templates generated by Laravel for authentication can works with Laravel Passport, JWT, etc.

The thing that you need to do is configure Passport properly.

Do not forget to modify the driver specified in the API config/auth.php file:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

Also, to retrieve a json response add this header in your requests:

'Accept' : 'application/json'

So your request should be like this:

$response = $client->request('GET', 'some-url/api/getPosts', [
    'headers' => [
        'Accept' => 'application/json',
        'Authorization' => 'Bearer '.$accessToken,
    ],
]);

Side note

I encourage you to read this presentation, "Teaching a Dog to REST", to create a clean, intuitive and easy to learn API properly using HTTP verbs and good practices.

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

2 Comments

you didnt get what i want i use my own API i did for mobile
Add more detail then, I can't see about two Laravel projects anywhere

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.