Use middleware except if auth user is admin

3 weeks ago 31
ARTICLE AD BOX

I have a small app where users have to subscribe to post topics.

I use Laravel Cashier with Stripe and it works fine.

I have created a middleware to check if auth::user() is subscribed or not :

public function handle(Request $request, Closure $next): Response { if (! $request->user()?->subscribedToPrice([env('MONTH_SUBSCRIPTION'), env('YEAR_SUBSCRIPTION')])) { return redirect('/subscribe/create'); } return $next($request); }

In the TopicController :

$this->middleware('auth')->except(['index', 'show']); $this->middleware(EnsureUserIsSubscribed::class)->except(['index', 'show']);

Everything works great for subscribed users, but I want the users who have the admin role to publish without subscribing, and I'm stuck.

In different views I use this to check if user is admin (even if I could probably use a new middleware)

if (Auth::user() && $user->roles->pluck('name')->contains('admin'))

Any help would be appreciated.

Thank you!

Read Entire Article