Now the Guard itself.... I just don't know how to create an instance in the method... It complains on httpClient, then router.... so I've found a solution relying on injection
import { CanActivateFn } from "@angular/router";
import { AuthenticationService } from "../authentication/authentication.service";
import { AppInjector } from "../../app.module";
export const accessGuard: CanActivateFn = (route, state) => {
return AppInjector.get(AuthenticationService).isSessionValid()
};
Here would be a tricky one.
Open your guard directory in a terminal window, and enter the following:
ng generate guard Access
In the app.module.ts file, in the bottom, paste the code:
Right after all imports and before @Ngmodule paste the piece:
export let AppInjector: Injector;
you would also need to import Injector
import { Injector, NgModule } from "@angular/core";
I leave my complete authentication.service.ts,
in login.component.ts insert in constructor
the login function in the same file has setSeesion method you need to place
Now the Guard itself.... I just don't know how to create an instance in the method... It complains on httpClient, then router.... so I've found a solution relying on injection