简体   繁体   English

Ember-simple-auth和ember-fetch在获取查询中不授权

[英]Ember-simple-auth and ember-fetch no authorization in fetch query

I am using ember with ember-simple-auth(1.7.0) for authentication. 我正在将ember与ember-simple-auth(1.7.0)一起使用进行身份验证。 Here is the application adapter function : 这是应用程序适配器功能:

authorize(xhr) {
    let { email, token } = this.get('session.data.authenticated');
    let authData = `Token token="${token}", email="${email}"`;
    xhr.setRequestHeader('Authorization', authData);
  }

When I use ember-fetch(5.1.3) there is no header for authentication : 当我使用ember-fetch(5.1.3)时,没有用于身份验证的标头:

fetch('/appname/v1/user/count'+count_params).then((response)=>{return response.json()})

The same model do successful emberDS query with the auth info in the header. 同一模型使用标题中的auth信息成功执行emberDS查询。 How can I add the information to the fetch headers ? 如何将信息添加到提取头中?

EDIT : 编辑:

This is the service I created to wrap fetch : 这是我创建的用于包装fetch的服务:

import Service from '@ember/service';
import fetch from 'fetch';
import { inject as service} from "@ember/service"

export default Service.extend({
  fetch(url){
    let { email, token } = this.get('session.data.authenticated')
    let authData = `Token token="${token}", email="${email}"`
    return fetch(url,{headers: {'Authorization': authData}}).then(
      (response)=>{return response.json()}
    )
  },
  session: service()
});

You need to create fetch wrapper service and use it, instead of "raw" fetch and boilerplating. 您需要创建并使用获取包装服务,而不是“原始”获取和样板服务。

Raw usage possible with headers - https://github.com/github/fetch#post-json 标头可以原始使用-https: //github.com/github/fetch#post-json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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