I'm working on a Laravel project and I want to use Redis to store session data for improved performance. I have set up Redis correctly and it's working fine with basic functionality. However, I'm having trouble getting the auth::attempt function to work with Redis.
Here's my current auth::attempt code:
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// User authentication successful
} else {
// User authentication failed
}
This works fine with the default session driver in Laravel, but I want to use Redis for better performance. I have tried changing the SESSION_DRIVER variable in my .env file to 'redis' and set the Redis connection in config/database.php, but I'm still getting an authentication error.
I'm not sure what I'm missing. Can anyone help me understand how to use Redis with auth::attempt in Laravel? Thanks in advance!
When using Redis for session storage in Laravel, you need to ensure that your Redis server is running and that your Laravel application is configured to use Redis as the session driver.
To set Redis as your session driver, you can change the value of the SESSION_DRIVER variable in your .env file to redis. You can also configure the Redis connection in the config/database.php file. Here's an example configuration:
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'table' => 'sessions',
'expire_on_close' => false,
],
Once you've configured Redis as your session driver, you can use the Auth::attempt method as usual. However, if you're still encountering authentication errors, there are a few things you can try:
Make sure your Redis server is running and accessible from your
Laravel application.
Check your Redis configuration in the config/database.php file to
ensure that it's set up correctly.
Clear your Laravel application's cache using the php artisan
cache:clear command.
Check your Redis server's logs for any errors or issues that might
be causing the authentication failures.
Related
I am trying to implement Redis publishing in my local RESTful API which is built in Laravel for the purposes of implementing a chat system later on with Web Sockets. I intend to read them from a Node.JS server later on.
I am using Redis::publish to publish a simple message to my test-channel.
However, for some reason Laravel doesn't seem to publish to it.
I have also noticed that when I call Redis::set, whatever I set doesn't get persisted in Redis, but using Redis::get I can read the values that I'm setting.
public function redis(Request $request) {
$data = $request->validate([
'message' => 'string|required'
]);
Redis::publish('test-channel', 'a test message');
return 'Done';
}
I am using the code above in the api/redis route:
Route::post('/redis', 'API\MessageController#redis');
I have subscribed to the test-channel using the redis-cli command.
If I manually publish a message to the test-channel using the redis-cli in a terminal instance, I properly receive the messages that I am publishing. However, they don't seem to get published with Laravel for some reason.
What I can notice while running php artisan serve and visiting the aforementioned route is Laravel logging the following:
[*timestamp*] 127.0.0.1:39448 Accepted
[*timestamp*] 127.0.0.1:39448 Closing
The port after 127.0.0.1 appears to be random.
I tried both php-redis php extension and the predis package, just to be sure that it isn't any one of them, but I get the same result with both of them. I am currently using php-redis with both igbinary and redis extensions enabled in /etc/php/config.d and have removed the Redis alias from config/app.php.
I am using PHP 7.4, Laravel 6.0 and Redis 5.0.7 on Manjaro.
Been there, discovered that with:
$ redis-client
psubscribe *
will show you what's going on.
Chances are that your default config/database.php contains something like:
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],
In that case, the channel name will be prefixed with this prefix option.
So you can just comment this option, or, if you keep it, be sure to subscribe to the right channel
Redis::publish('test-channel', 'a test message');
$prefix = config('database.redis.options.prefix');
$channel = $prefix . 'test-channel';
return "Done. (published on $channel)";
I have 2 laravel nodes running in separate servers under a load balancer, and a dedicated redis server for session and cache storage.
I configured the session and cache drivers accordingly to "redis" and it connects just fine. I see files being stored inside the redis server.
The issue is when I try to login, the page just gets refreshed without printing the "Invalid credential" errors that are normally stored in session.
Since the load balancer keeps redirecting from one node to another, the session is somehow getting lost. As a single instance it works just fine though. Is there anyone having the same issue with laravel and load balancing?
If there is a possible fix without configuring the balancer to use sticky sessions, that would be great!
Thanks in advance!
I think this package TrustedProxy solves your issue. Install it and then just add it to config/trustedproxy.php:
return [
'proxies' => [
'192.168.10.10',
],
// These are defaults already set in the config:
'headers' => [
(defined('Illuminate\Http\Request::HEADER_FORWARDED') ? Illuminate\Http\Request::HEADER_FORWARDED : 'forwarded') => 'FORWARDED',
\Illuminate\Http\Request::HEADER_CLIENT_IP => 'X_FORWARDED_FOR',
\Illuminate\Http\Request::HEADER_CLIENT_HOST => 'X_FORWARDED_HOST',
\Illuminate\Http\Request::HEADER_CLIENT_PROTO => 'X_FORWARDED_PROTO',
\Illuminate\Http\Request::HEADER_CLIENT_PORT => 'X_FORWARDED_PORT',
]
];
I am trying to configure my Laravel app so all my Laravel Queue Failed Jobs goes to Redis instead of mysql.
Currenly my jobs are configured to use redis but failed_jobs still goes to MySql Database
Didn't find anything on StackOverflow/Laravel
Laravel 5.4
Redis
PHP 7.0
Please help!
Check your settings in config/queue.php, you should find following values:
'failed' => [
'database' => env('DB_CONNECTION', 'mysql'),
'table' => env('QUEUE_FAILED_TABLE', 'failed_jobs'),
]
Change the connection and table on whatever you need.
i am working on Redis to store data Everything is working fine in my local system. i have successfully installed redis also in laravel with this command composer require predis/predis also and Redis setup of window also installed. Now when i store data in Redis like this:-
Redis::set('first',"My first Test"); // put data in Redis key
echo Redis ::get('first'); // get data
Above code is working fine in my local system. when i try to use this code in live server it is showing the below error:-
Please help me to resolve this issue. We are using amazon-ec2 server Thanks in advance :)
I had the same issue. But I believe its related to php 7 rather than Larevel 5.4 because I'm using Laravel 5.1 and I still have the problem.
I came across 2 solutions
Use use Illuminate\Support\Facades\Redis; instead of use Redis; if you want to call the Redis methods statically.
Change to dynamic calling
$redis = new Redis();
$redis->set('boo','Have beer and relax!')
$redis->get('boo');
Just remove or comment out extension=php_redis.dll from your php.ini
Laravel and server Redis conflicts with name "Redis"
This will work
In Laravel database config you can define a client for your Redis handler.
As you have installed predis, your Redis database configuration should look like this
'redis' => [
'client' => 'predis',
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
PhpRedis extension and Laravel alias for Redis Facades are same which is creating the issue. In case you want to use the PhpRedis extension you need to change the alias keyword defined in app.php and client in the database config.
I encounter issues with the database driver in order to store sessions cookies:
The database connection is currently working but cookies are not set in the browser however session data are in my database.
I have run:
php artisan session:table
Set database session in my .env file:
SESSION_DRIVER=database
Bellow the config/session.php file:
[
'driver' => env('SESSION_DRIVER', 'file'),
'connection' => 'mysql',
'table' => 'sessions',
]
EDIT 1:
The route in web.php:
Route::group(['domain' => GetDomainInfo::GetDomainName(), 'middleware' => 'web'], function () {
Route::get('/', "HomeController#index");
});
Solved issue by :
I have created for example a localhost domain: webcv by adding to my hosts file:
127.0.0.1 webcv.local
127.0.0.1 admin.webcv.local
Set the session domain to : .webcv.local in config/session.php like so:
'domain' => env('SESSION_DOMAIN', ".webcv.local"),
Or just by adding in the .env file:
SESSION_DOMAIN=.webcv.local
Now I can access to both webcv.local and admin.webcv.local with the same cookie.
When you work with the database driver for sessions, you have to create the session table.
I'll assume you already scaffold the native authentication system by doing php artisan make:auth and migrated the migrations.
So first you generate the migration, and then migrate:
php artisan session:table
Execute the migration:
php artisan migrate
Obviously, this won't work if the routes you're checking on, aren't using the web middleware.
If you're not registering the routes in /routes/web.php, then you have to manually specify the web middleware for them. Either in your controller's constructor, or directly in the route itself.
Edit after fix for future reference:
Also, check your domain in config/session.php is the same you're using in development or production, depending where you are working on.