1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Http/Middleware/TokenAuth.php
2019-03-27 20:38:28 +11:00

38 lines
820 B
PHP

<?php
namespace App\Http\Middleware;
use App\Models\CompanyToken;
use App\Models\User;
use Closure;
class TokenAuth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if( $request->header('X-API-TOKEN') && ($user = CompanyToken::whereRaw("BINARY `token`= ?",[$request->header('X-API-TOKEN')])->first()->user ) )
{
//$user->with('company');
auth()->login($user);
}
else {
$error['error'] = ['message' => 'Invalid token'];
return response()->json(json_encode($error, JSON_PRETTY_PRINT) ,403);
}
return $next($request);
}
}