first commit

This commit is contained in:
riccardo
2021-09-05 17:20:59 +02:00
commit 9b4e2b9963
53 changed files with 9015 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;
class Authenticate
{
/**
* The authentication guard factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Auth\Factory $auth
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if ($this->auth->guard($guard)->guest()) {
return response('Unauthorized.', 401);
}
return $next($request);
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\ExpiredException;
use Illuminate\Http\Request;
class JwtMiddleware
{
public function handle(Request $request, Closure $next)
{
$token = $request->header('Authorization');
if(!$token) {
return response()->json([
'error' => 'Token missing.'
], 401);
}
try {
$auth = JWT::decode(explode(" ", $token)[1], env('JWT_SECRET'), ['HS256']);
$request->auth = (array)$auth;
} catch(ExpiredException $e) {
return response()->json([
'error' => 'Token expired.'
], 400);
} catch(Exception $e) {
return response()->json([
'error' => 'Error decoding token.'
], 400);
}
return $next($request);
}
}