最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Laravel 11 Session not kept after redirect - Stack Overflow

programmeradmin0浏览0评论

I am creating a basic login feature. I use Auth::attempt() to login and redirect to dashboard after successful login. However, there is a problem that even though the login is successful, after redirecting, the login state is no longer maintained.

I did not put Auth::logout() anywhere. I made a small change in auth.php config.

It seems like Session doesn't retain my data. Can you help me?

If you need any more information I will provide it.

Here is my code

  1. auth.php
'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'administrators',
        ],
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ]
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication guards have a user provider, which defines how the
    | users are actually retrieved out of your database or other storage
    | system used by the application. Typically, Eloquent is utilized.
    |
    | If you have multiple user tables or models you may configure multiple
    | providers to represent the model / table. These providers may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => env('AUTH_MODEL', User::class),
        ],
        'administrators' => [
            'driver' => 'eloquent',
            'model' => env('AUTH_ADMIN_MODEL', Administrator::class),
        ],
    ],
  1. web.php
<?php

use App\Http\Controllers\Admin\AuthenticationController;
use Illuminate\Support\Facades\Route;

Route::domain(env('ADMIN_DOMAIN'))->group(function () {
    Route::prefix('auth')->name('auth.')->group(function () {
        Route::get('/login', [AuthenticationController::class, 'index'])->name('login');
        Route::post('/login', [AuthenticationController::class, 'authenticate'])->name('authenticate');
    })->middleware('guest');

    Route::middleware('auth')->group(function () {
        Route::name('dashboard.')->group(function () {
            Route::get('/', function () {
                return 1;
            })->name('index');
        });
    });
});
  1. Controller
public function authenticate(LoginRequest $request): RedirectResponse
    {
        $credential = [
            'account_id' => $request->validated('account_id'),
            'password' => $request->validated('password'),
        ];
        if(!Auth::attempt($credential, $request->validated('remember'))) {
            return $this->response(
                route('dashboard.index'),
                __('content.auth.login.failed'),
                HttpResponseCode::UNAUTHORIZED
            );
        }

        $request->session()->regenerate();

        return $this->redirectIntended(route('dashboard.index'), __('content.auth.login.success'));
    }
  1. env
APP_KEY=base64:NQ+cqFjdJ3xlo/H6bFhE72kDxVQbdthntNi1iaoFZ/k=
APP_NAME="Laravel"
APP_ENV=local
APP_DEBUG=true
APP_TIMEZONE="Asia/Ho_Chi_Minh"
APP_URL=http://localhost:8000

APP_LOCALE=vi
APP_FALLBACK_LOCALE=vi
APP_FAKER_LOCALE=en_US

APP_MAINTENANCE_DRIVER=file
APP_MAINTENANCE_STORE=database

BCRYPT_ROUNDS=12

LOG_CHANNEL=daily
LOG_STACK=single
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=root

SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null

BROADCAST_CONNECTION=log
FILESYSTEM_DISK=local
QUEUE_CONNECTION=database

CACHE_STORE=database
CACHE_PREFIX=

MEMCACHED_HOST=127.0.0.1

REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp-relay.brevo
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=""
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

VITE_APP_NAME="${APP_NAME}"

ADMIN_DOMAIN=""
API_DOMAIN=""
发布评论

评论列表(0)

  1. 暂无评论