成功请求后令牌未添加到本地存储

Token not being added to local storage after successful request

我在使用 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;  

login.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

如有任何建议,我们将不胜感激。

token(或包含它的对象)在 aurelia-auth

中默认响应中的 prop 'access_token' 的值

因此您需要更改 api 对

的响应的结构
{ access_token: YOUR_TOKEN }

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

此外,要更清楚地了解令牌是如何提取的,请查看 here