Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PetroGromovo's avatar

How to increase session vue/cli 4.0.5 / vuex 3 / Laravel 6 Backend REST API app?

Hello, I make @vue/cli 4.0.5 / vuex 3 app with data reading from Laravel 6 Backend REST API app with passport as auth and I want to set bigger time of logged session during development and for this in my app/Http/Controllers/AuthController.php I changed time and session

    protected function respondWithToken($token)
    {
        $loggedUser = $this->guard()->user();

        $user_avatar_path = User::getUserAvatarPath($loggedUser->id, $loggedUser->avatar);
        $filenameData     = User::setUserAvatarProps($loggedUser->id, $loggedUser->avatar, true);

        return response()->json([
            'access_token'     => $token,
            'user'             => $loggedUser,
            'token_type'       => 'bearer',
            'user_avatar_path' => $user_avatar_path,
            'filenameData'     => $filenameData,
            'expires_in'       => $this->guard('api')->factory()->getTTL() * 660
        ]);
    }

But seems this chages does not influence my app. Did I miss some options? Also as I can see parameter expires_in starts its time from login time and that seems to me not what I want, as I want this parameter works as time from last work in the app. In other way any request to backend part I have to refresh this this parameter...

Thanks!

0 likes
2 replies
PetroGromovo's avatar

Have anybody encountered such issue? How it can be salved ?

PetroGromovo's avatar

I want to clarify what I want: I use some banks accounts with personal area. If I inactive with 15-20 minutes I lose my session and have to login again. That is what I want.

Some more details: I have in backend app:

        "php": "^7.2",
        "barryvdh/laravel-cors": "^0.11.4",
        "laravel/framework": "^6.2",
        "laravel/passport": "^8.1",

And on Vue/Cli part :

    "store": "^2.0.12",
    "vue": "^2.6.10",
    "vue-js-modal": "^1.3.31",
    "vue-resource": "^1.5.1",
    "vue-router": "^3.1.3",
    "vuex": "^3.1.2"

Actualy I want in backend app in config/app.php to set some parameter like

    'personal_access_tokens_expire_in_hours' => 24, // Actually I think about value = 1

and to user it in 2 places : in app/Providers/AuthServiceProvider.php :

    public function boot()
    {
        $this->registerPolicies();
        Passport::routes();
        $personal_access_tokens_expire_in_hours   = config('app.personal_access_tokens_expire_in_hours',24);
        Passport::personalAccessTokensExpireIn(Carbon::now()->addHours($personal_access_tokens_expire_in_hours));
    }

and in app/Http/Controllers/AuthController.php :

    public function login(Request $request)
    {
        $credentials = request(['email', 'password']);
        $request->validate([
            'email'       => 'required|string|email',
            'password'    => 'required|string',
            'remember_me' => 'boolean'
        ]);

        if ( ! Auth::attempt($credentials)) {
            return response()->json(['message' => 'Unauthorized'], 401);
        }

        $user = $request->user();
        $user->last_logged= Carbon::now(config('app.timezone'));
        $user->save();
        $tokenResult = $user->createToken('Access Token');
        $token       = $tokenResult->token;

        if ($request->remember_me) {
            $personal_access_tokens_expire_in_hours   = config('app.personal_access_tokens_expire_in_hours',24);
            $token->expires_at = Carbon::now()->addHours($personal_access_tokens_expire_in_hours);
        } // Though Ronak Dhoot wrote that $token->expires_at does not infleunce anything.

I added last_logged field to users and fill it on any login.

With default personalAccessTokensExpireIn value in 1 day I login in the system in the middle of my working day. I turn off computer in the end of the day and opening it next morning I can enter my app with login I made yesterday(24 hours has not passed yet). That seems not safe for me.

I would prefer personalAccessTokensExpireIn = 1 hour and refresh it ANY authorized request from my vue/cli app. In which way that could be done? Working on vue/cli apps with baxkend api which way do you use?

I have some prior work with auth/jwt and in app/Http/Controllers/API/AuthController.php I found methods :

    public function refresh() // THIS METHOD IS NOT CALLED ANYWHERE
    {
        return $this->respondWithToken($this->guard()->refresh());
    }

    protected function respondWithToken($token)
    {
        $loggedUser= $this->guard()->user();

        $user_avatar_path= User::getUserAvatarPath($loggedUser->id, $loggedUser->avatar);
        $filenameData                     = User::setUserAvatarProps($loggedUser->id, $loggedUser->avatar, true);

        $usersGroups=  User::getUsersGroupsByUserId($loggedUser->id, false);
        return response()->json([
            'access_token' => $token,
            'user'         => $loggedUser,
            'token_type'   => 'bearer',
            'user_avatar_path'   => $user_avatar_path,
            'filenameData' => $filenameData,
            'usersGroups'  => $usersGroups,
            'expires_in'   => $this->guard('api')->factory()->getTTL() * 9360 // TOFIX
        ]);
    }

Can refresh() be used in my passport issue somehow?

Please or to participate in this conversation.