我正在使用护照js进行身份验证,并且护照没有将用户对象放入req对象(req.user返回未定义)。
登录路线:-
Router.post("/login",ensurelog,async (req, res, next) => {
await passport.authenticate("local", { failWithError: true }, function(err,user,info
) {
if (err) {
console.log("1", err);
}
else if (info)
{
res.json({info}).end();
}
else if (user) {
res.json({user}).end();
}
})(req, res, next);
});
react客户端收到用户对象时,使用this.props.history.push()函数将页面重定向到其他组件,该组件是使用react-router-dom中的BrowserRouter路由的
surelog功能:-
function(req,res,next){
//console.log("reached ensurelog")
console.log(req.user) //always returns undefined
console.log(req.isAuthenticated()) //always returns false
if(req.isAuthenticated()){
res.json({
message:'already loged in'
}).end();
}
next();
}
答案 0 :(得分:1)
我只是快速浏览了API documentation。看起来您的import React, { Component } from 'react';
import { connect } from 'react-redux';
class TaskDetail extends Component {
constructor(props) {
super(props);
this.state = {
files: [],
file: ''
}
this.loadFiles = this.loadFiles.bind(this);
}
componentDidMount() {
this.loadFiles();
}
loadFiles() {
fetch('/api/files')
.then(res => res.json())
.then(files => {
if (files.message) {
console.log('No Files');
this.setState({ files: [] })
} else {
this.setState({ files })
}
});
}
fileChanged(event) {
const f = event.target.files[0];
this.setState({
file: f
});
}
deleteFile(event) {
event.preventDefault();
const id = event.target.id;
fetch('/api/files/'+id, {
method: 'DELETE'
}).then(res => res.json())
.then(response => {
console.log(response);
if (response.success) this.loadFiles()
else alert('Delete Failed');
})
}
uploadFile(event) {
event.preventDefault();
let data = new FormData();
data.append('file', this.state.file);
fetch('/api/files', {
method: 'POST',
body: data
}).then(res => res.json())
.then(data => {
if (data.success) {
this.loadFiles();
} else {
alert('Upload failed');
}
});
}
render() {
const { files } = this.state;
return(
<div className="card p-3 col-6">
<input type="file" onChange={this.fileChanged.bind(this)}/>
<button onClick={this.uploadFile.bind(this)}>Upload</button>
<table className="App-table">
<thead>
<tr>
<th>File</th>
<th>Uploaded</th>
<th>Size</th>
<th></th>
</tr>
</thead>
<tbody>
{files.map((file, index) => {
var d = new Date(file.uploadDate);
return (
<tr key={index}>
<td><a href={`http://localhost:8080/api/files/${file.filename}`}>{file.filename}</a></td>
<td>{`${d.toLocaleDateString()} ${d.toLocaleTimeString()}`}</td>
<td>{(Math.round(file.length/100) / 10)+'KB'}</td>
<td><button onClick={this.deleteFile.bind(this)} id={file._id}>Remove</button></td>
</tr>
)
})}
</tbody>
</table>
</div>
)
}
}
function mapStateToProps(state,ownProps){
let id = ownProps.match.params.id;
let task = state.tasks.find(task=>task.id === id);
let isOwner = state.session.id === task.owner;
let groups = state.groups;
return {
id,
task,
isOwner,
sessionID: state.session.id,
groups
}
}
function mapDispatchToProps(dispatch, ownProps){
let id = ownProps.match.params.id;
return {
setTaskCompletion(id,isComplete){
dispatch(setTaskCompletion(id,isComplete));
},
setTaskGroup(e){
dispatch(setTaskGroup(id,e.target.value));
},
setTaskName(e){
dispatch(setTaskName(id,e.target.value));
}
}
}
export const ConnectedTaskDetail = connect(mapStateToProps,mapDispatchToProps)(TaskDetail);
参数有些混乱。你可以尝试一下吗?
post
还值得注意的是,该API似乎正在使用回调函数(而非Promises)来处理异步请求。