Are you trying to execute more middleware only if the user has the given role or check the role of a user after they are authenticated?
Middleware Before Auth
Hello guys,
to a problem and I would love you to help me ... I created a middleware to check the user's role .. more this middleware must be performed only after the user made the sign ... how should I complete this middleware ?
Thanks
@thomaskim Beat me to it.
@NetoPVH Assuming that 2 is the id of your common role, your middleware could have a handle method like:
public function handle($request, Closure $next)
{
if (Auth::user()->roles()->count() == 0) {
Auth::user()->roles()->attach(Role::find(2));
}
return $next($request);
}
If you run the middleware after auth as @thomaskim suggests, this middleware will only run if the user is authenticated and the user doesn't have any roles assigned.
Also make sure you import the Auth facade or assign your user variable as you did before.
use Illuminate\Support\Facades\Auth;
class YourMiddlewareClass {
Please or to participate in this conversation.