Support the ongoing development of Laravel.io →
posted 3 years ago
Last updated 2 years ago.
0

I found a solution using the Carbon class (also make sure that the time on the server is correct, mine was still a different timezone)

on the Laravel PHP side I read the last_activity as

`public function sessions(Request $request) { $jsonReposeData = [];

  foreach ($request->user()->getSessions()->get() as $currentSessionObject) {

     // If the session age has not reached the SESSION_LIFETIME value
     if ($currentSessionObject->LastActivity() > Carbon::now()->subMinutes(env('SESSION_LIFETIME'))->getTimestamp()) {
        $currentSession = [
           "row_id" => $currentSessionObject->RowID(),
           "user" => $request->user()->getUser()->get()->first()->getFullName(),
           "user_agent" => $currentSessionObject->UserAgent(),
           "ip_address" => $currentSessionObject->IPAddress(),
           "last_activity" => Carbon::parse($currentSessionObject->LastActivity())
           //"last_activity" => $currentSessionObject->LastActivity()
           //
        ];


        array_push($jsonReposeData, $currentSession);
     }
  }

  return response()
     ->json([
        'success' => true,
        'sessions' => $jsonReposeData,
        'responseMessage' => 'Your sessions has been retreived'
     ]);

}`

On the javascript front end side, the data is read by (new Date) <template> <v-simple-table :loading="loading"> <template v-slot:default> <thead> <tr> <th class="text-left"> User </th> <th class="text-left"> IP Address </th> <th class="text-left"> User Agent </th> <th class="text-left"> Last Activity </th> </tr> </thead> <tbody> <tr v-for="item in sessions" :key="item.row_id"> <td>{{ item.user }}</td> <td>{{ item.ip_address }}</td> <td>{{ item.user_agent }}</td> <td> {{ new Date(item.last_activity) }} </td> </tr> </tbody> </template> </v-simple-table> </template>

sheenilim08 liked this reply

1

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.