1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Http/Middleware/TokenAuth.php

45 lines
1.0 KiB
PHP
Raw Normal View History

2019-03-26 05:46:08 +01:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
2019-03-26 05:46:08 +01:00
namespace App\Http\Middleware;
2019-06-05 07:33:48 +02:00
use App\Events\User\UserLoggedIn;
2019-03-26 05:46:08 +01:00
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)
{
2019-03-27 05:50:13 +01:00
if( $request->header('X-API-TOKEN') && ($user = CompanyToken::whereRaw("BINARY `token`= ?",[$request->header('X-API-TOKEN')])->first()->user ) )
{
2019-03-27 10:38:28 +01:00
2019-03-26 05:46:08 +01:00
auth()->login($user);
2019-06-05 07:33:48 +02:00
event(new UserLoggedIn($user));
2019-03-26 05:46:08 +01:00
}
else {
2019-04-18 08:11:37 +02:00
return response()->json(json_encode(['message' => 'Invalid token'], JSON_PRETTY_PRINT) ,403);
2019-03-26 05:46:08 +01:00
}
return $next($request);
}
}