Skip to content

Commit

Permalink
Merge pull request #9 from P-Sintija/main
Browse files Browse the repository at this point in the history
Replace paths with route names and update readme
  • Loading branch information
miks authored Nov 7, 2023
2 parents bbea1bb + 5051d43 commit ce240ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ or
Add relation to User model manually
```php
use CubeAgency\NovaGoogle2fa\Models\User2fa;

use Illuminate\Database\Eloquent\Relations\HasOne;
...

/**
* @return HasOne
*/
public function user2fa(): HasOne
{
return $this->hasOne(User2fa::class);
return $this->hasOne(User2fa::class, 'user_id');
}
```

Expand Down
17 changes: 12 additions & 5 deletions src/Http/Middleware/Google2fa.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ public function handle($request, Closure $next)

private function userIsAuthenticating($request): bool
{
$novaPath = trim(config('nova.path'), '/') . '/';
return $request->path() === $novaPath . 'google2fa/authenticate' || $request->path() === $novaPath . 'google2fa/register'
|| $request->path() === $novaPath . 'google2fa/recover';
return $this->hasRouteName($request, 'nova.google2fa.authenticate')
|| $this->hasRouteName($request, 'nova.google2fa.register')
|| $this->hasRouteName($request, 'nova.google2fa.recover');
}

private function userHasNotFinishedSetup($request, $user): bool
{
return $request->path() === config('nova.path') . 'google2fa/authenticate' && (bool)$user->user2fa->google2fa_enable === false;
return $this->hasRouteName($request, 'nova.google2fa.authenticate') && (bool)$user->user2fa->google2fa_enable === false;
}

private function userHasFinishedSetup($request, $user): bool
{
return $request->path() === config('nova.path') . 'google2fa/register' && (bool)$user->user2fa->google2fa_enable === true;
return $this->hasRouteName($request, 'nova.google2fa.register') && (bool)$user->user2fa->google2fa_enable === true;
}

private function userIsAuthenticated($request): bool
Expand All @@ -87,4 +87,11 @@ private function twoFactorAuthNotSetup($user): bool
{
return !$user->user2fa || $user->user2fa->google2fa_enable === 0;
}

private function hasRouteName($request, $routeName): bool
{
$currentRouteName = $request->route()->getName();

return $currentRouteName === $routeName || $currentRouteName === $routeName . '.index';
}
}

0 comments on commit ce240ef

Please sign in to comment.