source: app/Http/Middleware/UserRole.php

Last change on this file was dfae77e, checked in by Igor Danilovski <igor_danilovski@…>, 2 years ago
  • Initial commit;
  • Property mode set to 100644
File size: 1.1 KB
Line 
1<?php
2
3namespace App\Http\Middleware;
4
5use App\Providers\RouteServiceProvider;
6use Closure;
7use Illuminate\Contracts\Foundation\Application;
8use Illuminate\Http\JsonResponse;
9use Illuminate\Http\RedirectResponse;
10use Illuminate\Http\Request;
11use Illuminate\Routing\Redirector;
12use Illuminate\Support\Facades\Auth;
13use Illuminate\Support\Facades\Storage;
14
15class UserRole
16{
17 /**
18 * Handle an incoming request.
19 *
20 * @param Request $request
21 * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
22 * @return Application|JsonResponse|RedirectResponse|Redirector
23 */
24 public function handle(Request $request, Closure $next, ...$roles)
25 {
26 if ($request->expectsJson()) {
27 return response()->json(['error' => 'Unauthenticated.'], 401);
28 }
29
30 if (!Auth::check()) {
31 return redirect(RouteServiceProvider::HOME);
32 }
33
34 if (in_array(strtolower(Auth::user()->type), $roles)) {
35 return $next($request);
36 }
37
38 return redirect('/login');
39 }
40}
Note: See TracBrowser for help on using the repository browser.