Home > Enterprise >  Laravel 8 How to restrict user login if email is not verified
Laravel 8 How to restrict user login if email is not verified

Time:10-29

I am using Bootstrap Starter template package in Laravel 8 that replaces Tailwind CSS framework with Bootstrap CSS framework. https://packagist.org/packages/shahvirag/laravel-ui-bootstrap

The problem I'm having is that user can sign in after registration without needing to verify account through email. How can I restrict his access until the email is not verified?

I have followed the steps and tried using routes middleware, but there is no such file in App\Http\Controllers\PagesController;

Route::get('/',[PagesController::class, 'index'])->name('users')->middleware('verified'); // this does not work

Any help?

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PagesController;

Route::get('/', function () {
    return view('main');
});

Route::middleware(['auth'])->group(function() {
    Route::get('/home', function() {
        return view('home');
    })->name('home');

    Route::get('/user/profile', function() {
        return view('profile');
    })->name('profile');
});

Route::get('/',[PagesController::class, 'index'])->name('users')->middleware('verified');

resources/views/auth/login.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Login') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('login') }}">
                        @csrf

                        <div hljs-string">">
                            <label for="email" hljs-number">4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                            <div hljs-number">6">
                                <input id="email" type="email" hljs-built_in">error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>

                                @error('email')
                                    <span hljs-string">" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div hljs-string">">
                            <label for="password" hljs-number">4 col-form-label text-md-right">{{ __('Password') }}</label>

                            <div hljs-number">6">
                                <input id="password" type="password" hljs-built_in">error('password') is-invalid @enderror" name="password" required autocomplete="current-password">

                                @error('password')
                                    <span hljs-string">" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div hljs-string">">
                            <div hljs-number">6 offset-md-4">
                                <div hljs-string">">
                                    <input hljs-string">" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>

                                    <label hljs-string">" for="remember">
                                        {{ __('Remember Me') }}
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div hljs-number">0">
                            <div hljs-number">8 offset-md-4">
                                <button type="submit" hljs-string">">
                                    {{ __('Login') }}
                                </button>

                                @if (Route::has('password.request'))
                                    <a hljs-string">" href="{{ route('password.request') }}">
                                        {{ __('Forgot Your Password?') }}
                                    </a>
                                @endif
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

CodePudding user response:

To use verified middleware, your user model should implement MustVerifyEmail

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    // ...
}

CodePudding user response:

Try this instead

Route::get('/',[PagesController::class, 'index'])->name('users')->middleware(['auth', 'verified']);

CodePudding user response:

You can use the method included with the User model:

$user->hasVerifiedEmail()

Or in the routes middleware(['verified'])

  • Related