Skip to content

Commit

Permalink
Login and logout activities create and record to database
Browse files Browse the repository at this point in the history
  • Loading branch information
Raccoon254 committed Jun 24, 2024
1 parent 291f611 commit 95d5701
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions app/Http/Controllers/Auth/AuthenticatedSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Jenssegers\Agent\Agent;

class AuthenticatedSessionController extends Controller
{
Expand All @@ -28,6 +29,24 @@ public function store(LoginRequest $request): RedirectResponse

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

$user = Auth::user();

// Get device and browser information using Agent
$agent = new Agent();
$device = $agent->device();
$browser = $agent->browser();
$browserVersion = $agent->version($browser);

$user->activity()->create([
'activity' => 'login',
'description' => 'User ' . $user->name . ' logged in',
'device' => $device,
'browser' => $browser . ' ' . $browserVersion,
'ip' => $request->ip(),
'user_agent' => $request->userAgent(),
]);


return redirect()->intended(route('dashboard', absolute: false));
}

Expand All @@ -36,6 +55,21 @@ public function store(LoginRequest $request): RedirectResponse
*/
public function destroy(Request $request): RedirectResponse
{
$user = Auth::user();
$agent = new Agent();
$device = $agent->device();
$browser = $agent->browser();
$browserVersion = $agent->version($browser);

$user->activity()->create([
'activity' => 'logout',
'description' => 'User ' . $user->name . ' logged out',
'device' => $device,
'browser' => $browser . ' ' . $browserVersion,
'ip' => $request->ip(),
'user_agent' => $request->userAgent(),
]);

Auth::guard('web')->logout();

$request->session()->invalidate();
Expand Down
2 changes: 1 addition & 1 deletion resources/views/livewire/user-show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<p class="text-gray-500 text-xs">Joined {{ $user->created_at->diffForHumans() }}</p>
</div>
<div>
<p class="text-gray-500 text-xs">Last seen ... </p>
<p class="text-gray-500 text-xs">Last seen ...</p>
</div>
</div>

Expand Down

0 comments on commit 95d5701

Please sign in to comment.