Laravel package session variable not saved when ajax call

I am creating a package called under-construction. When this package is activated in the config file will be incomplete, only people with the correct code can access the expression.

https://github.com/larsjanssen6/underconstruction

BQnXaMh.png

The problem I have right now is:

When I enter the code, I make an ajax call that gets into this controller method (called check ):

https://github.com/larsjanssen6/underconstruction/blob/master/src/Controllers/CodeController.php

If the code is correct, the session variable is set:

session(['can_visit' => true]);

Then in my code vue.js I redirected to /. And it will hit my middleware again. Here I check if a session with a name exists can_visit.

return session()->has('can_visit');

https://github.com/larsjanssen6/underconstruction/blob/master/src/UnderConstruction.php

But the session variable can_visithas always gone! How is this possible?

Thank you for your time.

+4
source share
1 answer

You do not download session middleware, so the session does not start and values ​​are not saved.

, (/) ( ), (/under/construction, /under/check) ( ).

, .

$routeConfig = [
    'namespace' => 'LarsJanssen\UnderConstruction\Controllers',
    'prefix' => 'under',
    'middleware' => [
        'web', // add this
        // DebugbarEnabled::class, // leaving this dead code behind despite vcs
    ],
];

, . - , , underconstruction.

public function handle($request, Closure $next)
{
    // check this isn't one of our routes
    // too bad router hasn't loaded named routes at this stage in pipeline yet :(
    // let hope it doesn't conflict with user routes
    if ($request->is('under/*')) {
        return $next($request);
    }

    if (! $this->config['enabled']) {
        return $next($request);
    }

    if (!$this->hasAccess($request)) {
        return new RedirectResponse('/under/construction');
    }

    return $next($request);
}

, , . , , , , . . !

+2

Source: https://habr.com/ru/post/1686431/


All Articles