Skip to content

Commit

Permalink
Added user registered activity and IP tracking for the users
Browse files Browse the repository at this point in the history
  • Loading branch information
Raccoon254 committed Jun 24, 2024
1 parent da2ee94 commit 291f611
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 3 deletions.
21 changes: 19 additions & 2 deletions app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification;
use Illuminate\Validation\Rules;
use Illuminate\Validation\ValidationException;
use Illuminate\View\View;
use Jenssegers\Agent\Agent;

class RegisteredUserController extends Controller
{

/**
* Display the registration view.
*/
public function create(): View
{
return view('auth.register');
}

/**
* Handle an incoming registration request.
*
* @throws \Illuminate\Validation\ValidationException
* @throws ValidationException
*/
public function store(Request $request): RedirectResponse
{
Expand Down Expand Up @@ -58,6 +60,21 @@ public function store(Request $request): RedirectResponse

event(new Registered($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' => 'registered',
'description' => 'User registered',
'device' => $device,
'browser' => $browser . ' ' . $browserVersion,
'ip' => $request->ip(),
'user_agent' => $request->userAgent(),
]);

Auth::login($user);

// Send notification to the new user
Expand Down
6 changes: 6 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Storage;
Expand Down Expand Up @@ -62,6 +63,11 @@ public function referrals(): HasMany
return $this->hasMany(Referral::class, 'referrer_id');
}

public function activity(): HasMany
{
return $this->hasMany(UserActivity::class);
}

public function getAvatarAttribute(): string
{
return 'https://api.dicebear.com/8.x/identicon/svg?seed=' . $this->name;
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"require": {
"php": "^8.2",
"jenssegers/agent": "^2.6",
"laravel/framework": "^11.0",
"laravel/horizon": "^5.24",
"laravel/tinker": "^2.9",
Expand Down
199 changes: 198 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 291f611

Please sign in to comment.