简体   繁体   中英

Angular AuthGuard with NGRX

I am trying to use the AuthGuard with NGRX, but i always get undefined on this route:

When i look at Store, the estaAutenticado is true, but i cant get on time, because its async?

My guard:

     return this.store.pipe(
       select(fromAuth.getEstaAutenticado ),
       map(authed => {
       console.log(authed) // <---- Always return undefined
         if (!authed) {
           this.router.navigate(['/'])
           return false;
         }
         return true;
       })
     );
   }
 ```
// Reducer
    export interface State {
        usuario: Usuario,
        estaAutenticado: boolean,
        erro: string
    }

    export const initialState: State = {
        usuario: null,
        estaAutenticado: false,
        erro: ''
    };

    export const getUsuario = (state: State) => state.usuario;
    export const getEstaAutenticado = (state: State) => state.estaAutenticado;
    export const getErro = (state: State) => state.erro;

解决了,我错过了 selectorFeature

export const selecionaState = createFeatureSelector<State>('autenticacao');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM