Would it be advisable, if i am doing authentication in a middleware and adding some data to the \Illuminate\Http\Request $request
object and using that data in the controller by injecting \Illuminate\Http\Request $request
into the controller method?
The reason, the app needs to make a database call to find out if the credentials are valid and if it is, it returns something like a primary key that i use in subsequent db operations.
At the moment, everything is done in the controller. If i were to use a separate middleware for authentication, can i bind the data that my controller needs to the request object if the middleware check passes? if so, how should i go about doing it?
Inspiration - Expressjs way of binding and passing data along the request through a stack of middlewares / routes.
The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method.
Laravel itself creates an instance of the application, is the initial/first step. Next step will occur on the Kernel part of the application. The incoming request is sent to either the HTTP kernel or the console kernel, depending on the type of request that is entering the application .
Laravel's Illuminate\Http\Request class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request.
The has MethodThe $request->has() method will now return true even if the input value is an empty string or null . A new $request->filled() method has been added that provides the previous behaviour of the has() method. The $request->exists() method still works, it is just an alias for $request->has() .
I dont understand - why dont you just use the Laravel authenticator?
You say:
The reason, the app needs to make a database call to find out if the credentials are valid and if it is, it returns something like a primary key that i use in subsequent db operations.
And that is exactly what the Laravel Authenticator does?
Then in your controller you can just do
`auth()->user()` // gives you the user record
`auth()->id()` // user_id from the DB
`auth()->user()->name` // gives you the `name` column off the record. You can change it to anything.
Edit: Meanwhile - you can still use the Laravel Authenticator package whilst using a legacy system for authentication. In your middleware you can do something like this:
if (doLegacyCheckHere()) {
Auth::loginUsingId(1);
}
This means you can do your check via the neo4j graph db - and if it returns true
that the user is authenticated correctly - then you just log them into the Laravel system yourself.
Yes, that is probably a good way to do it, as the build-in Laravel Authentication system works the same way: you can access a logged in user via $request::user()
. See http://laravel.com/docs/5.0/authentication#retrieving-the-authenticated-user
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With