Authentication

Check

@if (user())
    Logged in as {{ user()->name }} | @livewire('logout')
@else
    @livewire('login')
@endif

Login & Password Reset

<div>
    @if ($forgot)
        <form wire:submit.prevent="sendResetLink" class="space-y-4">
            @if ($sent)
                <div class="flex items-center space-x-2 bg-green-50 text-green-500 border border-green-500 px-5 py-4 rounded-md">
                    {{ icon('check') }}
                    <div>We have e-mailed your password reset link!</div>
                </div>
            @endif

            <div>
                <label for="email" class="block mb-1">Email</label>
                <input type="email" wire:model.defer="email" id="email" class="form-input" autofocus>
                @error('email')
                    <div class="text-sm text-red-500 mt-1">{{ $message }}</div>
                @enderror
            </div>

            <div>
                <button type="submit">Send Reset Link</button>
            </div>

            <div>
                <button type="button" wire:click="$set('forgot', false)">Back to login</button>
            </div>
        </form>
    @elseif ($token)
        <form wire:submit.prevent="resetPassword" class="space-y-4">
            <div>
                <label for="email" class="block mb-1">Email</label>
                <input type="email" wire:model.defer="email" id="email" class="form-input" autofocus>
                @error('email')
                    <div class="text-sm text-red-500 mt-1">{{ $message }}</div>
                @enderror
            </div>

            <div>
                <label for="password" class="block mb-1">New Password</label>
                <input type="password" wire:model.defer="password" id="password" class="form-input">
                @error('password')
                    <div class="text-sm text-red-500 mt-1">{{ $message }}</div>
                @enderror
            </div>

            <div>
                <button type="submit">Reset Password</button>
            </div>
        </form>
    @else
        <form wire:submit.prevent="login" class="space-y-4">
            <div>
                <label for="email" class="block mb-1">Email</label>
                <input type="email" wire:model.defer="email" id="email" class="form-input" autofocus>
                @error('email')
                    <div class="text-sm text-red-500 mt-1">{{ $message }}</div>
                @enderror
            </div>

            <div>
                <label for="password" class="block mb-1">Password</label>
                <input type="password" wire:model.defer="password" id="password" class="form-input">
                @error('password')
                    <div class="text-sm text-red-500 mt-1">{{ $message }}</div>
                @enderror
            </div>

            <div>
                <label class="inline-flex items-center space-x-2">
                    <input type="checkbox" wire:model.defer="remember">
                    <div>Remember me</div>
                </label>
            </div>

            <div>
                <button type="submit">Login</button>
            </div>

            <div>
                <button type="button" wire:click="$set('forgot', true)">Forgot password?</button>
            </div>
        </form>
    @endif
</div>

<?php

class Login extends Component
{
    public $url;

    public $email;
    public $password;
    public $remember = false;

    public $token;
    public $forgot = false;
    public $sent = false;

    protected $queryString = [
        'token',
    ];

    public function mount()
    {
        $this->url = request()->url();
    }

    public function login()
    {
        $this->validate([
            'email' => 'required|string|email',
            'password' => 'required|string',
            'remember' => 'boolean',
        ]);

        if (! $this->guard()->attempt(['username' => $this->email, 'password' => $this->password], $this->remember)) {
            $this->throwValidationError('email', 'These credentials do not match our records.');
        }

        return redirect($this->url);
    }

    public function sendResetLink()
    {
        $this->validate([
            'email' => 'required|string|email',
        ]);

        if (! $user = $this->guard()->findUser($this->email)) {
            $this->throwValidationError('email', 'No user can be found with this email address.');
        }

        mail('reset-password')
            ->parameter('url', $this->url.'?token='.$user->createPasswordResetToken())
            ->to($user->email)
            ->send();

        $this->sent = true;
    }

    public function resetPassword()
    {
        $this->validate([
            'email' => 'required|string|email',
            'password' => 'required|string|min:8',
        ]);

        if (! $user = $this->guard()->findUser($this->email)) {
            $this->throwValidationError('email', 'No user can be found with this email address.');
        }

        if (! $user->resetPassword($this->token, $this->password)) {
            $this->throwValidationError('password', 'The reset token is not correct or expired. Try a new password reset from scratch.');
        }

        $this->guard()->login($user);

        return redirect($this->url);
    }

    protected function guard()
    {
        // Replace this with guard('slug') if you want to use a
        // specific guard instead of the default one

        return guard();
    }
}

Logout

<button type="button" wire:click="logout">
    Logout
</button>

<?php

class Logout extends Component
{
    public $url;

    public function mount()
    {
        $this->url = request()->url();
    }

    public function logout()
    {
        $this->guard()->logout();

        return redirect($this->url);
    }

    protected function guard()
    {
        // Replace this with guard('slug') if you want to use a
        // specific guard instead of the default one

        return guard();
    }
}

Password Reset Mail

@component('mail::layout')

@slot('header')
@component('mail::header', ['url' => config('app.url')])
@endcomponent
@endslot

# Reset Password Notification

You are receiving this email because we received a password reset request for your account.

@component('mail::button', ['url' => $parameters['url'], 'color' => 'success'])
Reset Password
@endcomponent

This password reset link will expire in 24 hours.

If you did not request a password reset, no further action is required.

@slot('footer')
@component('mail::footer')
© {{ date('Y') }} {{ globals('general')->website_title }}. All rights reserved.
@endcomponent
@endslot

@endcomponent