【问题标题】:Angular AuthGuard with NGRX带有 NGRX 的 Angular AuthGuard
【发布时间】:2020-06-20 21:48:58
【问题描述】:

我正在尝试将 AuthGuard 与 NGRX 一起使用,但在这条路线上我总是不确定:

当我查看 Store 时,estaAutenticado 是真的,但我无法准时,因为它是异步的?

我的后卫:

     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;

【问题讨论】:

  • 尝试在地图前添加filter(authed =&gt; typeof authed !== 'undefined')
  • createReducer 代码在哪里?
  • @SWilko 哪一部分?
  • @PrzemyslawJanBeigert 我得到了相同的结果,动作被调度我登录成功,但我仍然收到 undefined 。

标签: angular authentication store ngrx


【解决方案1】:

已解决,我缺少选择器功能

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 2020-04-11
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    相关资源
    最近更新 更多