Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add middleware after login in laravel

Tags:

php

laravel

I am using laravel for my web application,in login I am asking for username ,password and I want to check the email of the logged in user is verified or not. If the verified status is 0 I want to sent the error message to the login page using the verifiedemail named middleware.

route.php

Route::group(['middleware' => 'auth', 'superadmin'], function () {
    Route::resource('/users', 'UserController'); 
});
Route::get('/', function () {
    if (Auth::guest())
        return view('/auth/login');
    else
        return redirect('/tests');
});
Route::resource('/tests', 'TestController');
Route::get('/sites', 'SiteController@index'); 
Auth::routes();
Route::get('/home', 'HomeController@index');

Redirectedifauthenticated.php <--- middleware file

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/home');
    }
    return $next($request);
}

verifiedemail.php <--- middleware file

public function handle($request, Closure $next)
{
    if ( Auth::check() && Auth::user()->isVerifiedEmail() )
    {
        return redirect('/login');
    }
    return $next($request);
}

kernel.php

 protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'superadmin' => 'App\Http\Middleware\SuperAdmin',
        'verifiedemail' => 'App\Http\Middleware\VerifiedEmail',
    ];
}

I think these are the files where i have to change but what and where I have to change that's the question for me .please help thanks in advance.

like image 731
Alfiza malek Avatar asked Nov 23 '16 09:11

Alfiza malek


People also ask

How can you assign middleware in Laravel?

Assigning Middleware To Routes If you would like to assign middleware to specific routes, you should first assign the middleware a key in your application's app/Http/Kernel.php file. By default, the $routeMiddleware property of this class contains entries for the middleware included with Laravel.

Where do I register middleware in Laravel?

The middleware can be registered at app/Http/Kernel. This file contains two properties $middleware and $routeMiddleware.

What is signed middleware in Laravel?

If you have a route under signed middleware, it means that all those routes should have valid signature. Otherwise it will give you 403 error. When you call URL::signedRoute(..) , that signature particularly represent that specific route url.


1 Answers

If you're using the default laravel authentication you can add a listener on the Illuminate\Auth\Events\Attempting which is fired on every login attempt and do your validation in the listener.

More about fired event on Auth

More about event listeners

like image 111
jeanj Avatar answered Oct 18 '22 13:10

jeanj