0

我在使用aurelia-auth插件时遇到问题。我可以从后端请求令牌并获得成功的响应 - 但是,我的应用程序似乎没有经过身份验证,并且没有令牌保存到本地存储中。

auth-config.js

var config = {

  baseUrl: 'http://localhost:64794',
  signupUrl: 'users',
  loginUrl: 'api/values/PostPassword',
  tokenName: 'id_token',
  loginRedirect: '#/welcome',
  authHeader: 'Authorization',
  authToken: 'Bearer',
  storage: 'localStorage'
}

export default config;  

登录.js

import {AuthService} from 'aurelia-auth';
import {inject} from 'aurelia-framework';

@inject(AuthService)

export class Login {

  heading = 'Login';

  email = '';
  password = '';

  loginError = '';

  constructor(auth) {
    this.auth = auth;
  };

  login() {
    return this.auth.login(this.email, this.password)
    .then(response => {
      console.log("Login response: " + response);
      console.log("Auth: " + this.auth.isAuthenticated());
    })
    .catch(error => {
      this.loginError = error.response;
    });
  };
}

控制台输出:

Login response: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6ImIiLCJuYmYiOjE1MDkyNzkxMzYsImV4cCI6MTUwOTI4MDMzNiwiaWF0IjoxNTA5Mjc5MTM2fQ.4QZu8pQI-K_71x_CKT9ANu1vQD7VvVUcyep51CvvCXg
login.js:27 Auth: false

任何意见,将不胜感激。

4

1 回答 1

1

在 aurelia-auth 中,默认情况下,令牌(或包含它的对象)应该是响应中 prop 'access_token' 的值

所以你需要改变你的api响应的结构

{ access_token: YOUR_TOKEN }

如果您无法控制服务器,您仍然可以通过覆盖默认配置来提取令牌。这里有一个问题,这里responseTokenProp更多

另外,为了更清楚地了解令牌的提取方式,请查看此处

于 2017-10-29T12:46:38.093 回答