神经授权问题(始终成功登录)-ngx-admin

时间:2018-10-17 11:19:14

标签: angular authentication oauth ngx-admin nebular

`NbAuthModule.forRoot({       策略:[         NbPasswordAuthStrategy.setup({           名称:“ auth”,

      token: {
        class: NbAuthJWTToken,

        key: 'token',
      },

      baseEndpoint: '/api/',
       login: {
        alwaysFail: false,
         endpoint: 'Login',
         method: 'get',
         redirect: {
          success: '/pages',
          failure: '/register',
          },
       },
       register: {
         endpoint: 'register',
         method: 'post',
       },
    }),
  ],
  forms: {
    login: {
      redirectDelay: 0, // delay before redirect after a successful login, while success message is shown to the user
      strategy: 'auth',  // strategy id key.
      rememberMe: false,   // whether to show or not the `rememberMe` checkbox
      showMessages: {     // show/not show success/error messages
        success: false,
        error: true,
      }, // social links at the bottom of a page
    },
  },
}),

我已设置此密码策略来调用我的Asp.net Web Api项目 我还按如下所示设置了代理服务器

{ “ / api”:{ “ target”:“ http://localhost:54575”, “安全”:错误 } }

预期的行为: 我期望它调用我的api并返回auth令牌并将URL更改为仪表板 但其网址没有改变。

2 个答案:

答案 0 :(得分:0)

Setup your NbPasswordAuthStrategy in your app.module:
  ...NbAuthModule.forRoot({
    strategies: [
      NbPasswordAuthStrategy.setup({
        name: 'email',
        baseEndpoint: 'auth/',
        login: {
          endpoint: 'sign-in',
        },
        register: {
          endpoint: 'sign-up',
        },
        logout: {
          endpoint: 'sign-out',
        },
        token: {
          class: NbAuthJWTToken,
          key: 'token',
        },
      }),
    ],
    forms: {
      login: {
        socialLinks: socialLinks,
      },
      register: {
        socialLinks: socialLinks,
      },
    },
  }).providers,

以及您的登录组件:

constructor(protected service: NbAuthService) {
}
login(): void {
  this.errors = [];
  this.messages = [];
  this.submitted = true;

  this.service.authenticate(this.strategy, this.user).subscribe((result: NbAuthResult) => {
    this.submitted = false;

    if (result.isSuccess()) {
      this.messages = result.getMessages();
    } else {
      this.errors = result.getErrors();
    }

    const redirect = result.getRedirect();
    if (redirect) {
      setTimeout(() => {
        return this.router.navigateByUrl('dashboard');
      }, this.redirectDelay);
    }
    this.cd.detectChanges();
  });

}

完整的登录组件:https://github.com/akveo/nebular/tree/master/src/framework/auth/components/login

在导航中,您可以设置成功登录后要去的地方。当前,它是主页。

答案 1 :(得分:0)

对于失败的登录,您需要返回http错误400。不仅安全:错误。