Laragear / TwoFactor

Two-Factor Authentication for all your users out-of-the-box.
https://github.com/sponsors/DarkGhostHunter
MIT License
271 stars 20 forks source link

[2.x] Expose attemptWhen method #80

Closed tklie closed 5 months ago

tklie commented 6 months ago

Description

This feature branch adds and exposes the attemptWhen method to/on the TwoFactorLoginHelper and Auth2FA. This allows projects that need to pass their own callbacks for pre-login checks to integrate with this package.

This will not break backward compatibility since it merely adds a new method.

Code samples

This PR simply adds a new method matching the signature of the SessionGuard's method:

    public function attemptWhen(array $credentials = [], $callbacks = null, $remember = false): bool
    {
        // Code same as old `attempt` method.
        // Only change: merge $callbacks and the TwoFactor callback
        // ...

        return $guard->attemptWhen(
            $credentials, array_merge(Arr::wrap($callbacks), [TwoFactor::hasCodeOrFails($this->input, $this->message)]), $remember
        );

        // ...
    }

And changes the current attempt method to call this new method passing null as $callbacks:

    public function attempt(array $credentials = [], $remember = false): bool
    {
        return $this->attemptWhen($credentials, null, $remember);
    }

This approach seems cleaner than simply duplicating the code (which is actually what the SessionGuard does).

sonarcloud[bot] commented 6 months ago

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 100.00%. Comparing base (48ef0b9) to head (e502f57). Report is 1 commits behind head on 2.x.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## 2.x #80 +/- ## =========================================== Coverage 100.00% 100.00% - Complexity 157 158 +1 =========================================== Files 20 20 Lines 444 446 +2 =========================================== + Hits 444 446 +2 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.