trypost/app/Models/User.php
Paulo Castellano 2e9e0f716b feat: capture signup UTMs/IP and add GitHub OAuth login
Persists marketing attribution and registration metadata for new users
across the three signup paths (email, Google, GitHub):

- 5 utm_* columns + registration_ip on the users table
- PreservesUtmParameters trait stores incoming utm_* query params on
  the register/redirect GET, retrieves them on the POST/callback —
  surviving the OAuth round-trip via session
- request()->ip() captured at the controller layer

Adds GitHub as a second OAuth provider:

- GitHubController mirroring the Google one (now renamed from
  SocialLoginController for symmetry)
- Settings → Authentication can connect/disconnect GitHub like Google
- Single SocialLogin.vue component replaces the per-provider buttons
  on Login/Register, rendering each enabled provider plus a single
  "or continue with" divider

UserFactory gains defaults for the new nullable columns so model
strict-mode access in tests doesn't trip.
2026-05-04 18:42:25 -03:00

117 lines
3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\Notification\Type as NotificationType;
use App\Models\Traits\HasMedia;
use App\Models\Traits\HasWorkspace;
use Database\Factories\UserFactory;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\Contracts\OAuthenticatable;
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable implements MustVerifyEmail, OAuthenticatable
{
/** @use HasFactory<UserFactory> */
use HasApiTokens, HasFactory, HasMedia, HasUuids, HasWorkspace, Notifiable;
/**
* @var list<string>
*/
protected $fillable = [
'name',
'email',
'password',
'google_id',
'github_id',
'account_id',
'current_workspace_id',
'email_verified_at',
'utm_source',
'utm_medium',
'utm_campaign',
'utm_term',
'utm_content',
'registration_ip',
];
/**
* @var list<string>
*/
protected $hidden = [
'password',
'two_factor_secret',
'two_factor_recovery_codes',
'remember_token',
];
protected $appends = [
'has_photo',
'photo_url',
];
public function getHasPhotoAttribute(): bool
{
return $this->getFirstMedia('avatar') !== null;
}
public function getPhotoUrlAttribute(): ?string
{
return $this->getFirstMediaUrl('avatar');
}
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'two_factor_confirmed_at' => 'datetime',
];
}
public function notifications(): HasMany
{
return $this->hasMany(Notification::class);
}
public function notificationPreference(): HasOne
{
return $this->hasOne(NotificationPreference::class);
}
public function account(): BelongsTo
{
return $this->belongsTo(Account::class);
}
public function isAccountOwner(): bool
{
return $this->id === $this->account?->owner_id;
}
public function wantsEmailFor(NotificationType $type): bool
{
$preference = $this->notificationPreference;
if (! $preference) {
return true;
}
return match ($type) {
NotificationType::PostPublished => $preference->post_published,
NotificationType::PostFailed, NotificationType::PostPartiallyPublished => $preference->post_failed,
NotificationType::AccountDisconnected => $preference->account_disconnected,
NotificationType::MentionedInComment => $preference->mentioned_in_comment ?? true,
default => true,
};
}
}