php-auth-client/src/PdAuth/PdAuthServiceProvider.php

51 lines
1.4 KiB
PHP
Raw Normal View History

2018-01-02 03:15:51 +00:00
<?php
namespace PdAuth;
2018-01-05 07:42:41 +00:00
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
2018-01-02 03:15:51 +00:00
use Illuminate\Support\ServiceProvider;
2018-01-05 07:42:41 +00:00
use PdAuth\Middleware\Authenticate;
2018-01-02 03:15:51 +00:00
class PdAuthServiceProvider extends ServiceProvider
{
2018-01-05 07:42:41 +00:00
/**
* 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 = $request->cookie(Authenticate::CookieName);
if ($token) {
try {
$user = app('pd.auth')->getUserInfo($token);
if ($user) {
return $user;
}
} catch (DecryptException $ex) {
return null;
}
}
return null;
});
}
2018-01-02 03:15:51 +00:00
public function register()
{
$this->app->singleton('pd.auth', function () {
$this->app->configure('pdauth');
2018-01-05 07:42:41 +00:00
return new OAuth(config('pdauth'));
2018-01-02 03:15:51 +00:00
});
}
}