Laravel 8 Custom User Login and Registration Example

Published on : July 19,2022
Laravel 8 Custom User Login and Registration Example

Hello Developer,
In this article will show laravel custom auth and regsiter example i am going to show you laravel 8 custom user Login and Registration. Though laravel provides auth scaffolding but in this article we will created custom login and registration features in our web application.

By Default Laravel provides the basic auth there includes the basic login, registration, and password reset modules. We can also customized default functionality easily.

But i this login and registration example article we will show you how to make login and registration without using laravel default auth. That mean it will be done programmatically.

In this article we have not verify email before registration and login. Just we will create simple login and registration code.

let's create our own custom laravel login and registration example from scratch So don't worry. Just follow the below step by step.

Let's start our Laravel custom auth and registration example.

 

1: Install Laravel 

We need install laravel project to complete our example. So download it using this command.

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

 

2: Create route

After downloading laravel we have to setup our route. routes\web.php

Route::namespace('Auth')->group(function () {
  Route::get('/login','LoginController@show_login_form')->name('login');
  Route::post('/login','LoginController@process_login')->name('login');
  Route::get('/register','LoginController@show_signup_form')->name('register');
  Route::post('/register','LoginController@process_signup');
  Route::post('/logout','LoginController@logout')->name('logout');
});

 

3:  Create New Controller : LoginController

Now we need write code on login controller for our custom login and registration form. So just you have to create it in this this following path. 

App\Http\Controllers\Auth\LoginController.php


namespace App\Http\Controllers\Auth;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{  
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    
    public function show_login_form()
    {
        return view('login');
    }
    public function process_login(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'password' => 'required'
        ]);

        $credentials = $request->except(['_token']);

        $user = User::where('name',$request->name)->first();

        if (auth()->attempt($credentials)) {

            return redirect()->route('home');

        }else{
            session()->flash('message', 'Invalid credentials');
            return redirect()->back();
        }
    }
    public function show_signup_form()
    {
        return view('backend.register');
    }
    public function process_signup(Request $request)
    {   
        $request->validate([
            'name' => 'required',
            'email' => 'required',
            'password' => 'required'
        ]);
 
        $user = User::create([
            'name' => trim($request->input('name')),
            'email' => strtolower($request->input('email')),
            'password' => bcrypt($request->input('password')),
        ]);

        session()->flash('message', 'Your account is created');
       
        return redirect()->route('login');
    }
    public function logout()
    {
        \Auth::logout();

        return redirect()->route('login');
    }
}

 

4: Create Blade for View

In this step we need to create new blade file for login view and registrtaion view form. 

resourses\views\login.blade.php

<form method="post" action="{{ route('login') }}">
    @csrf
  <div class="form-group">
    <label>Username</label>
    <input type="text" name="name" class="form-control p_input">
  </div>
  <div class="form-group">
    <label>Password *</label>
    <input type="password" name="password" class="form-control p_input">
  </div>

  <div class="text-center">
    <button type="submit" class="btn btn-primary btn-block enter-btn">Login</button>
  </div>

  <p class="sign-up">Don't have an Account?<a href="{{ route('register') }}"> Sign Up</a></p>
</form>

Now paste this code to your register view blade.

resourses/views/register.blade.php

<form method="post" action="{{ route('register') }}">
  @csrf
<div class="form-group">
  <label>Username</label>
  <input type="text" name="name" class="form-control p_input">
</div>
<div class="form-group">
  <label>Email</label>
  <input type="email" name="email" class="form-control p_input">
</div>
<div class="form-group">
  <label>Password</label>
  <input type="password" name="password" class="form-control p_input">
</div>

<div class="text-center">
  <button type="submit" class="btn btn-primary btn-block enter-btn">Register</button>
</div>

<p class="sign-up text-center">Already have an Account?<a href="{{ route('login') }}"> Sign In</a></p>
<p class="terms">By creating an account you are accepting our<a href="#"> Terms & Conditions</a></p>
</form>

 

Now everything is done for login blade and registration blade. Just we need to setup logout method and route link for completing it. So create it. Paste it where you want to show logout button.

    <li class="nav-item menu-items">
      <a class="nav-link href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('frm-logout').submit();">
        
        <span class="menu-icon">
          <i class="mdi mdi-speedometer"></i>
        </span>
        <span class="menu-title">Logout</span>
    </a>    
    <form id="frm-logout" action="{{ route('logout') }}" method="POST" style="display: none;">
        {{ csrf_field() }}
    </form>
    </li> 

Now all going good. If you need to redirect user to custom path after successfully login then you have to do one thing. Go to this following path and change the path. 

app/Providers/RouteServiceProvider.php

public const HOME = '/add_your_custom_url_path_here';


Hope it can help you…

Categories : PHP Laravel

Tags : PHP Laravel HTML CSS Laravel 8

Praful Sangani
Praful Sangani
I'm a passionate full-stack developer with expertise in PHP, Laravel, Angular, React Js, Vue, Node, Javascript, JQuery, Codeigniter, and Bootstrap. I enjoy sharing my knowledge by writing tutorials and providing tips to others in the industry. I prioritize consistency and hard work, and I always aim to improve my skills to keep up with the latest advancements. As the owner of Open Code Solution, I'm committed to providing high-quality services to help clients achieve their business goals.


25 Comments

f



tricor 200mg without prescription buy fenofibrate tablets how to buy tricor


cialis 20mg viagra 50mg oral sildenafil for sale online


ketotifen 1mg generic buy zaditor tofranil 25mg usa


order acarbose without prescription fulvicin 250 mg pill fulvicin 250mg tablet


purchase aspirin online buy generic levoflox over the counter purchase zovirax sale


buy melatonin online order generic desogestrel 0.075mg order danazol 100mg sale


dipyridamole 100mg sale purchase dipyridamole online cheap pravastatin for sale


buy prasugrel online cheap buy generic detrol over the counter generic detrol 2mg


vasotec order online enalapril 10mg uk buy duphalac for sale


xalatan sale exelon 3mg over the counter buy exelon medication


premarin for sale online order dostinex 0.25mg pills purchase sildenafil online


omeprazole 10mg uk buy omeprazole 20mg pill buy metoprolol 50mg pills


micardis 80mg sale where can i buy hydroxychloroquine buy molnunat cheap


cialis 5mg ca cialis 10mg brand buy viagra 100mg online


order cenforce 50mg generic naproxen pills where can i buy chloroquine


modafinil 200mg generic purchase modafinil sale order prednisone 40mg


purchase cefdinir generic order metformin for sale buy generic prevacid


order isotretinoin 10mg sale amoxicillin 500mg over the counter how to buy azithromycin


azithromycin 500mg without prescription buy gabapentin 800mg sale gabapentin 100mg generic


lipitor 40mg over the counter norvasc 5mg usa order amlodipine pills


real online casino san manuel casino online cost furosemide


big fish casino online albuterol price buy ventolin inhalator generic


buy protonix 20mg generic order lisinopril generic phenazopyridine medication


Leave a comment

We'll never share your email with anyone else. Required fields are marked *

Related Articles

Access specifier in php
Praful Sangani By Praful Sangani - July 20,2022