update controllers logic
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use PHPOpenSourceSaver\JWTAuth\Exceptions\JWTException;
|
||||
use PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Session (web) auth first (CodeIgniter parity), then Sanctum / JWT like {@see MultiAuth}.
|
||||
*/
|
||||
class TeacherPortalAuthenticate
|
||||
{
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (Auth::guard('web')->check()) {
|
||||
Auth::setUser(Auth::guard('web')->user());
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
$sanctumUser = Auth::guard('sanctum')->user();
|
||||
if ($sanctumUser) {
|
||||
Auth::setUser($sanctumUser);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
$token = $request->bearerToken();
|
||||
if ($token !== null && $token !== '') {
|
||||
try {
|
||||
$jwtUser = JWTAuth::setToken($token)->authenticate();
|
||||
if ($jwtUser) {
|
||||
Auth::setUser($jwtUser);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
} catch (JWTException $e) {
|
||||
// fall through
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'Unauthorized.'], 401);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user