Skip to content

getCurrentUser loading infinitely when not always calling it in global middleware and having node_modules/.cache/vite cleared #1625

Open
@dword-design

Description

@dword-design

Reproduction

https://github.com/dword-design/demo-vuefire-getcurrentuser-infinitely

Steps to reproduce the bug

I have a global middleware and I'm conditionally checking auth state. When clearing the cache via nuxi cleanup and early exiting the middleware but calling getCurrentUser another navigation time, it will just load infinitely and not return. Actually it only happens when deleting the node_modules/.cache/vite dir.

When calling getCurrentUser always at the beginning however, it works as expected.

When rerunning nuxt dev it's working as expected.

Additional observation: Is only the case for a global middleware. When setting one via definePageMeta it's working as expected.

EDIT: It looks like global middlewares run in a different context than assigned middlewares. The initialUserMap variable is different in getCurrentUser vs. onIdTokenChanged, which is why the promise is never resolved.

// middleware/auth.global.js => ✅ Works

export default defineNuxtRouteMiddleware(async to => {
  console.log('start');
  await getCurrentUser();
  console.log('end');
  if (to.fullPath === '/') {
    return;
  }
});
// middleware/auth.global.js => ❌ Doesn't work

export default defineNuxtRouteMiddleware(async to => {
  if (to.fullPath === '/') {
    return;
  }
  console.log('start');
  await getCurrentUser();
  console.log('end');
});
  1. rm -rf node_modules/.cache/vite && pnpm dev
  2. Click on the login link. It will load infinitely and not navigate

Expected behavior

getCurrentUser returns the user without hanging.

Actual behavior

getCurrentUser not returning.

Additional information

Probably related to #1624.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions