douji3426 2017-06-07 11:00
浏览 63
已采纳

Laravel API过期了

In Laravel 5.4 I created some API on my routes/web.php. These API are called by .NET application. The first called API is the login, after that, I put in session some datas. Before to call other API, I check if the datas are in session, but after the first call (login), when I try to call the second one, the session seems to be flushed/expired.

Here my route code:

// API
Route::group(["prefix" => "api"], function() {
    // Login
    Route::post("login", "APIController@login");

    Route::group(["middleware" => "isNotAuthenticatedAPI"], function() {
        Route::group(["middleware" => "loginDBUser"], function() {
            // Importazioni
            Route::group(["prefix" => "importazioni"], function() {

                // Utenti
                Route::post("utenti", "APIController@importCustomers");
                // Attività (Abbonamenti)
                Route::post("attivita", "APIController@importSubscriptions");
                // Iscrizioni
                Route::post("iscrizioni", "APIController@importCustomersSubscriptions");
            });

            // Richieste
            Route::group(["prefix" => "richieste"], function() {
                // Ultima iscrizione
                Route::post("ultima_iscrizione/{subscription_external_id?}", "APIController@getLastCustomerSubscription");
            });
        });
    });
});

Middleware isNotAuthenticatedAPI:

public function handle($request, Closure $next) {
        $res = [
            "result" => null,
            "errors" => [
            ]
        ];

        if (!$request->session()->get("user.api")) {
            $res["result"] = false;
            $res["errors"][] = [
                "code" => APIController::WARNING_SESSION_EXPIRED,
                "message" => trans("api.w_session_expired")
            ];
            return response()->json($res);
        }

        return $next($request);
    }

My config/session.php:

'lifetime' => 120,

    'expire_on_close' => false,

I tried to put the route's code into routes/api.php, but the result doesn't change.

  • 写回答

2条回答 默认 最新

  • dounai6626 2017-06-07 12:42
    关注

    If you want to continue using Session in API, please make sure that the client (your .NET application) store the cookie and include it in later requests.

    A simple way to test if your problem is the cookie, try login your api in browser and call other api after that. If everything work well, then it's the cookie problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?