Livewire Login Register in Laravel

Livewire Login Register in Laravel

Laravel livewire registration and login. Here, we will show you how to create livewire registration and login in laravel app.

You must have already used Laravel’s default login and registration system.

But, In this tutorial, we will guide you step by step on how to create livewire login and registration in laravel.

Laravel Livewire Registration and Login Tutorial

Just follow the below given simple and easy steps to create livewire registration and login in laravel:

  • Step 1: Download Laravel App
  • Step 2: Add Database Detail
  • Step 3: Run Migration Command
  • Step 4: Install Livewire to Create Components
  • Step 5: Generate Livewire Components for Login & Registration
  • Step 6: Add Routes
  • Step 7: Create View File
  • Step 8: Run Development Server

Step 1: Download Laravel App

First of all, Open your terminal OR command prompt and run following command to install laravel fresh app for laravel livewire registration and login project:

 composer create-project --prefer-dist laravel/laravel blog 

Step 2: Add Database Detail

In this step, Add database credentials in the .env file. So open your project root directory and find .env file. Then add database detail in .env file:

DB_CONNECTION=mysql  
DB_HOST=127.0.0.1  
DB_PORT=3306  
DB_DATABASE=here your database name here 
DB_USERNAME=here database username here 
DB_PASSWORD=here database password here

Step 3: Run Migration Command

Now, Open command prompt and run the following command to create the table into your added database:

php artisan migrate

Step 4: Install Livewire to Create Components

In this step, you need to install livewire to your laravel project using the following command:

composer require livewire/livewire

Step 5: Generate Livewire Components for Login & Registration

In this step, create the livewire components for creating a laravel livewire registration and login components using the following command. So Open your cmd and run the following command:

php artisan make:livewire registration-login

This command will create the following components on the following path:

app/Http/Livewire/RegistrationLogin.php
resources/views/livewire/registration-login.blade.php

Now, Navigate to app/Http/Livewire folder and open RegistrationLogin.php file. Then add the following code into your RegistrationLogin.php file:

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Hash;
use App\User;

class RegistrationLogin extends Component
{
    public $users, $email, $password, $name;
    public $registerForm = false;

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

    private function resetInputFields(){
        $this->name = '';
        $this->email = '';
        $this->password = '';
    }

    public function login()
    {
        $validatedDate = $this->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);
        
        if(\Auth::attempt(array('email' => $this->email, 'password' => $this->password))){
                session()->flash('message', "You have been successfully login.");
        }else{
            session()->flash('error', 'email and password are wrong.');
        }
    }

    public function register()
    {
        $this->registerForm = !$this->registerForm;
    }

    public function registerStore()
    {
        $v = $this->validate([
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required',
        ]);

        $this->password = Hash::make($this->password); 

        $data = [ 'name' => $this->name, 
                  'email' => $this->email,
                  'password' => $this->password
                ];

        User::create($data);

        session()->flash('message', 'You have been successfully registered.');

        $this->resetInputFields();

    }
}

After that, Navigate to resources/views/livewire folder and open registration-login.blade.php file. Then add the following code into your registration-login.blade.php file:

<div>
<div class="row">
<div class="col-md-12">
@if (session()->has('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
@endif
@if (session()->has('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
@endif
</div>
</div>
@if($registerForm)
<form>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Name :</label>
<input type="text" wire:model="name" class="form-control">
@error('name') <span class="text-danger error">{{ $message }}</span>@enderror
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Email :</label>
<input type="text" wire:model="email" class="form-control">
@error('email') <span class="text-danger error">{{ $message }}</span>@enderror
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Password :</label>
<input type="password" wire:model="password" class="form-control">
@error('password') <span class="text-danger error">{{ $message }}</span>@enderror
</div>
</div>
<div class="col-md-12 text-center">
<button class="btn text-white btn-success" wire:click.prevent="registerStore">Register</button>
</div>
<div class="col-md-12">
<a class="text-primary" wire:click.prevent="register"><strong>Login</strong></a>
</div>
</div>
</form>
@else
<form>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Email :</label>
<input type="text" wire:model="email" class="form-control">
@error('email') <span class="text-danger error">{{ $message }}</span>@enderror
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Password :</label>
<input type="password" wire:model="password" class="form-control">
@error('password') <span class="text-danger error">{{ $message }}</span>@enderror
</div>
</div>
<div class="col-md-12 text-center">
<button class="btn text-white btn-success" wire:click.prevent="login">Login</button>
</div>
<div class="col-md-12">
Don't have account? <a class="btn btn-primary text-white" wire:click.prevent="register"><strong>Register Here</strong></a>
</div>
</div>
</form>
@endif
</div>

Step 6: Create Route

In this step, Navigate to routes folder and open web.php. Then add the following routes into your web.php file:

Route::view('registration-login','livewire.home');

Step 7: Create View File

In this step, navigate to resources/views/livewire folder and create one blade view files that name home.blade.php file. Then add the following code into your home.blade.php file:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel Livewire Registration and Login Example - tutsmake.com</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.1/css/bootstrap.min.css">
<!-- Styles -->
<style>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Nunito', sans-serif;
font-weight: 200;
height: 100vh;
margin: 0;
}
.full-height {
height: 100vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.top-right {
position: absolute;
right: 10px;
top: 18px;
}
.content {
text-align: center;
}
.title {
font-size: 84px;
}
.links > a {
color: #636b6f;
padding: 0 25px;
font-size: 13px;
font-weight: 600;
letter-spacing: .1rem;
text-decoration: none;
text-transform: uppercase;
}
.m-b-md {
margin-bottom: 30px;
}
</style>
</head>
<body>
<div class="container mt-5">
<div class="row mt-5 justify-content-center">
<div class="mt-5 col-md-8">
<div class="card">
<div class="card-header bg-primary text-white"><h3>Laravel Livewire Registration and login Example - tutsmake.com</h3></div>
<div class="card-body">
@livewire('registration-login')
</div>
</div>
</div>
</div>
</div>
@livewireScripts
</body>
</html>
Note that, if you want to add HTML(blade views), CSS, and script code into your livewire files. So, you can use @livewireStyles, @livewireScripts, and @livewire(‘ blade views’).

Step 8: Run Development Server

Finally, you need to run the following PHP artisan serve command to start your laravel livewire registration and login app:

php artisan serve

If you want to run the project diffrent port so use this below command

php artisan serve --port=8080

Now, you are ready to run laravel livewire registration and login app. So open your browser and hit the following URL into your browser:

http://localhost:8000/registration-login

Recommended Laravel Tutorial

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *