Skip to content

Commit bf19d93

Browse files
committed
Simplify Auth, only add user when already resolved
1 parent 96e86c0 commit bf19d93

File tree

2 files changed

+39
-38
lines changed

2 files changed

+39
-38
lines changed

src/DataCollector/MultiAuthCollector.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,27 @@ public function setShowName($showName)
5050
*/
5151
public function collect()
5252
{
53-
$data = [];
53+
$data = [
54+
'guards' => [],
55+
];
5456
$names = '';
5557

5658
foreach($this->guards as $guardName => $config) {
5759
try {
58-
$user = $this->resolveUser($this->auth->guard($guardName), $config);
60+
$guard = $this->auth->guard($guardName);
61+
if ($this->hasUser($guard)) {
62+
$user = $guard->user();
63+
64+
if(!is_null($user)) {
65+
$data['guards'][$guardName] = $this->getUserInformation($user);
66+
$names .= $guardName . ": " . $data['guards'][$guardName]['name'] . ', ';
67+
}
68+
} else {
69+
$data['guards'][$guardName] = null;
70+
}
5971
} catch (\Exception $e) {
6072
continue;
6173
}
62-
63-
$data['guards'][$guardName] = $this->getUserInformation($user);
64-
65-
if(!is_null($user)) {
66-
$names .= $guardName . ": " . $data['guards'][$guardName]['name'] . ', ';
67-
}
6874
}
6975

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

81-
private function resolveUser(Guard $guard, array $config)
87+
private function hasUser(Guard $guard)
8288
{
83-
// if we're logging in using remember token
84-
// then we must resolve user „manually”
85-
// to prevent csrf token regeneration
86-
if ($guard instanceof SessionGuard) {
87-
88-
$recaller = new Recaller($guard->getRequest()->cookies->get($guard->getRecallerName()));
89-
$provider = $this->auth->createUserProvider($config['provider']);
89+
if (method_exists($guard, 'hasUser')) {
90+
return $guard->hasUser();
91+
}
9092

91-
$user = $provider->retrieveByToken($recaller->id(), $recaller->token());
92-
if ($user) {
93-
return $user;
94-
}
93+
// For Laravel 5.5
94+
if (method_exists($guard, 'alreadyAuthenticated')) {
95+
return $guard->alreadyAuthenticated();
9596
}
9697

97-
return $guard->user();
98+
return false;
9899
}
99100

100101
/**

src/LaravelDebugbar.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -449,23 +449,23 @@ function ($query, $bindings = null, $time = null, $connectionName = null) use ($
449449
$this->addCollector(new FilesCollector($app));
450450
}
451451

452-
// if ($this->shouldCollect('auth', false)) {
453-
// try {
454-
// $guards = $this->app['config']->get('auth.guards', []);
455-
// $authCollector = new MultiAuthCollector($app['auth'], $guards);
456-
457-
// $authCollector->setShowName(
458-
// $this->app['config']->get('debugbar.options.auth.show_name')
459-
// );
460-
// $this->addCollector($authCollector);
461-
// } catch (\Exception $e) {
462-
// $this->addThrowable(
463-
// new Exception(
464-
// 'Cannot add AuthCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e
465-
// )
466-
// );
467-
// }
468-
// }
452+
if ($this->shouldCollect('auth', false)) {
453+
try {
454+
$guards = $this->app['config']->get('auth.guards', []);
455+
$authCollector = new MultiAuthCollector($app['auth'], $guards);
456+
457+
$authCollector->setShowName(
458+
$this->app['config']->get('debugbar.options.auth.show_name')
459+
);
460+
$this->addCollector($authCollector);
461+
} catch (\Exception $e) {
462+
$this->addThrowable(
463+
new Exception(
464+
'Cannot add AuthCollector to Laravel Debugbar: ' . $e->getMessage(), $e->getCode(), $e
465+
)
466+
);
467+
}
468+
}
469469

470470
if ($this->shouldCollect('gate', false)) {
471471
try {

0 commit comments

Comments
 (0)