Call to undefined method Vender\Package\Auth::extend() in package - laravel

I'm trying to create an authentication package (module) using jwt-auth that can be reused across all of my applications. But I receive errors.
Framework Laravel
Framework version 8.46.0
jwt-auth version ^1.0.0-beta.3#dev
PHP version 7.4.20
Steps to reproduce
Create a fresh laravel project
Create a fresh package inside it
Add "tymon/jwt-auth": "^1.0.0-beta.3#dev", as requirement to the composer.json of the package
Implement JWTSubject in your User model
Try to run any artisan command.
I receive this error:
Error
Call to undefined method Vendor\Package\Auth::extend()
at vendor/tymon/jwt-auth/src/Providers/AbstractServiceProvider.php:98
94▕ * #return void
95▕ */
96▕ protected function extendAuthGuard()
97▕ {
➜ 98▕ $this->app['auth']->extend('jwt', function ($app, $name, array $config) {
99▕ $guard = new JWTGuard(
100▕ $app['tymon.jwt'],
101▕ $app['auth']->createUserProvider($config['provider']),
102▕ $app['request']
+8 vendor frames
9 [internal]:0
Illuminate\Foundation\Application::Illuminate\Foundation\{closure}()
+5 vendor frames
15 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
My package name is also Auth. I'm stuck at solving this problem. Any help?

Related

How to exchange factory() function in Laravel?

I learn Laravel from the tutorial and there is use factory(). In my project show me an error, Undefined function factory. I know that in Laravel 9 factory() does not exist, but someone could help me how can I modify the below code that will run in Laravel 9.
public function run(Faker $faker)
{
factory(
Task::class,
$faker->numberBetween(25, 50)
)->create();
}
factory() was used until Laravel 8. In newer versions of Laravel model factories are used: https://laravel.com/docs/9.x/eloquent-factories#main-content

Generating models from existing database at once

I'm trying to generate models from an existing database at once without having to do it separately for all tables. I have tried to do this with reliese/laravel. I have executed:
php artisan -v code:models
However, I'm getting the following error.
ErrorException : mkdir(): Invalid path
at C:\xampp\htdocs\schaden\vendor\laravel\framework\src\Illuminate\Filesystem\Filesystem.php:466
462| if ($force) {
463| return #mkdir($path, $mode, $recursive);
464| }
465|
466| return mkdir($path, $mode, $recursive);
467| }
468|
469| /**
470| * Move a directory.
Exception trace:
1 mkdir("")
C:\xampp\htdocs\schaden\vendor\laravel\framework\src\Illuminate\Filesystem\Filesystem.php:466
I'm not posting the full error stack here. Can anyone help?
Probably the best solution is using the package Eloquent Model Generator that you can find on github at https://github.com/krlove/eloquent-model-generator.
Then you can easily use, for example, php artisan krlove:generate:model User --table-name=users or php artisan krlove:generate:model MyModel --table-name=my_models and use some of the package options.

How do I convert Laravel 4.2 beforeFilter in controller into custom middleware in Laravel 5.2

I'm in the process of upgrading a Laravel 4.2 app I inherited to Laravel 5.2. The app has multiple roles for logged in users that were handled with a before filter. Each controller has an array of functions and roles allowed for those functions:
public $actionFilter = [
'directories-create'=>['super','tsr'],
'directories-destroy'=>['super','tsr'],
'directories-edit'=>['super','tsr'],
'directories-directoryinfo'=>['super','tsr','admin'],
'directories-index'=>['super','tsr'],
'directories-store'=>['super','tsr'],
'directories-update'=>['super','tsr'],
];
then in the construct function it calls two beforeFilters that were in Controller.php
public function __construct()
{
$this->beforeFilter('#filterAuthorization');
$this->beforeFilter('#rerouteSite');
}
Controller.php had a public function filterAuthorization that checked if user's role had access to the route, and a public function rerouteSite that allowed user to stay on the same page but switch between accounts (for example, for a support rep).
I've spent a fair amount of time reading the manual, Googling and reading various tutorials, but I'm still unclear how to get my route-role array connected to the auth middleware. The Laravel docs provide syntax but not the context and the examples I've read either take a different approach or have a different usecase from mine.
I tried leaving the filter functions in Controller.php and calling them like this in the construct:
public function __construct()
{
$this->middleware('#filterAuthorization');
$this->middleware('#rerouteSite');
}
I get an error message: "Class #filterAuthorization does not exist"
I tried putting those functions in app\Http\Middleware\Authenticate, but I get the same error message: "Class #filterAuthorization does not exist"
I followed the steps on Matt Stauffer's blog here (https://mattstauffer.com/blog/laravel-5.0-middleware-filter-style/) and here (https://mattstauffer.com/blog/passing-parameters-to-middleware-in-laravel-5.1/) and on Nwanze Franklin's post here (https://dev.to/franko4don/deep-dive-into-middlewares-in-laravel-doo) as follows.
Create two new middleware files with Artisan
php artisan make:middleware FilterAuthorization
php artisan make:middleware RerouteSite
Edit the new middleware files with the functions from the old Controller.php
Register the new middleware in App\Http\Kernel
protected $routeMiddleware = [
'filterauth' => \Illuminate\Routing\Middleware\FilterAuthorization::class,
'reroutesite' => \Illuminate\Routing\Middleware\RerouteSite::class,
];
Edit the public function __contstruct() in the Controllers than need filtering
public function __construct()
{
$this->middleware('FilterAuthorization');
$this->middleware('RerouteSite');
}
Run
composer dump-autoload
php artisan clear-compiled
php artisan optimize
and I still get the same error:
Class FilterAuthorization does not exist
I'm sure there's a simple way to put this together without rewriting the whole role authorization system. Can someone point me in the right direction?
The kernel registration needs to reference the correct file locations as follows:
'filterauth' => \App\Http\Middleware\FilterAuthorization::class,
'reroutesite' => \App\Http\Middleware\RerouteSite::class,
And the controller boot should use the aliases rather than the class names:
public function __construct()
{
$this->middleware('filterauth');
$this->middleware('reroutesite');
}
Then Laravel can find the custom middleware.

JWTGenerateCommand::handle() does not exist

I am using Laravel 5.4 and JWT Auth Library for user authentication in API development. After installation while i am running php artisan jwt:generate then it throws me error of
Method Tymon\JWTAuth\Commands\JWTGenerateCommand::handle() does not exist
Any idea what i am missing ?
This error generally display when you install jwt package in laravel 5.5 version. then after you set service providers and run following command.
php artisan jwt:generate
then you seen this error message in terminal.
how to resolve it? simple follow this step
Step - 1 Re-install package
composer require tymon/jwt-auth:dev-develop --prefer-source
or the following is a new release package use laravel 6.X
composer require tymon/jwt-auth:1.0.*
in this developement version this errors fixed.
Step - 2 Set Service Provider
'providers' => [
....
Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class to
Tymon\JWTAuth\Providers\LaravelServiceProvider::class
],
Step - 3 Generate key
php artisan jwt:secret
i found this solution from here https://laravelcode.com/post/method-tymonjwtauthcommandsjwtgeneratecommandhandle-does-not-exist
Go to JWTGenerateCommand.php file located in vendor/tymon/src/Commands and paste this method
public function handle() { $this->fire(); }
It's never a great idea to change anything in the vendor folder but the there's two ways to deal with this ...
Generate a random string yourself and just change the value in the JWT config file.
Go to Tymon\JWTAuth\Commands\JWTGenerateCommand and change the fire method to handle.
go to given file path
vendor/tymon/jwt-auth/src/Commands/JWTGenerateCommand.php
change function name
public function fire() to public function handle()
run command:
php artisan jwt:generate
I'm publishing this answer because I have crash in this error more than one time.
The only solution I found that it works with Laravel 5.6 is the following:
Add "tymon/jwt-auth": "1.0.0-rc.1" to composer.json and run composer update
Open config/app.php and add the following:
config/app.php:
'providers' => [
/*
* JWT Service Provider...
*/
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
],
'aliases' => [
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,
],
Execute:
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
Finally, execute: php artisan jwt:secret
After all that, when I hit my endpoint for login I got the following exception:
Class Tymon\JWTAuth\Providers\JWT\NamshiAdapter does not exist
This was fixed by:
Open config/jwt.php and change the following:
config/jwt.php:
'jwt' => Tymon\JWTAuth\Providers\JWT\Namshi::class,
'auth' => Tymon\JWTAuth\Providers\Auth\Illuminate::class,
'storage' => Tymon\JWTAuth\Providers\Storage\Illuminate::class,
Finally, note that in order to work your User model should be defined as follows:
class User extends Authenticatable implements JWTSubject
{
...
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
...
}
I can advise one solution. Go to JWTGenerateCommand.php file located in vendor/tymon/src/Commands and paste this part of code public function handle() { $this->fire(); }
I know this is not an elegant solution, but it works. I hope this might help until official fix arrive.
see here for more info
Change fire() function to handle() in this path
vendor/tymon/jwt-auth/src/commands/JWTGenerateCommand.php
In the file path: /vendor/tymon/jwt-auth/src/Commands/JWTGenerateCommand.php
Add public function
public function handle()
{
$this->fire();
}

Laravel 5.1 throws ReflectionException after upgrade from 5.0

I have recently upgraded Laravel to version 5.1 from 5.0, but whenever I send an API response to the server, it throws an exception like the following:
ReflectionException in RouteDependencyResolverTrait.php line 57: Class App\Http\Requests\User\GetRequest does not exist
Before upgrading everything was working fine.
This is the autoload section of my composer.json file:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Bloom\\" : "library/Bloom"
}
}
Could it be that I did something went wrong during the upgrade procedure?
This is how the GetRequest class is declared:
<?php namespace App\Http\Requests\User;
use App\User;
use App\Http\Requests\Request;
use Illuminate\Support\Facades\Auth;
/**
* Class GetRequest
* #package App\Http\Requests\User
*/
class GetRequest extends Request {
Update: I just reinstalled Laravel 5.1 and moved all the old files to the new installation but I still receive the same error.
Update 2: After running php composer dump-autoload -o I can notice that the file vendor/composer/autoload_classmap.php contains the class that Laravel is unable to find:
'App\\Http\\Requests\\User\\GetRequest' => $baseDir . '/app/Http/Requests/User/GetRequest.php',
Update 3: I tried also to create a new request using
php artisan make:request TestRequest
and then using the TestRequest in the UserController.php
/**
* #param GetRequest $request
* #return \Symfony\Component\HttpFoundation\Response
*/
public function get(TestRequest $request)
{
if(!$this->userRepository->getUser()) return $this->respondOK('User not found.');
$user = $this->userRepository->getUser();
return $this->respondOK('', (new UserTransformer())->transform($user->toArray()));
}
but I still receive the same error.
OK, I found the solution.
I noticed that if in GetRequest class I swap use App\Http\Requests\Request; with use Request; then the GetRequest class is found and the ReflectionException is not thrown anymore.
So the problem is not in the App\Http\Requests\User\GetRequest class but in the abstract class App\Http\Requests\Request.
After looking at the App\Http\Requests\Request class, to which I made changes, I decided to replace it with the dist version from Laravel and then I reapplied my changes.
Long story short: The problem was in App\Http\Requests\Request because I made changes to it and something changed with the update to Laravel 5.1.
I've got the same problem. Currently I found the solution as specify "use" in the controller that used new request class.
Set "use" in controller for right path to the request files:
use App\Http\Requests\Request;
use App\Http\Requests\YOUR_NEW_REQUEST_CLASSRequest;

Resources