Laravel - 'Auth guard [admin] is not defined.' - laravel

This is a fresh copy of Laravel 5.6.
using spatie laravel permissions.
I have maybe altered something while dealing with permissions that could have altered the guard. I have no idea what since I can't see any git changes, maybe via the DB?
At first I tried to tinker and give a specific user admin privileges with:
$role = Spatie\Permission\Models\Role::where('name', 'admin')->get()->first();
When that returned undefined, I checked Auth::guard('admin'), that returns undefined, so I understand that the issue has nothing to do with Spatie package.
After looking in a few questions/answers here on stackoverflow, one of them being config:clear and config:cache, which didn't work and still getting undefined for Auth guard admin.
Any other options I could take to get Auth admin?
thanks

In case you edited your config/auth.php, e.g. to add another guard and your config is cached, your guards may not be reloaded. If you experience this problem, clearing the config will fix it.
https://github.com/laravel/laravel/blob/v5.2.0/config/auth.php
php artisan config:clear OR php artisan config:cache
I'm using Laravel 5.4.

Add admin or administrator to the auth guards. After this php artisan config:clear can be run to remove the previous cached configurations. It should match your roles->name in the database.
\config\auth.php (file)
'guards' => [
...
'administrator' => [
'driver' => 'session',
'provider' => 'users',
],
],
An old question, but stil valid from Laravel 5 to 7

Related

I cannot disable Laravel debug logging?

I kept running the debug logging level for some time for my application also on prod, after now 2 weeks, I wanted to disable it. So I went ahead and opend the .env variable and set APP_DEBUG=false
I did a php artisan down, php artisan cache:clear, then php artisan config:clear, php artisan config:cache and brought it back up. To be fully safe here.
I went back in the log file after a minute or so and still saw production.DEBUG appearing.
I then retried all the steps, checked the config/app.php file and made sure that nothing is wrong in there. But everything looks fine.
Then I went to my local environment and tried the same but to my surprise the application still keeps logging debug messages also on local!
I cannot get it disabled!
What I tried:
php artisan config:clear / :cache / cache:clear optimize:clear / down / up ...
I checked the app.php file, the config/logging.php but all looks fine
APP_ENV=production, APP_DEBUG=false
Then to my surprise I tried something:
I changed LOG_LEVEL=info in the .env file -> no change
I opened the config/logging.php and went to the daily section (because LOG_CHANNEL=daily) and set the 'level' entry to 'info' => IT WORKS! both on PROD and LOCAL
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'info',
'days' => 21,
],
Can someone explain to me, why this is happening?
I was under the impression that the APP_DEBUG and the LOG_LEVEL should be the key indicators for Laravel but somehow they aren't?!
Might that be related to the LARAVEL DEBUGBAR? I have it installed.
Laravel version 7, php 7.4 (local Homestead, prod apache)

Laravel and Passport getting SQLSTATE[42S22]: Column not found: 1054 Unknown column 'api_token'

I've seen many posts about this error, but none of the solutions are working for me.
I'm running Laravel with Passport which is working fine on my development server. As expected, when attempting to check if a user is authenticated on my development server, it returns the catch (inside an axios call):
Development Server
"message":"Unauthenticated."
However, when running the same code on my production server, it returns the catch:
Production Server:
"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'api_token'
in 'where clause' (SQL: select * from `users` where `api_token` = ...
I have have run Passport migration and ['guards']['api']['driver'] set to passport in config/auth.php, and updated the configuration cache that apparently solved the problem for others.
It looks like authentication needs to use the oauth tables from passport migration, but the query appears to be looking at the user table.
EDIT:
I was able to determine that my development server uses the RequestGuard class to find user and my production server uses the TokenGuard class to find user.
I have faced the same problem. And I am using Laravel Passport. It all about configuration. got to config/auth.php and change driver name of api array to passport within the guards array
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',//instead of token
'provider' => 'users',
'hash' => false,
],
],
I was facing same problem and i have solve this by
php artisan config:clear
php artisan config:cache
php artisan cache:clear
may be it helpful
it also may happen when you use wrong auth middleware :
public function __construct()
{
$this->middleware('auth:api');
}
for example for using sanctum :
public function __construct()
{
$this->middleware('auth:sanctum');
}
Despite the confusing error, I followed the steps in this post and it correctly now routes the auth request through the RequestGuard and authentication is made as expected.
Not sure how cache can get messed up to cause this, but I'm guessing that my config cache was stuck on web guard and perhaps clearing it now routes correctly through the api guard.
Hope this helps others.
there must be a column named api_token in your users table ,
and this column must be filled with unique values for each record ,
you can add it to your migration like
$table->string(‘api_token’, length)->after( .. ) or create manually
api apps authanticates the users via using the api_token value , instead "id"
I know the question got answered, but this answer is explained to those who really want to know the real cause of the error in depth.
Remember, it is an error when your route in the API is a protected route and you need to specify a token as a bearer while sending a request after authentication to the protected routes.
The below route is protected, if I want to request it I will need an access token that is generated with authentication through the passport.
Route::middleware('auth:api')->group(function () {
Route::get('/loginCheck', 'Auth\ApiAuthController#LoginCheck');
});
What you need to do is when you change the "token" to "passport" are as follows:
Location of below file: config\auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',//instead of token
'provider' => 'users',
'hash' => false,
],
],
then you will need to run the following commands.
php artisan cache:clear
php artisan route:clear
php artisan config:cache
now start the laravel server again
php artisan serve
And you are done!
Try running:
php artisan config:cache
php artisan cache:clear
php artisan config:clear
I have been solved it by running the following commands in the composer window
php artisan key:generate
php artisan config:cache
php artisan cache:clear
php artisan config:clear

Laravel 5.5 Socialite Error

I have an issue with Socialite on Laravel 5.5 I have Socialite required by composer, updated services.php with different providers credential and updated providers in app.php This is the error I am getting
Type error: Argument 1 passed to Laravel\Socialite\SocialiteManager::formatRedirectUrl() must be of the type array, null given
Any help is highly appreciated.
In most of the cases, the issue is solved through 3 steps:
Run php artisan clear-compiled
Run composer dump-autoload
Run php artisan optimize
Hope it helps. Thanks.
Check config/services.php for the provider you're authenticating with. The redirect entry needs to be set to your defined route.
'redirect' => 'http://your-callback-url'
Example:
// config.services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => 'http://example.com/github/redirect',
],
// routes/web.php
Route::get('github/redirect', 'GitHubController#redirect')

Laravel keeps using hello#example.com with changed conf

In mail.php I have changed from fields but I still get mails with demo values. There is no reference to hello#example.com in project, yet I still get this in mail.
'from' => [
'address' => 'do-not-reply#mysite.net',
'name' => 'MySite.net',
],
Does anyone have an idea what might be wrong?
Clear the config cache by running the following Artisan command:
php artisan config:clear
If you are using supervisor, ensure you set the right user in the conf file, otherwise you will see the emails are being sent as hello#example.com.
In my case the user is www-data - I set it up as root before and was getting this issue.

Laravel Facade not being loaded

I started with a fresh installation of Laravel and then did the following...
I filled in the config/workbench.php file with my info: -
return array('name' => 'My Name', 'email' => 'fullysick#email.com');
I ran the artisan command: -
php artisan workbench dicksmith/curl --resources
Everything reported success. It installed dependencies etc.
I populated the app.php file with the necessary service provider and alias information. My code can be seen here -> http://paste.laravel.com/wFj
I'm struggling to think why it may not be loading through the Facade accessor.
Any ideas?
Problem solved.
I just needed to have the Facades directory located inside the package.
A stupid, stupid, stupid, stupid mistake on my behalf. 2 days lost... I feel sheepish.

Resources