Support the ongoing development of Laravel.io →
Authentication Requests Session
Last updated 1 year ago.
0

what exactly are you trying to do here?

0

Basically can I forward some JSON to into $next($request) and then return the whole thing? If it isnt possible or not worth the trouble, can I add some kind of string value parameter into the $next($request) and then return the whole thing?

Last updated 8 years ago.
0

Hi Volvo,

I see what you're trying to do, however, the $next($request) has an endpoint which is either a controller or clousure on the route that is referring this request, meaning, when you create a middleware in Laravel application, you're protecting something the handle() method therefore evaluate your requirement to advance to whatever you're protecting.

If the handle test fails, the middleware will prevent the advancement of the request - which you will need to handle what happens in the body of the handle before returning the $next($request), on the other hand, if the test is satisfied the request will be allowed to the end point.

The job of the middleware is not to carry or transport any data between view,controller or route, it simply is to stay in the middle and protect the end point, hence "middleware".

Here is a quick scenario of how i would use this to return JSON data to view.

For this lets eliminate Controller lets just stay with Route.

I have a get uri that will return list of patient names, however, in order to get this data the requestor of the data must be logged in.

lets say the uri is * /patients-list *

The user requests the data by either http or ajax (doesn't matter to us because we'll return json data in either case)


Route::get('patient-list',['middleware'=>['auth']], function(){
    
 //Get your data from model or somewhere  
  $list = [
         '1'=>'Jon Doe',
         '2'=> 'Jane Doe',
          ...
            ];
  //here is where you should send the data with the json... any data sent from here is processed with the request. 
  // Also, if you were expecting to receive data from the request to be used within the end point where its form, or query     //string they will be available in Request  / Input class

      return response()->json($list, 200);   
    
});

Now your request will be processed here, unless the handle() in the auth middleware is satisfied, the requestor will never hit this end point. Normally, instead of handing the end point in the route, you'll handle them in a controller.

I hope this is able to help, or at least shed some light into what you're trying to accomplish, always please check the Laravel official documentation

Thanks.

Last updated 8 years ago.
0

Thank you sp01010011, but what if I want to return a token to the user after the auth middleware is satisfied? What is the best way to do so...

0

Once the auth middleware is satisfied the endpoint will return whatever you want to send back to the requestor which almost always is a controller, but you can also do it in the route per my example above.

Middleware should never deal with any business logic of any kind, its sole job is security - the watchdog.

$next($request) simply means go to where you were going, you're authorized (licensed) to go through this pathway but are not invited (authorized) to stay around and or conduct any kind of business.

Its like having a transit visa in a foreign country, while you're legally allowed to pass through the country, your visa does not allow you to stay in the country (even as tourist) let lone doing any kind of business.

I hope that helps.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.