I have a guard that checks if there is a token on state.
canActivate(): boolean {
const token = this.store.selectSnapshot((state: AuthenticationState) => state.token);
if (!token) {
return true;
}
this.router.navigate(['home']);
return false;
}
Then I have something like this:
export class AuthenticationState {
@Selector()
static token(state: AuthenticationStateModel) {
return state.token;
}
}
I get an error. Property 'token' does not exist on type 'AuthenticationState'
NGXS. Store. The store is a global state manager that dispatches actions your state containers listen to and provides a way to select data slices out from the global state.
Yes, if you subscribe manually in the component then you need to unsubscribe. Best to avoid that if possible and subscribe in the component template using the async pipe.
The mistake you are making here is that you are assuming that the state parameter of the lambda would be your AuthenticationState
it would in fact be the entire application state, which is a parent of the AuthenticationState
. You should rather pass your selector like this:
canActivate(): boolean {
const token = this.store.selectSnapshot(AuthenticationState.token);
if (!token) {
return true;
}
this.router.navigate(['home']);
return false;
}
There was actually a post by the author of NGXS a few days ago on this exact topic: https://medium.com/@amcdnl/authentication-in-ngxs-6f25c52fd385
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With