diff --git a/config/config.php b/config/config.php index 091f61473..129acabd7 100644 --- a/config/config.php +++ b/config/config.php @@ -101,6 +101,7 @@ // Name of the login and password attributes of the User Model. 'login_attribute' => 'email', 'password_attribute' => 'password', + 'avatar_attribute' => 'avatar', 'rate_limiting' => [ 'enabled' => true, diff --git a/demo/app/Models/User.php b/demo/app/Models/User.php index 47a047ab8..ec161bcd4 100644 --- a/demo/app/Models/User.php +++ b/demo/app/Models/User.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphOne; @@ -38,6 +39,13 @@ public function avatar(): MorphOne ->where('model_key', 'avatar'); } + public function avatarUrl(): Attribute + { + return Attribute::make( + get: fn () => $this->avatar?->thumbnail(500), + ); + } + public function getDefaultAttributesFor(string $attribute): array { return in_array($attribute, ['avatar']) diff --git a/demo/app/Providers/DemoSharpServiceProvider.php b/demo/app/Providers/DemoSharpServiceProvider.php index 464f0919e..3c4c6a7d5 100644 --- a/demo/app/Providers/DemoSharpServiceProvider.php +++ b/demo/app/Providers/DemoSharpServiceProvider.php @@ -42,6 +42,7 @@ protected function configureSharp(SharpConfigBuilder $config): void ->setAuthCustomGuard('web') ->setLoginAttributes('email', 'password') ->setUserDisplayAttribute('name') + ->setUserAvatarAttribute('avatar_url') ->enable2faCustom(Demo2faNotificationHandler::class) ->enableLoginRateLimiting(maxAttempts: 3) ->suggestRememberMeOnLoginForm() diff --git a/docs/guide/authentication.md b/docs/guide/authentication.md index d6d6f6640..0b9498c9c 100644 --- a/docs/guide/authentication.md +++ b/docs/guide/authentication.md @@ -14,12 +14,14 @@ class SharpServiceProvider extends SharpAppServiceProvider $config ->setLoginAttributes('login', 'pwd') ->setUserDisplayAttribute('last_name') + ->setUserAvatarAttribute('avatar_url') // [...] } } ``` -The `setUserDisplayAttribute()` is useful to display the user's name in the Sharp UI. Default is `name`. +- The `setUserDisplayAttribute()` is useful to display the user's name in the Sharp UI. Default is `name`. +- The `setUserAvatarAttribute()` is useful to display the user's avatar in the Sharp UI. Default is `avatar`, when the attribute returns null, the default user icon is displayed instead. ## Login form diff --git a/resources/js/Layouts/Layout.vue b/resources/js/Layouts/Layout.vue index 533cc187b..71ca2fadd 100644 --- a/resources/js/Layouts/Layout.vue +++ b/resources/js/Layouts/Layout.vue @@ -206,7 +206,10 @@ class="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground" > - + +
{{ auth().user.name }} diff --git a/resources/js/types/generated.d.ts b/resources/js/types/generated.d.ts index 333ffd568..6100d8558 100644 --- a/resources/js/types/generated.d.ts +++ b/resources/js/types/generated.d.ts @@ -852,6 +852,7 @@ export type ShowTextFieldData = { export type UserData = { name: string | null; email: string | null; + avatar: string | null; }; export type UserMenuData = { items: Array; diff --git a/src/Config/SharpConfigBuilder.php b/src/Config/SharpConfigBuilder.php index fdea30efe..095dc64fe 100644 --- a/src/Config/SharpConfigBuilder.php +++ b/src/Config/SharpConfigBuilder.php @@ -49,6 +49,7 @@ class SharpConfigBuilder 'login_page_url' => null, 'display_attribute' => 'name', 'login_attribute' => 'email', + 'avatar_attribute' => 'avatar', 'password_attribute' => 'password', 'impersonate' => [ 'enabled' => false, @@ -327,6 +328,13 @@ public function setUserDisplayAttribute(string $displayAttribute): self return $this; } + public function setUserAvatarAttribute(string $avatar): self + { + $this->config['auth']['avatar_attribute'] = $avatar; + + return $this; + } + public function setAuthCustomGuard(?string $guardName): self { $this->config['auth']['guard'] = $guardName; diff --git a/src/Data/UserData.php b/src/Data/UserData.php index 652064e01..d16b42fd3 100644 --- a/src/Data/UserData.php +++ b/src/Data/UserData.php @@ -12,6 +12,7 @@ final class UserData extends Data public function __construct( public ?string $name, public ?string $email, + public ?string $avatar, ) {} public static function from(Authenticatable $user): self @@ -19,6 +20,7 @@ public static function from(Authenticatable $user): self return new self( name: $user->{sharp()->config()->get('auth.display_attribute')} ?? null, email: $user->{sharp()->config()->get('auth.login_attribute')} ?? null, + avatar: $user->{sharp()->config()->get('auth.avatar_attribute')} ?? null, ); } }