Skip to content

Commit

Permalink
Simplify Auth, only add user when already resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Aug 22, 2019
1 parent 96e86c0 commit bf19d93
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 38 deletions.
43 changes: 22 additions & 21 deletions src/DataCollector/MultiAuthCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,27 @@ public function setShowName($showName)
*/
public function collect()
{
$data = [];
$data = [
'guards' => [],
];
$names = '';

foreach($this->guards as $guardName => $config) {
try {
$user = $this->resolveUser($this->auth->guard($guardName), $config);
$guard = $this->auth->guard($guardName);
if ($this->hasUser($guard)) {
$user = $guard->user();

if(!is_null($user)) {
$data['guards'][$guardName] = $this->getUserInformation($user);
$names .= $guardName . ": " . $data['guards'][$guardName]['name'] . ', ';
}
} else {
$data['guards'][$guardName] = null;
}
} catch (\Exception $e) {
continue;
}

$data['guards'][$guardName] = $this->getUserInformation($user);

if(!is_null($user)) {
$names .= $guardName . ": " . $data['guards'][$guardName]['name'] . ', ';
}
}

foreach ($data['guards'] as $key => $var) {
Expand All @@ -78,23 +84,18 @@ public function collect()
return $data;
}

private function resolveUser(Guard $guard, array $config)
private function hasUser(Guard $guard)
{
// if we're logging in using remember token
// then we must resolve user „manually”
// to prevent csrf token regeneration
if ($guard instanceof SessionGuard) {

$recaller = new Recaller($guard->getRequest()->cookies->get($guard->getRecallerName()));
$provider = $this->auth->createUserProvider($config['provider']);
if (method_exists($guard, 'hasUser')) {
return $guard->hasUser();
}

$user = $provider->retrieveByToken($recaller->id(), $recaller->token());
if ($user) {
return $user;
}
// For Laravel 5.5
if (method_exists($guard, 'alreadyAuthenticated')) {
return $guard->alreadyAuthenticated();
}

return $guard->user();
return false;
}

/**
Expand Down
34 changes: 17 additions & 17 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,23 +449,23 @@ function ($query, $bindings = null, $time = null, $connectionName = null) use ($
$this->addCollector(new FilesCollector($app));
}

// if ($this->shouldCollect('auth', false)) {
// try {
// $guards = $this->app['config']->get('auth.guards', []);
// $authCollector = new MultiAuthCollector($app['auth'], $guards);

// $authCollector->setShowName(
// $this->app['config']->get('debugbar.options.auth.show_name')
// );
// $this->addCollector($authCollector);
// } catch (\Exception $e) {
// $this->addThrowable(
// new Exception(
// 'Cannot add AuthCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e
// )
// );
// }
// }
if ($this->shouldCollect('auth', false)) {

This comment has been minimized.

Copy link
@mikemand

mikemand Aug 22, 2019

You have an extra space in front of every line of this block now. 🤣

try {
$guards = $this->app['config']->get('auth.guards', []);
$authCollector = new MultiAuthCollector($app['auth'], $guards);

$authCollector->setShowName(
$this->app['config']->get('debugbar.options.auth.show_name')
);
$this->addCollector($authCollector);
} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add AuthCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e
)
);
}
}

if ($this->shouldCollect('gate', false)) {
try {
Expand Down

0 comments on commit bf19d93

Please sign in to comment.