Reputation: 442
I seem to remember in a former version of laravel you could set some logic which fired before the roots were hit. What I am looking to do is:
if(Auth::check())
{
if(!Auth::user()->email || is_null(Auth::user()->email))
{
return redirect('/dashboard')->with('error', 'You have not provided an email address');
}
}
Reason being that I have a social login and Twitter logins do not provide an email address nor do some Facebook logins.
If you can think of a better option than redirecting the user to the dashboard and am add email form please share it, as on registration I send out a welcome email via an event handler and listener and it would save me putting the same logic in the controller before firing the event.
Upvotes: 0
Views: 89
Reputation: 16882
You can add another middleware that does this check, or you could just add it as another condition in the application's existing Authenticate
middleware.
Here is the default Authenticate
middleware in Laravel 5.2, with your code added to it. (you should be able to find this file in app/Http/Middleware/Authenticate.php
.)
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Authenticate
{
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
}
return redirect()->guest('login');
}
/* YOUR ADDED CODE STARTS HERE */
// Note that you don't need to call `Auth::check` because
// `Auth::guest` has already been called above and has returned
// `false` in order to get here.
if(!Auth::user()->email || is_null(Auth::user()->email))
{
return redirect('/dashboard')->with('error', 'You have not provided an email address');
}
/* YOUR ADDED CODE ENDS HERE */
return $next($request);
}
}
Upvotes: 1