JWTAuth::user() returns null - laravel

I am using Laravel 8 and JWTAuth package for the api requests, often the request throws error when trying to access user info with the following line:
JWTAuth::user()->id
The error: 500 Internal Server Error
ErrorException: Trying to get property 'id' of non-object ...
The only way I could resolve this, temporary however, is to refresh the token. I am wondering that if token is expired it should say token is expired, but seems something else is happening here, which I could not figure it out.
Appreciate any help!

For anyone else facing this issue, working with tokens can quickly create caching issues which can cause headaches if not considered. So in my case resetting the JWT secrete keys fixed the issue.
php artisan jwt:secret
Also for clearing most of the cached sections:
php artisan config:clear && php artisan cache:clear && composer dump-autoload && php artisan view:clear && php artisan route:clear && npm cache clean --force

Related

Mixed Content, This request has been blocked; the content must be served over HTTPS , Laravel

I have this error one production env. only on some routes, I see no difference between the routes that works and the few of them that don't work. All my routes are in web.php, and in front I try to access via Vuejs/Axios.
Also, the both url from error when I try to access them I get to https://, even if I try http:// , I get redirect to https.
What I tried till now.
.env
APP_URL=https://my.url
web.php
URL::forceScheme('https'); //at the top of the file
App/Providers/AppServiceProvider
public function boot()
{
URL::forceScheme('https');
}
Also:
composer dump-autoload
php artisan cache:clear
php artisan config:clear
php artisan view:clear
php artisan route:clear
php artisan config:cache
npm run prod
I found a strange solution.
I changed the route name from random_question to random, also the controller method name that was show to random and works.

Laravel ER diagram generator getAllModelsFromEachDirectory()

When I try to follow the instruction here (https://github.com/beyondcode/laravel-er-diagram-generator) I get the following error.
Symfony\Component\Debug\Exception\FatalThrowableError : Argument 1 passed to BeyondCode\ErdGenerator\GenerateDiagramCommand::getAllModelsFromEachDirectory() must be of the type array, null given, called in vendor\beyondcode\laravel-er-diagram-generator\src\GenerateDiagramCommand.php on line 96
at vendor\beyondcode\laravel-er-diagram-generator\src\GenerateDiagramCommand.php:101
97|
98| return $modelsFromDirectories;
99| }
100|
101| protected function getAllModelsFromEachDirectory(array $directories): Collection
102| {
103| return collect($directories)
104| ->map(function ($directory) {
105| return $this->modelFinder->getModelsInDirectory($directory)->all();
Exception trace:
1 BeyondCode\ErdGenerator\GenerateDiagramCommand::getAllModelsFromEachDirectory()
vendor\beyondcode\laravel-er-diagram-generator\src\GenerateDiagramCommand.php:96
2 BeyondCode\ErdGenerator\GenerateDiagramCommand::getModelsThatShouldBeInspected()
vendor\beyondcode\laravel-er-diagram-generator\src\GenerateDiagramCommand.php:57
Please use the argument -v to see more details.
I've already opened an issue in the repository.
Screen capture of the error:
Posting for others that may encounter same issue.
Confirm you are using the latest version 1.4.0 of the library
Also if you're using php artisan serve try to stop the server. Run php artisan config:cache, and restart the artisan sever.
Note that the reason why you might need to run php artisan config:cache is because Laravel does cache the app's configurations. if you changed or added new configurations you might explicitly need to clear the configuration cache so that Laravel can cache the new configurations.
If no solution yet. Try this
php artisan vendor:publish --provider=BeyondCode\\ErdGenerator\\ErdGeneratorServiceProvider.
Then repeat step 2.

Laravel 5.6 env('APP_BASE_URL') returns null

In staging and production environment, when I try to get my custom defined variable from .env file, it returns null. I have tried creating new Key for APP and cleared all sort of caches, but result is same.
APP_NAME="App - Staging"
APP_ENV=staging
APP_KEY=<HIDDEN_KEY>
APP_DEBUG=true
APP_LOG_LEVEL=none
APP_URL=https://staging.app.com
APP_BASE_URL="https://app-staging.app.com"
Clearing Cache
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan config:cache
But when using following in blade view gets nothing
env('APP_BASE_URL') returns null
It is because you have run php artisan config:cache. If you are using config:cache, your env() calls should only be made in your config files.
See here: https://laravel.com/docs/5.6/configuration#configuration-caching
This post solved my issue, I defined custom config in config/app.php
https://laracasts.com/discuss/channels/laravel/accessing-custom-environment-variable
/*
|-------------------------------------------------------------------------
| Custom config variables
|-----------
|
*/
'base_url' => env('APP_BASE_URL', 'http://localhost'),
Then in .env I definded:
APP_BASE_URL="https://app-staging.app.com"
Finally cleared the cache, worked.
In blade view
{{ config('app.base_url') }}
Yes it will return only null
If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.
See the laravel documentation https://laravel.com/docs/5.6/configuration#configuration-caching

Getting messages feature in Debugbar to work in Laravel

I've installed Debugbar for Laravel as described in the steps on the website https://laravel-news.com/laravel-debugbar; and tried to make use of the Messages feature by placing the following below in my code.
Debugbar::info($object);
Debugbar::error('Error!');
Debugbar::warning('Watch out…');
Debugbar::addMessage('Another message', 'mylabel');
But when I run my website, I get the error message from Laravel saying:
1/1
FatalErrorException in HistoryController.php line 11:
Class 'App\Http\Controllers\Debugbar' not found
I have to go like /Debugbar::info(...) or put use Debugbar at the top of my code to not get the error message. Why can't I use it straight like Debugbar::info(...)?
To be able to reference the facade without prefixing it with \ you should add
use Barryvdh\Debugbar;
to the top of your controller.
after adding setting code in /config/app.php
you can use it as a facade
app('debugbar')->info('info message');
or
debugbar()->info('message');
no need use
I think you should try this :
first you add below code in config/app.php
in provider section
'Barryvdh\Debugbar\ServiceProvider',
in aliases section
'Debugbar' => 'Barryvdh\Debugbar\Facade',
after you should clear the cache like:
php artisan config:cache
php artisan cache:clear
php artisan config:clear
Hope this work for you !
Firstly, Go to the terminal and install by typing:-
composer require barryvdh/laravel-debugbar
In second step, Check your laravel verision:-
php artisan --version
In third if your laravel version is greater than 5(Laravel 5.x)
add the ServiceProvider to the providers array in config/app.php
Barryvdh\Debugbar\ServiceProvider::class,
add this to your facades in app.php:
'Debugbar' => Barryvdh\Debugbar\Facade::class,
Finally, published vendor configuration by command:-
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
After vendor published clear cache,route,view by command
php artisan cache:clear
php artisan route:clear
php artisan config:clear
php artisan dump-autoload -o

Laravel 4 - unserialize(): Error at offset 0 of 32 bytes

When I install Laravel 4, I receive this error:
ErrorException
unserialize(): Error at offset 0 of 32 bytes
C:\xampp\htdocs\blog\laravel\bootstrap\compiled.php
return unserialize($this->stripPadding($this->mcryptDecrypt($value, $iv)));
If I modify return like this:
return unserialize(base64_decode($this->stripPadding($this->mcryptDecrypt($value, $iv))));
Then the error goes away. But of course every time I run composer update this change will be undone.
What could be the reason of this problem?
I appreciate any help.
Update: This only happens when I use Auth::check().
Update 2: Now it only works with base64_decode() removed. It's like if the xampp installation has become self-aware. Jesus!
Refer to this issue: laravel/framework#1526
A change in the encryption mechanism is the cause. My solution was to empty out the sessions and views that were cached in the storage folder, then run php artisan key:generate and relaunch the server. I'm not sure which part of the process fixed the issue, but I haven't seen it since.
The command > php artisan cache:clear fixed the problem for me.
I did not have to restart the server
Can you post what you're doing that causes this error? You shouldn't be modifying the core - because as you said, updates will overwrite it.
You have to set a news Key, use the following command:
php artisan key:generate
After that test again to run the Laravel Application
php artisan serve

Resources