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

Extracted the authentication portion of the authenticated middleware into a navigation guard #11186

Merged
merged 1 commit into from
Jun 10, 2024

Conversation

codyrancher
Copy link
Contributor

@codyrancher codyrancher commented Jun 5, 2024

Summary

Extracted the authentication portion of the authenticated middleware into a navigation guard

Technical notes summary

This is the closest analogue for getting authentication to be executed at the same point in time it was when it was in middleware.

I did verify this works for both local and github authentication providers.

Areas or cases that should be tested

All authenticated area

Areas which could experience regressions

All authenticated area

Checklist

  • The PR is linked to an issue and the linked issue has a Milestone, or no issue is needed
  • The PR has a Milestone
  • The PR template has been filled out
  • The PR has been self reviewed
  • The PR has a reviewer assigned
  • The PR has automated tests or clear instructions for manual tests and the linked issue has appropriate QA labels, or tests are not needed
  • The PR has reviewed with UX and tested in light and dark mode, or there are no UX changes

@codyrancher codyrancher changed the title Extracted the authentication portion of the authenticated middleware … Extracted the authentication portion of the authenticated middleware into a navigation guard Jun 10, 2024
@codyrancher codyrancher added this to the v2.9.0 milestone Jun 10, 2024
Comment on lines +97 to +98
export const routeRequiresAuthentication = (to) => {
return routeMatched(to, (matched) => matched.meta?.requiresAuthentication);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I decided to update this to use metadata instead of names so that we don't have to maintain a separate list of authenticated pages.

The vue3 router docs also have this exact example for authentication: https://v3.router.vuejs.org/guide/advanced/meta.html#route-meta-fields so it should migrate better.

Copy link
Member

Choose a reason for hiding this comment

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

I like this approach, it's straightforward and the idiomatic way to handle authentication in vue router.

@@ -297,9 +297,9 @@ export function notLoggedIn(store, redirect, route) {
store.commit('auth/hasAuth', true);

if ( route.name === 'index' ) {
return redirect(302, '/auth/login');
return redirect('/auth/login');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

navigation guard next() doesn't support passing a status.

@@ -17,52 +15,7 @@ export default async function({
route, store, redirect, from, $plugin, next
}) {
if ( store.getters['auth/enabled'] !== false && !store.getters['auth/loggedIn'] ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is still necessary because we don't always proceed any further in the middleware depending on whether the user was authenticated or not.

Comment on lines +268 to +270
if (from?.name !== to?.name) {
updatePageTitle(getVendor());
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When investigating a flaky test I noticed that the page title was flickering on pages where the query or hash in the url was updated (due to tabs for instance)

Copy link
Member

Choose a reason for hiding this comment

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

Could the flashing behavior also have anything to do with the TabTitle (shell/components/TabTitle.vue) component?

@codyrancher codyrancher marked this pull request as ready for review June 10, 2024 22:12
Copy link
Member

@rak-phillip rak-phillip left a comment

Choose a reason for hiding this comment

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

I think that this looks great! I just have one question about initializing garbage collection from within the authentication guard.

Comment on lines +97 to +98
export const routeRequiresAuthentication = (to) => {
return routeMatched(to, (matched) => matched.meta?.requiresAuthentication);
Copy link
Member

Choose a reason for hiding this comment

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

I like this approach, it's straightforward and the idiomatic way to handle authentication in vue router.

Comment on lines +268 to +270
if (from?.name !== to?.name) {
updatePageTitle(getVendor());
}
Copy link
Member

Choose a reason for hiding this comment

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

Could the flashing behavior also have anything to do with the TabTitle (shell/components/TabTitle.vue) component?

}
}

store.dispatch('gcStartIntervals');
Copy link
Member

Choose a reason for hiding this comment

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

How is gcStartIntervals related to authentication?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's really not. But it's not related to setting product either and I was going to move it into one of the two lol.

Would you like to put it into a separate navigation guard? I think we can preserve order and timing that way

Copy link
Member

Choose a reason for hiding this comment

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

Would you like to put it into a separate navigation guard? I think we can preserve order and timing that way

Perhaps.. I might also be fine with a comment that explains why we need to start garbage collection intervals at the end of the authentication guard.

If we were to separate this into a new guard, what do we think would happen to this gc action that we also dispatch in the authenticated middleware, would these two end up in closer proximity to each other?

// GC should be notified of route change before any find/get request is made that might be used for that page
store.dispatch('gcRouteChanged', route);

Copy link
Member

Choose a reason for hiding this comment

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

@codyrancher and I synced on this out of band. It looks like there's quite a bit of behavior in the authenticated middleware that isn't related to authentication. We will follow up with an issue to address the garbage collection in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created an issue to track per our conversation #11205

@codyrancher
Copy link
Contributor Author

Could the flashing behavior also have anything to do with the TabTitle (shell/components/TabTitle.vue) component?

So it can cause flashing if the content changes but there isn't much we can do about that besides debouncing. This was constantly resetting it back to 'Rancher' as the url was modified even if the route hadn't actually changed.

@codyrancher codyrancher merged commit 8a4cdc6 into rancher:master Jun 10, 2024
35 checks passed
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.

None yet

2 participants