PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Question By
Solved

Possible to perform login credential check without a page reload?

Solved
alexmorning

Jun 15th, 2023 09:57 AM

I tried to find the code for this function but I'm having a hard time. If it's possible, I would like to perform this check without a reload so that your email doesn't get removed if you type the wrong password.

It's not the biggest deal in the world so if it's like some complex thing to do I just won't worry about it. But if it's simple I'd love to implement this.

Please let me know if you have any tips! Thank you

bobbyiliev

Jun 15th, 2023 12:41 PM

Best Answer

Hey Alex!

Yes, you can use Laravel Livewire to implement a login form without a page reload. This will allow you to maintain the form state even if the user inputs incorrect credentials. Here is an example of how you could implement this:

  • To create the Livewire component:
php artisan make:livewire LoginForm

This command will create two new files:

  • app/Http/Livewire/LoginForm.php - This is where you will put the component's PHP class.

  • resources/views/livewire/login-form.blade.php - This is where you will put the component's HTML.

Once these files are created, you can add the code I provided in my previous message.

The LoginForm.php file would look something like this:

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Illuminate\Support\Facades\Auth;

class LoginForm extends Component
{
    public $email;
    public $password;
    public $error;

    protected $rules = [
        'email' => 'required|email',
        'password' => 'required',
    ];

    public function login()
    {
        $this->validate();

        if (!Auth::attempt(['email' => $this->email, 'password' => $this->password])) {
            $this->error = 'The provided credentials are incorrect.';
            return;
        }

        return redirect()->intended('dashboard');
    }

    public function render()
    {
        return view('livewire.login-form');
    }
}

And the blade view would look something like this:

<div>
    <form wire:submit.prevent="login">
        <input type="email" wire:model="email" placeholder="Email">
        @error('email') <span>{{ $message }}</span> @enderror

        <input type="password" wire:model="password" placeholder="Password">
        @error('password') <span>{{ $message }}</span> @enderror

        @if($error)
        <span>{{ $error }}</span>
        @endif

        <button type="submit">Log in</button>
    </form>
</div>

This is just a simplified version, but you should be able to get the form classes from this file here and re-implement them as needed:

views/themes/tailwind/auth/login.blade.php

You can then include this Livewire component in the login blade view of the theme that you are using with:

@livewire('login-form')

Also, if you are not already using Livewire, you will have to include the scripts and styles with:

<head>
    ...
    @livewireStyles
</head>
<body>
    ...
    @livewireScripts
</body>

Let me know if you have any questions!

alexmorning

Jun 15th, 2023 01:21 PM

Wow, that is more than I was expecting, thank you for taking the time put all that in there. I'm gonna start messing with it now and see what I can get going with it. That is so helpful.

I also replied on that Paddle inline thread, I'm not sure if it notifies you for really old threads like that lol. It doesn't bump it to the top of the feed anymore so I figured it might not alert you at all.

bobbyiliev

Jun 16th, 2023 02:08 AM

Hey! No worries at all! Let me know how it goes and if you hit any blockers!

alexmorning

Jun 17th, 2023 04:26 PM

I've tried so many things but cannot seem to figure out what to do to fix this.

I'm getting this error

Unable to set component data. Public property [$email] not found on component: [login-form]

When I use the html you gave me for login form, at first I got the error about an undefined variable ($error) but I just gave up on that temporarily and commented out the code. When I do that is when I get the $email error above.

I figured for my previous login form, there was code elsewhere that designed the email input as $email, because I didn't see anything in the HTML that looked like it did that. But I wasn't able to find anything like that. I wasn't sure if I was just missing it, or there was something that this new livewire component needs that the other one didn't, and if that's the case then I really need help, lmao.

Thank you again, hopefully someone else uses all of your help in the future and this isn't only for just me 🙃

alexmorning

Jun 17th, 2023 04:32 PM

Nevermind, I'm an idiot. I pasted your LoginForm.php code into the file but didn't save it....

Christ what waste of time.

alexmorning

Jun 17th, 2023 05:28 PM

Okay jesus christ, I am having a hell of a time, I have another blocker...

So immediately whenever I save your code you gave me for LoginForm.php I get this error

Unable to find component: [login-form]

ShareX_2023-06-17-17-25-14.jpg

But then I can go back to my LoginForm.php and undo the changes and go back to the original code that's generated when I created LoginForm, and now the error doesn't go away. I save the file, refresh my page and the missing component error is still there. I ran the livewire:discover command and still nothing.

When I initially made it, before I pasted in your code, my page loads with the form, but then I get the $email error after a bit (which I now understand why, because my LoginForm doesn't have your code yet). But when I add your code, then it breaks. The weird thing is that undoing your code does not fix it, it just still errors until I delete the component entirely and do php artisan:make again.

Any tips?

alexmorning

Jun 17th, 2023 05:56 PM

okay, I got that figured out as well. Your code did not include the namespace information that was in the original LoginForm.php file, so I had to add that in there. Once I did everything was good.

Just in case anyone sees this and has a similar issue, I had to add this line back to the top of my LoginForm.php right under the <?php

namespace App\Http\Livewire;

Sorry for all the unnecessary reading, I think I'm all good :)

Report
1
bobbyiliev

Jun 18th, 2023 04:25 AM

Hey Alex,

Happy to hear that you've figured this out! And thank you for sharing the extra details here with the community!

Best,

Bobby