0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Laravel】GuestLoginの場合、Profileを編集不可にする手順

Last updated at Posted at 2025-04-16

ModelにisGuest()を追記

/app/Models/User.php
<?php

namespace App\Models;

class User extends Authenticatable
{

    ~~~

    // ゲストログイン
    public function isGuest()
    {
    return $this->email === 'guest@example.com';
    }
}

viewsを編集

タイトルに編集不可であることを追記

/views/profile/edit.blade.php
{{ __('Profile') }} @if(Auth::user()->isGuest()) (GuestLoginの場合、編集不可) @endif

スクリーンショット 2025-04-16 12.19.10.png

「update-profile-information-form.blade.php」の「name」と「email」を編集不可にする

inputの編集

「:readonly="Auth::user()->isGuest()"」を追記する

/views/profile/partials/update-profile-information-form.blade.php
# name
<x-text-input id="name" name="name" type="text" class="mt-1 block w-full" :value="old('name', $user->name)" required autofocus autocomplete="name" 
    :readonly="Auth::user()->isGuest()" />

# email
<x-text-input id="email" name="email" type="email" class="mt-1 block w-full" :value="old('email', $user->email)" required autocomplete="username"
    :readonly="Auth::user()->isGuest()" />

buttonの編集

/views/profile/partials/update-profile-information-form.blade.php
# 編集前
{{-- <div class="flex items-center gap-4">
    <x-primary-button>{{ __('Save') }}</x-primary-button>

    @if (session('status') === 'profile-updated')
        <p
            x-data="{ show: true }"
            x-show="show"
            x-transition
            x-init="setTimeout(() => show = false, 2000)"
            class="text-sm text-gray-600"
        >{{ __('Saved.') }}</p>
    @endif
</div> --}}

# 編集後
<div class="flex items-center gap-4">
    @if(Auth::user()->isGuest())
        <x-primary-button disabled class="opacity-50 cursor-not-allowed">
            {{ __('Save') }}
        </x-primary-button>
    @else
        <x-primary-button>
            {{ __('Save') }}
        </x-primary-button>
    @endif

    @if (session('status') === 'profile-updated')
        <p
            x-data="{ show: true }"
            x-show="show"
            x-transition
            x-init="setTimeout(() => show = false, 2000)"
            class="text-sm text-gray-600"
        >{{ __('Saved.') }}</p>
    @endif
</div>    

スクリーンショット 2025-04-16 12.24.11.png

「update-password-form.blade.php」の「current_password」と「password」と「password_confirmation」を編集不可にする

inputの編集

「:readonly="Auth::user()->isGuest()"」を追記する

/views/profile/partials/update-password-form.blade.php
# current-password
<x-text-input id="current_password" name="current_password" type="password" class="mt-1 block w-full" autocomplete="current-password"
    :readonly="Auth::user()->isGuest()" />

# password
<x-text-input id="password" name="password" type="password" class="mt-1 block w-full" autocomplete="new-password"
                :readonly="Auth::user()->isGuest()" />

# password_confirmation
<x-text-input id="password_confirmation" name="password_confirmation" type="password" class="mt-1 block w-full" autocomplete="new-password"
                :readonly="Auth::user()->isGuest()" />

buttonの編集

/views/profile/partials/update-password-form.blade.php
# 編集前
{{-- <div class="flex items-center gap-4">
    <x-primary-button>{{ __('Save') }}</x-primary-button>

    @if (session('status') === 'password-updated')
        <p
            x-data="{ show: true }"
            x-show="show"
            x-transition
            x-init="setTimeout(() => show = false, 2000)"
            class="text-sm text-gray-600"
        >{{ __('Saved.') }}</p>
    @endif
</div> --}}

# 編集後
<div class="flex items-center gap-4">
    @if(Auth::user()->isGuest())
        <x-primary-button disabled class="opacity-50 cursor-not-allowed">
            {{ __('Save') }}
        </x-primary-button>
    @else
        <x-primary-button>
            {{ __('Save') }}
        </x-primary-button>
    @endif

    @if (session('status') === 'password-updated')
        <p
            x-data="{ show: true }"
            x-show="show"
            x-transition
            x-init="setTimeout(() => show = false, 2000)"
            class="text-sm text-gray-600"
        >{{ __('Saved.') }}</p>
    @endif
</div>

スクリーンショット 2025-04-16 12.28.14.png

「delete-user-form.blade.php」の「button2箇所」を編集不可にする

1箇所目の編集

/views/profile/partials/delete-user-form.blade.php
# 編集前
{{-- <x-danger-button
    x-data=""
    x-on:click.prevent="$dispatch('open-modal', 'confirm-user-deletion')"
>{{ __('Delete Account') }}</x-danger-button> --}}

# 編集後
@if(Auth::user()->isGuest())
    <x-danger-button disabled class="opacity-50 cursor-not-allowed">
        {{ __('Delete Account') }}
    </x-danger-button>
@else
    <x-danger-button
        x-data=""
        x-on:click.prevent="$dispatch('open-modal', 'confirm-user-deletion')"
    >
        {{ __('Delete Account') }}
    </x-danger-button>
@endif

スクリーンショット 2025-04-16 12.35.14.png

2箇所目の編集

こちらは1箇所目を修正していれば表示されることはないが、念のために修正する

/views/profile/partials/delete-user-form.blade.php
# 編集前
{{-- <x-danger-button class="ml-3">
    {{ __('Delete Account') }}
</x-danger-button> --}}

# 編集後
@if(Auth::user()->isGuest())
    {{-- 表示はされるが無効化 --}}
    <x-danger-button class="ml-3 opacity-50 cursor-not-allowed" disabled>
        {{ __('Delete Account') }}
    </x-danger-button>
@else
    {{-- 通常ユーザー用 --}}
    <x-danger-button type="submit" class="ml-3">
        {{ __('Delete Account') }}
    </x-danger-button>
@endif

スクリーンショット 2025-04-16 12.35.49.png

参考:GuestLoginの実装

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?