Skip to content

Injection error with async guard in RouteMeta #1678

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

Open
2 tasks
kedom1337 opened this issue Apr 8, 2025 · 1 comment
Open
2 tasks

Injection error with async guard in RouteMeta #1678

kedom1337 opened this issue Apr 8, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@kedom1337
Copy link

kedom1337 commented Apr 8, 2025

Please provide the environment you discovered this bug in.

{
  "name": "...",
  "version": "0.0.0",
  "type": "module",
  "private": true,
  "engines": {
    "node": ">=18.19.1"
  },
  "scripts": {
  },
  "dependencies": {
    "@analogjs/content": "^1.15.0",
    "@analogjs/router": "^1.15.0",
    "@angular/animations": "^19.2.5",
    "@angular/cdk": "^19.2.8",
    "@angular/common": "^19.2.5",
    "@angular/compiler": "^19.2.5",
    "@angular/core": "^19.2.5",
    "@angular/forms": "^19.2.5",
    "@angular/material": "^19.2.8",
    "@angular/material-moment-adapter": "^19.2.8",
    "@angular/platform-browser": "^19.2.5",
    "@angular/platform-browser-dynamic": "^19.2.5",
    "@angular/platform-server": "^19.2.5",
    "@angular/router": "^19.2.5",
    "rxjs": "~7.8.2",
    "tslib": "^2.8.1"
  },
  "devDependencies": {
    "@analogjs/platform": "^1.15.0",
    "@analogjs/vite-plugin-angular": "^1.15.0",
    "@analogjs/vitest-angular": "^1.15.0",
    "@angular-devkit/build-angular": "^19.2.6",
    "@angular/build": "^19.2.6",
    "@angular/cli": "^19.2.6",
    "@angular/compiler-cli": "^19.2.5",
    "jsdom": "^22.1.0",
    "typescript": "~5.5.4",
    "vite": "^6.2.5",
    "vite-plugin-webfont-dl": "^3.10.4",
    "vite-tsconfig-paths": "^4.3.2",
    "vitest": "^3.1.1"
  },
  "overrides": {
    "@nx/vite": {
      "vite": "$vite",
      "vitest": "$vitest"
    }
  }
}

Which area/package is the issue in?

create-analog

Description

I have the following guard for authentication:

export const loggedInGuard: CanMatchFn = async () => {
  const authStore = inject(AuthStoreService)
  const router = inject(Router)
  const snackbar = inject(MatSnackBar)
  const i18n = inject(TranslateService)
  const authApi = inject(AuthApiService)

  if (!authStore.loggedIn()) {
    try {
      authStore.user.set(await authApi.checkAuthState())
    } catch {
      snackbar.open(i18n.instant('common.SESSION_EXPIRED'))

      return new RedirectCommand(router.parseUrl('/auth/login'), {
        skipLocationChange: true,
      })
    }
  }

  return true
}

which I use as such in my pages:

export const routeMeta: RouteMeta = {
  canMatch: [loggedInGuard],
}

After updating from version 1.14.0 to 1.15.0 I get error NG0203 telling me my injection context is invalid.
I looked up this problem and encountered this issue. In my cause though I do not have any calls to inject after the asynchronous call. I am also sure that it's the async call to authApi.checkAuthState() which causes the problem, because removing this function call resolves the issue.

Please provide the exception or error you saw

main.ts:5 ERROR RuntimeError: NG0203: The `UserQueriesService` token injection failed. `inject()` function must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext`. Find more at https://angular.dev/errors/NG0203

Other information

No response

I would be willing to submit a PR to fix this issue

  • Yes
  • No
@kedom1337 kedom1337 added the bug Something isn't working label Apr 8, 2025
@brandonroberts
Copy link
Member

Did you upgrade Angular also? We haven't changed anything about how that works. A reproduction would be helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants