在我的堆栈中,我正在使用
redux-token-auth
使用my rails后端验证用户(
devise_token_auth
).在我的应用程序中,我需要允许经过身份验证的用户上载图像,为此,我正在使用
react-fine-uploader
。
我的问题是我没有办法
ract-fine-uploader
以经过身份验证的方式发布图像。一般来说,如何使用redux token auth通过身份验证将数据上载到我的后端。
我发现了
redux-token-auth
将身份验证令牌存储在
localStorage
,所以我可以检索它并验证我的请求。虽然我不喜欢用
localStorage.get("access-token")
(它是由软件包编写的,使用软件包来处理它似乎是公平的)。
还具有
react-fine-uploader
响应对象不包含来自服务器响应的头,因此我不确定如何获取新令牌来存储它们。
以下是我目前获得的代码:
我的
ImagesUploader
:
import React from "react";
import PropTypes from "prop-types";
import FineUploaderTraditional from "fine-uploader-wrappers";
import Gallery from "react-fine-uploader";
import { backend } from "../../libs/itemTools";
import "react-fine-uploader/gallery/gallery.css";
const ImagesUploader = props => {
const uploader = new FineUploaderTraditional({
options: {
multiple: false,
validation: {
itemLimit: 1
},
request: {
endpoint: backend.createImage,
customHeaders: {
"access-token": localStorage.getItem("access-token"),
"token-type": localStorage.getItem("token-type"),
client: localStorage.getItem("client"),
uid: localStorage.getItem("uid")
}
},
session: {
endpoint: backend.loadImages(props.itemId)
},
deleteFile: {
enabled: true,
endpoint: backend.deleteImage(props.itemId)
},
cors: {
expected: true
},
callbacks: {
onComplete: (id, name, response, xhr) => {
if (response.success) {
response.image.id;
//TODO save new access-token
// ####### THIS DOESN'T work, #########
//fine-uploader doesn't include headers in the response object.
localStorage.setItem("access-token", response.headers["access-token"]);
localStorage.setItem("token-type", response.headers("token-type"));
localStorage.setItem("client", response.headers(client));
localStorage.setItem("uid", response.headers(uid));
} else {
// [...]
}
},
onSessionRequestComplete: (...params) => {
console.log(
"onSessionRequestComplete: " + JSON.stringify(params, 0, 2)
);
}
}
}
});
return (
<div id="upload-area">
<Gallery uploader={uploader} />
</div>
);
};
ImagesUploader.propTypes = {
userId: PropTypes.number.isRequired,
itemId: PropTypes.number.isRequired
};
export default ImagesUploader;
我觉得奇怪
redux令牌身份验证
包不考虑经过身份验证的后端调用
fine-uploader
不授予对响应标头的访问权限。。
有没有可能我遗漏了什么?