mestu
mestu

Reputation: 126

In auth gaurd angular 17 always showing toastr error message

Problem solved
Issue for SSR (server site rendering). In angular 17 have an option server site rendering while creating project. Server site create error and showing it in HTML but don't remove it. To solve I updated the angular.json file

...
"server": "src/main.server.ts",
"prerender": true,
"ssr": {
"entry": "server.ts"
}
...

to

...
"server": "src/main.ts"
...

Problem
I'm using angular 17 and using toastr to show informational messages also using auth guard.

export const authGuard: CanActivateFn = (route, state) => {

  const accountService = inject(AccountService);
  const toastr = inject(ToastrService);

  return accountService.currentUser$.pipe(
    map(user => {
      if(user){
        console.log("Calling me valid user",user);
        return true;
      }
      else{
        console.log("Calling me invalid user",user);
        toastr.error('you shall not pass!');
        return false;
      }
    })
  )
};

routing:

{
    path:'',
    runGuardsAndResolvers : 'always',
    canActivate : [authGuard],
    children : [
      {path:'members',component:MemberListComponent},
      {path:'members/:id',component:MemberDetailComponent},
      {path:'lists',component:ListsComponent},
      {path:'messages',component:MessagesComponent},
    ]
  },

after logging when i reload page , in my vscode terminal showing console.log("Calling me invalid user",user); output

and browser console showing console.log("Calling me valid user",user); output

and again in browser showing toastr error message toastr.error('you shall not pass!');

Upvotes: 1

Views: 347

Answers (0)

Related Questions