Lumen: Class Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse does not exist - php

I'm new to Lumen and i am trying to setup authentification using OAuth2 with this tutorial
when i try to access any route i got this error:
ReflectionException in Container.php line 738:
Class Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse does not exist
in Container.php line 738
at ReflectionClass->__construct('Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse') in Container.php line 738
at Container->build('Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse', array()) in Container.php line 633
at Container->make('Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse', array()) in Application.php line 205
at Application->make('Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse') in Pipeline.php line 123
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Laravel\Lumen\Routing\{closure}(object(Request)) in OAuthExceptionHandlerMiddleware.php line 36
at OAuthExceptionHandlerMiddleware->handle(object(Request), object(Closure))
at call_user_func_array(array(object(OAuthExceptionHandlerMiddleware), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Laravel\Lumen\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in RoutesRequests.php line 626
at Application->sendThroughPipeline(array('LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware', 'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse'), object(Closure)) in RoutesRequests.php line 382
at Application->dispatch(object(Request)) in RoutesRequests.php line 327
at Application->run(object(Request)) in index.php line 28
bootstrap\app.php
$app->middleware([
'LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse' // <--- added
]);
$app->routeMiddleware([
'check-authorization-params' => 'Optimus\OAuth2Server\Middleware\CheckAuthCodeRequestMiddleware',
'csrf' => 'Laravel\Lumen\Http\Middleware\VerifyCsrfToken',
'oauth' => 'Optimus\OAuth2Server\Middleware\OAuthMiddleware',
'oauth-owner' => 'Optimus\OAuth2Server\Middleware\OAuthOwnerMiddleware'
]);
$app->register(App\Providers\EventServiceProvider::class);
$app->register('LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider');
$app->register('Optimus\OAuth2Server\OAuth2ServerServiceProvider');
routes.php
$app->group(['prefix' => 'v1','namespace' => 'App\Http\Controllers'], function($app)
{
$app->get('post','PostController#index');
}

Just run
composer require illuminate/cookie
Then add this code at bootstrap/app.php
$app->singleton('cookie', function () use ($app) {
return $app->loadComponent('session', 'Illuminate\Cookie\CookieServiceProvider', 'cookie');
});
$app->bind('Illuminate\Contracts\Cookie\QueueingFactory', 'cookie');
ref https://github.com/esbenp/lumen-api-oauth/issues/23

Related

Laravel Hesto multi-auth NotFoundHttpException

I use Hesto multi-auth to make multiple authentication. I made controllers, routes and others, but when I try open url e.g. localhost:8000/student/register it throws NotFoundHttpException.
This is example of route
Route::group(['domain' => 'student.' . env('APP_DOMAIN')], function () {
Route::get('/login', 'StudentAuth\LoginController#showLoginForm');
Route::post('/login', 'StudentAuth\LoginController#login');
Route::post('/logout', 'StudentAuth\LoginController#logout');
Route::get('/register', 'StudentAuth\RegisterController#showRegistrationForm');
Route::post('/register', 'StudentAuth\RegisterController#register');
Route::post('/password/email', 'StudentAuth\ForgotPasswordController#sendResetLinkEmail');
Route::post('/password/reset', 'StudentAuth\ResetPasswordController#reset');
Route::get('/password/reset', 'StudentAuth\ForgotPasswordController#showLinkRequestForm');
Route::get('/password/reset/{token}', 'StudentAuth\ResetPasswordController#showResetForm');
});
And this is exception
in RouteCollection.php line 179
at RouteCollection->match(object(Request)) in Router.php line 533
at Router->findRoute(object(Request)) in Router.php line 512
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
at require_once('F:\\Studia\\Sem 3 mgr\\Praca magisterska\\thesis_system\\public\\index.php') in server.php line 21
It looks like you have only defined path for /register on a domain student.<your-app-domain>, but in your request the domain is localhost and the path is /student/register, so it does not match any of the path definitions.

Lumen: $user is null inside Policy

I am trying to validate if a user exists inside a Policy in Lumen.
This is my Controller function:
public function create(Request $request)
{
$rules = array(
'content' => 'required|string',
);
$user = $request->user();
$this->validate($request, $rules);
$this->authorize('create', Card::class);
This is the Policy:
public function create(User $user)
{
// As long as the user is real, allowed
return $user->id != null;
}
The $user inside the create Policy seems to be null. This is the error:
HttpException in Handler.php line 90:
This action is unauthorized.
in Handler.php line 90
at Handler->render(object(Request), object(AuthorizationException)) in Handler.php line 48
at Handler->render(object(Request), object(AuthorizationException)) in Pipeline.php line 78
at Pipeline->handleException(object(Request), object(AuthorizationException)) in Pipeline.php line 54
at Pipeline->Laravel\Lumen\Routing\{closure}(object(Request)) in Authenticate.php line 42
at Authenticate->handle(object(Request), object(Closure), 'api') in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Laravel\Lumen\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in RoutesRequests.php line 780
at Application->sendThroughPipeline(array('App\\Http\\Middleware\\Authenticate:api'), object(Closure)) in RoutesRequests.php line 626
at Application->handleFoundRoute(array(true, array('uses' => 'App\\Http\\Controllers\\CardController#create', 'middleware' => array('auth:api')), array())) in RoutesRequests.php line 528
at Application->Laravel\Lumen\Concerns\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Laravel\Lumen\Routing\{closure}(object(Request)) in ExampleMiddleware.php line 18
at ExampleMiddleware->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Laravel\Lumen\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in RoutesRequests.php line 780
at Application->sendThroughPipeline(array('App\\Http\\Middleware\\ExampleMiddleware'), object(Closure)) in RoutesRequests.php line 534
at Application->dispatch(object(Request)) in RoutesRequests.php line 475
at Application->run(object(Request)) in index.php line 29
I meet the same problem.
But the issue is nothing about the policy class.
This is resolved by using fully qualified class name in AuthServiceProvider file,
change:
Gate::policy(Post::class, PostPolicy::class);
to:
Gate::policy(\App\Post::class, \App\Policies\PostPolicy::class);
hope this help.

Cannot login with username on Sentinel (Laravel5)

I am trying out the sample Sentinel methods. I just want to register a user and authenticate him using his username.
I modified the user table by replacing the email attribute with username and did the migration. Here is my users table.
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username');
$table->string('password');
$table->text('permissions')->nullable();
$table->timestamp('last_login')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
$table->unique('username');
});
When I try to call the below method to register a sample user, I am getting an error.
Code:
$credentials = [
'username' => 'testuser123',
'password' => '123'
];
$activation = Sentinel::registerAndActivate($credentials);
Error:
InvalidArgumentException in IlluminateUserRepository.php line 271:
No [login] credential was passed.
in IlluminateUserRepository.php line 271
at IlluminateUserRepository->validateUser(array('username' => 'testuser123', 'password' => '123')) in IlluminateUserRepository.php line 154
at IlluminateUserRepository->validForCreation(array('username' => 'testuser123', 'password' => '123')) in Sentinel.php line 164
at Sentinel->register(array('username' => 'testuser123', 'password' => '123'), true) in Sentinel.php line 191
at Sentinel->registerAndActivate(array('username' => 'testuser123', 'password' => '123')) in Sentinel.php line 97
at Sentinel::__callStatic('registerAndActivate', array(array('username' => 'testuser123', 'password' => '123'))) in AuthenticationController.php line 196
at Sentinel::registerAndActivate(array('username' => 'testuser123', 'password' => '123')) in AuthenticationController.php line 196
at AuthenticationController->testFunction()
at call_user_func_array(array(object(AuthenticationController), 'testFunction'), array()) in Controller.php line 246
at Controller->callAction('testFunction', array()) in ControllerDispatcher.php line 162
at ControllerDispatcher->call(object(AuthenticationController), object(Route), 'testFunction') in ControllerDispatcher.php line 107
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 108
at ControllerDispatcher->callWithinStack(object(AuthenticationController), object(Route), object(Request), 'testFunction') in ControllerDispatcher.php line 67
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\AuthenticationController', 'testFunction') in Route.php line 204
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 701
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Router.php line 703
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 670
at Router->dispatchToRoute(object(Request)) in Router.php line 628
at Router->dispatch(object(Request)) in Kernel.php line 214
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 115
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53
at require_once('/home/pranavaghanan/Documents/GitHub Projects/To Integrate For Release 1/SEP/public/index.php') in server.php line 21
Is there any way to correct the problem?
Thanks.
The problem is that in the credentials parameter it requires email not username.
So change it like this and try agian:
$credentials = [
'email' => 'testuser123',
'password' => '123'
];
In case you have to use username instead of email to authenticate, read this answer.

Laravel 5 form submit creates an error MethodNotAllowedHttpException

I'm using Laravel 5. When editing a form a get this error
MethodNotAllowedHttpException
There are a few posts but I think it may be code specific. I'd rather not use the resource route methods if possible. Route:
Route::get('profile/edit', array('as' => 'admin.profile.edit', 'uses' => 'ProfileController#edit'));
Route::post('profile/edit', array('as' => 'admin.profile.update', 'uses' => 'ProfileController#update'));
The form is :
{!!Form::model($profile,array('method'=>'Patch','route' =>array('admin.profile.update'))) !!}
Controller:
public function update()
{
$user_id=$this->user->id;
$input=array_except(Input::all(),'_method');
Profile::where('user_id','=',$user_id)->update($input);
return Redirect::route('admin.profile.index');
}
Here is the error info:
n RouteCollection.php line 207
at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) in RouteCollection.php line 194
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'POST')) in RouteCollection.php line 142
at RouteCollection->match(object(Request)) in Router.php line 729
at Router->findRoute(object(Request)) in Router.php line 652
at Router->dispatchToRoute(object(Request)) in Router.php line 628
at Router->dispatch(object(Request)) in Kernel.php line 214
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 115
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53
Thanks any help appreciated
Can you change this:
Route::post('profile/edit', array(
'as' => 'admin.profile.update',
'uses' => 'ProfileController#update'
));
To this:
Route::patch('profile/edit', array(
'as' => 'admin.profile.update',
'uses' => 'ProfileController#update'
));
I think your form action may need a matching route verb
http://laravel.com/docs/5.1/routing#basic-routing
For the second issue:
Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update user_profile` set _method = PATCH, _token =
Add this to the top of your controller
use Illuminate\Http\Request;
Then update your method as so:
public function update(Request $request)
{
$user_id = $this->user->id;
$input = $request->except('_method', '_token');
Profile::where('user_id','=', $user_id)->update($input);
return Redirect::route('admin.profile.index');
}
In my case works adding protected $guarded = ['_token']; in Model file.

Laravel 5 Routing Issue - Bad Method Call Exception

I'm attempting to simply send a 'PUT' request to one of my routes (which is called as a resource in the route file), yet I recieve this traceback as the response.
Whoops, looks like something went wrong.
1/1 BadMethodCallException in Controller.php line 273: Method [show] does not exist.
in Controller.php line 273
at Controller->__call('show', array('1'))
at AccountControllerV2->show('1')
at call_user_func_array(array(object(AccountControllerV2), 'show'), array('account' => '1')) in Controller.php line 246
at Controller->callAction('show', array('account' => '1')) in ControllerDispatcher.php line 162
at ControllerDispatcher->call(object(AccountControllerV2), object(Route), 'show') in ControllerDispatcher.php line 107
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 108
at ControllerDispatcher->callWithinStack(object(AccountControllerV2), object(Route), object(Request), 'show') in ControllerDispatcher.php line 67
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\API\V2\AccountControllerV2', 'show') in Route.php line 198
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 131
at Route->run(object(Request)) in Router.php line 692
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Authenticate.php line 47
at Authenticate->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Router.php line 694
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 661
at Router->dispatchToRoute(object(Request)) in Router.php line 619
at Router->dispatch(object(Request)) in Kernel.php line 214
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in HttpsProtocol.php line 15
at HttpsProtocol->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 115
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53
I cannot for the life of me figure out why this is happening.
Here is a code snippet from the Controller itself (which extends BaseController):
/**
* Update the currently logged User in storage.
*
* #return Response 201
* #return jsonArray account
* #return string message
*/
public function update($account_id) {
$input = Input::all();
$message = 'Account Successfully Updated';
$user = Auth::User();
And here is the Route declaration:
Route::group(array('prefix' => 'api/v2', 'namespace' => 'API\V2', 'middleware' => 'auth'), function()
{
Route::resource('challenges', 'ChallengeControllerV2', ['only'=> ['index','store','update','destroy']]);
Route::resource('groups', 'GroupControllerV2');
Route::resource('account', 'AccountControllerV2');
The call is being made to the correct path. Any ideas why I'm getting what seems to be an illogical response? There is no additional middleware/filters attached to the controller than what is shown.
It turns out that in Laravel 5 if you do not specify the resources available on a Route it will assume that they are all there (unlike in Laravel 4 where it checks to see what resources are available in all controllers). So I had to change the line like so.
Route::resource('account', 'AccountControllerV2', ['only'=> ['index','update','destroy']]);

Categories