This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccount/app/Providers/AuthServiceProvider.php

80 lines
1.9 KiB
PHP
Raw Normal View History

2019-04-24 18:46:41 +00:00
<?php
namespace App\Providers;
use App\Models\AccessToken;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Boot the authentication services for the application.
*
* @return void
*/
public function boot()
{
// Here you may define how you wish users to be authenticated for your Lumen
// application. The callback which receives the incoming request instance
// should return either a User instance or null. You're free to obtain
// the User instance via an API token or any other method necessary.
$this->app['auth']->viaRequest('api', function (Request $request) {
$token = null;
if(isset($_SESSION["token"])) {
$token = $_SESSION["token"];
}
if(isset($_GET["access_token"])) {
$token = $_GET["access_token"];
}
if(isset($_GET["token"])) {
$token = $_GET["token"];
}
if($request->header("Authorization", false)) {
$token = trim($request->header("Authorization"));
$t = explode(" ", $token);
$token = last($t);
}
if($token == null) {
return null;
}
$accessToken = AccessToken::query()->where("token", "=", $token)->first();
2019-04-25 15:33:15 +00:00
if($accessToken == null) {
return null;
}
2019-04-24 18:46:41 +00:00
if(time() > strtotime($accessToken->expires_at)) {
return null;
}
if($accessToken->status =! "active") {
return null;
}
return $accessToken->getUser();
});
}
}