Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add guard assignment helper middleware #362

Merged
merged 5 commits into from
Apr 11, 2023

Conversation

evansims
Copy link
Member

@evansims evansims commented Apr 11, 2023

Changes

This PR adds a convenience helper middleware that simplifies the assignment of specific guards for handling request routing.

Example usage:

<?php

use Auth0\Laravel\Http\Controller\Stateful\{Login, Logout, Callback};

Route::middleware('guard:someGuardName')->group(function () {
    Route::get('/login', Login::class)->name('login');
    Route::get('/logout', Logout::class)->name('logout');
    Route::get('/callback', Callback::class)->name('callback');
});

This ensures that the example guard, someGuardName, is used to handle the routing of requests for the specified group of routes.

This streamlines our documentation by ensuring that downstream applications are configured to use the Auth0 Laravel SDK guard, regardless of other installed guards or configurations.

References

N/A

Testing

This PR adds tests to cover the new middleware.

Contributor Checklist

@evansims evansims added the Scope: Improvement Proposed improvements, or feature additions. label Apr 11, 2023
@evansims evansims requested a review from a team as a code owner April 11, 2023 07:31
@codecov
Copy link

codecov bot commented Apr 11, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (844b6c9) 98.97% compared to head (28bc2f8) 98.97%.

Additional details and impacted files
@@            Coverage Diff            @@
##               main     #362   +/-   ##
=========================================
  Coverage     98.97%   98.97%           
  Complexity      289      289           
=========================================
  Files            20       20           
  Lines           783      783           
=========================================
  Hits            775      775           
  Misses            8        8           
Flag Coverage Δ
unittests 98.97% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/Auth/Guard.php 96.58% <100.00%> (ø)
src/ServiceProvider.php 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@evansims evansims added this to the 7.6.0 milestone Apr 11, 2023
README.md Outdated Show resolved Hide resolved
Co-authored-by: Rita Zerrizuela <[email protected]>

$this->get($routeUnspecifiedGuard)
->assertStatus(Response::HTTP_OK)
->assertSee($defaultGuardClass);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this assertion done above? On L43.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies if this is intentional.

Copy link
Member Author

@evansims evansims Apr 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Widcket 👋 No worries! It's repeated a few times to ensure the guard changes are properly persisted between requests. Here's the breakdown of what it's doing:

// Ensure the active guard is the default/unspecified one, 'web' ($defaultGuardClass' value.)
$this->get($routeUnspecifiedGuard)
     ->assertStatus(Response::HTTP_OK)
     ->assertSee($defaultGuardClass);

// Attempt to change the active guard to one that doesn't exist; throws an error.
$this->get($routeNonexistentGuard)
     ->assertStatus(Response::HTTP_INTERNAL_SERVER_ERROR);

// Change the guard to 'testGuard' ($sdkGuardClass' value.)
$this->get($routeMiddlewareAssignedGuard)
     ->assertStatus(Response::HTTP_OK)
     ->assertSee($sdkGuardClass);

// Ensure the active guard was correctly persisted as `testGuard` following the previous request.
$this->get($routeUnspecifiedGuard)
     ->assertStatus(Response::HTTP_OK)
     ->assertSee($sdkGuardClass);

// Clear the active guard we specified, resetting it to the app's default, `web`.
$this->get($routeMiddlewareUnassignedGuard)
     ->assertStatus(Response::HTTP_OK)
     ->assertSee($defaultGuardClass);

// Ensure the active guard was correctly persisted as `web` following the previous request.
$this->get($routeUnspecifiedGuard)
     ->assertStatus(Response::HTTP_OK)
     ->assertSee($defaultGuardClass);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification!

@evansims evansims merged commit 4f7a770 into main Apr 11, 2023
@evansims evansims deleted the feat/add-guard-assignment-helper-middleware branch April 11, 2023 19:44
@evansims evansims mentioned this pull request Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Improvement Proposed improvements, or feature additions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants