Skip to content

[2.x] Expose attemptWhen method#80

Merged
DarkGhostHunter merged 1 commit intoLaragear:2.xfrom
tklie:feat/attempt-when
May 22, 2024
Merged

[2.x] Expose attemptWhen method#80
DarkGhostHunter merged 1 commit intoLaragear:2.xfrom
tklie:feat/attempt-when

Conversation

@tklie
Copy link
Copy Markdown
Contributor

@tklie tklie commented May 8, 2024

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).

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented May 8, 2024

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
Copy link
Copy Markdown

codecov bot commented May 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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
@@             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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@DarkGhostHunter DarkGhostHunter merged commit 4c21f63 into Laragear:2.x May 22, 2024
@tklie tklie deleted the feat/attempt-when branch May 23, 2024 05:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants