Laravel Passwordless Authentication Part 2

1 year ago admin Laravel

In the second part of this tutorial, we will add the controller which will hold the functions for storing and authenticating users, and we will add the base layout with the navigation menu.


Create the controller

First, let's create the 'UserController' here we have the functions for storing and authenticating users.

                                                    
                                                                                                                
<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Mail\LoginLink;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Mail;

class UserController extends Controller
{
    //
    public function login() 
    {
        return view('auth.login');
    }

    public function auth(Request $request)
    {
        $request->validate([
            'email' => ['required', 'email', Rule::exists(User::class, 'email')]
        ]);

        Mail::to($request->email)->send(new LoginLink($request->email));
        return redirect()->back()->with([
            'success' => 'Your login link has been sent please check your mail box'
        ]);
    }

    public function register() 
    {
        return view('auth.register');
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => ['required','max:255'],
            'email' => ['required', 'email', 'unique:users']
        ]);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email
        ]);

        Mail::to($request->email)->send(new LoginLink($user->email));
        return redirect()->back()->with([
            'success' => 'Your login link has been sent please check your mail box'
        ]);
    }

    public function logout()
    {
        auth()->logout();
        return redirect()->route('login');
    }

    public function session($email)
    {
        $user = User::whereEmail($email)->first();
        auth()->login($user);
        return redirect()->route('home');
    }


}


Create the base layout

Inside views let's create a new folder 'layouts' inside let's add a new file 'app.blade.php' inside we have CSS & JS links.

                                                        
                                                                                                                        
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <!-- Font Awesome -->
    <link
        href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"
        rel="stylesheet"
    />
        <!-- Google Fonts -->
    <link
        href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
        rel="stylesheet"
    />
        <!-- MDB -->
    <link
        href="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/6.4.2/mdb.min.css"
        rel="stylesheet"
    />
    <title>Laravel Password Less Auth</title>
</head>
<body class="bg-light">
    @include('layouts.navbar')
    <div class="container">
        @yield('content')
    </div>
    <!-- MDB -->
    <script
        type="text/javascript"
        src="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/6.4.2/mdb.min.js"
    ></script>
</body>
</html>

Create the navbar menu

Next, let's add a new file 'navbar.blade.php' Inside we have the navigation menu.

                                                        
                                                                                                                        
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <!-- Container wrapper -->
  <div class="container-fluid">
    <!-- Toggle button -->
    <button
      class="navbar-toggler"
      type="button"
      data-mdb-toggle="collapse"
      data-mdb-target="#navbarSupportedContent"
      aria-controls="navbarSupportedContent"
      aria-expanded="false"
      aria-label="Toggle navigation"
    >
      <i class="fas fa-bars"></i>
    </button>

    <!-- Collapsible wrapper -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <!-- Navbar brand -->
      <a class="navbar-brand mt-2 mt-lg-0" href="{{route('home')}}">
        Laravel Password Less Auth
      </a>
      <!-- Left links -->
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <li class="nav-item">
          <a class="nav-link" href="{{route('home')}}">
              <i class="fas fa-home"></i> Home
          </a>
        </li>
        @guest
          <li class="nav-item">
              <a class="nav-link" href="{{route('login')}}">
                  <i class="fas fa-sign-in"></i> Login
              </a>
          </li>
          <li class="nav-item">
              <a class="nav-link" href="{{route('register')}}">
                  <i class="fas fa-user-plus"></i> Register
              </a>
          </li>
        @endguest
      </ul>
      <!-- Left links -->
    </div>
    <!-- Collapsible wrapper -->

    <!-- Right elements -->
    @auth
      <div class="d-flex align-items-center">
          <!-- Avatar -->
          <div class="dropdown">
              <a
                  class="dropdown-toggle d-flex align-items-center hidden-arrow"
                  href="#"
                  id="navbarDropdownMenuAvatar"
                  role="button"
                  data-mdb-toggle="dropdown"
                  aria-expanded="false"
              >
                  <img
                  src="https://mdbcdn.b-cdn.net/img/new/avatars/2.webp"
                  class="rounded-circle"
                  height="25"
                  alt="Black and White Portrait of a Man"
                  loading="lazy"
                  />
              </a>
              <ul
                  class="dropdown-menu dropdown-menu-end"
                  aria-labelledby="navbarDropdownMenuAvatar"
              >
                  <li>
                      <a class="dropdown-item" href="#">{{auth()->user()->name}}</a>
                  </li>
                  <li>
                      <form id="formLogout" action="{{route('logout')}}" method="post">
                          @csrf
                      </form>
                      <a class="dropdown-item" href="#"
                          onclick="document.getElementById('formLogout').submit();">Logout</a>
                  </li>
              </ul>
          </div>
      </div>   
    @endauth
    <!-- Right elements -->
  </div>
  <!-- Container wrapper -->
</nav>
<!-- Navbar -->

Display flash messages

To display validation errors and flash messages let's create a new file 'alerts.blade.php'.

NB: all the files are inside the layouts folder.

                                                        
                                                                                                                        
@if ($errors->any())
    @foreach ($errors->all() as $error)
        <div class="alert alert-danger">
            {{$error}}
        </div>
    @endforeach
@endif

@if (session()->has('success'))
    <div class="alert alert-success">
        {{session()->get('success')}}
    </div>
@endif

Related Tuorials

How to Conditionally Include a Blade Template in Laravel

In this lesson, we will see how to conditionally include a blade template in Laravel.Sometimes,...


How to Include a Blade Template Only if it Exists in Laravel

In this lesson, we will see how to include a blade template only if it exists in Laravel.Sometimes,&...


How to Pass a Variable to Include in Laravel

In this lesson, we will see how to pass a variable to include in Laravel. Sometimes, we want to pass...


How to the Get the Previous and Next Posts in Laravel

In this lesson, we will see how to get the previous and next posts in Laravel, sometimes when you ge...


How Do you Format a Number as K/M/B in Laravel

In this lesson, we will see how to format a number as K/M/B in Laravel, sometimes we want to display...


How to Override Laravel Fortify Default Registration Redirect

In this lesson, we will see how to override Laravel Fortify default registration redirect, sometimes...


How to Override Laravel Fortify Default Login Redirect

In this lesson, we will see how to override Laravel Fortify default login redirect, sometimes when w...


How to Use the Same Validation Form Request for both Create and Update in Laravel

In this lesson, we will see how to use the same validation form request for both create and update i...


How to Get Raw SQL Output from the Eloquent Model in Laravel 11

In this lesson, we will see how to get raw SQL output from the eloquent model in Laravel 11, sometim...


How to Check if a Record Does Not Exist in Laravel

in this lesson, we will see how to check if a record does not exist in laravel, sometimes you need t...