这里我在express中的后端生成令牌………….
router.post("/login",async(req,res)=>{
const {email,password}=req.body;
if(!email || !password){
return res.status(401).send({error:"please filled the data properly"});
}
try {
const loginUser=await User.findOne({email:email});
if(!loginUser){
return res.status(400).send({error:"not found"});
}
const isMatch = await bcrypt.compare(password,loginUser.password);
if(isMatch){
const token=await loginUser.generateToken();
res.cookie("jwtoken",token,{
expires:new Date(Date.now()+15000000),
httpOnly:true,
//secure:true //it is applicable when we use https method
})
console.log(token);
res.send({message:"login success"});
}else{
res.status(400).send({error:"please enter correct data"})
}
} catch (error) {
res.status(400).send(error)
}
})
令牌是在我登录brouser 时创建的
这是关于页面(react)………….
const verifyPage=async()=>{
try{
const res=await fetch('/about',{
method:"GET",
headers:{
Accept:"application/json",
"Content-Type":"application/json"
},
credentials:"include"
});
const data=await res.json();
console.log(data);
if(!res.status===200){
const err=new Error(res.error);
throw err;
}
}catch(err) {
console.log(err);
history.push("/login");
}
}
useEffect(()=>{
verifyPage();
},[])
在这里我验证令牌………….
router.get("/about",Authentication,(req,res)=>{
res.send(req.rootUser);
})
身份验证页面…………
const jwt = require("jsonwebtoken")
const User=require("../models/shegma")
const Authentication=async (req,res,next)=>{
try{
const token=req.cookies.jwtoken;
console.log(token)
const verifyToken=jwt.verify(token,process.env.TOKENID);
console.log(verifyToken);
const rootUser=await User.findOne({_id:verifyToken._id,"tokens.token":token})
if(!rootUser){throw new Error("user not found")}
req.token=token;
req.rootUser=rootUser;
req.userID=rootUser._id;
next();
}catch(err){
res.status(401).send("no token found");
console.log(err);
}
}
module.exports=Authentication;
这是错误……
TypeError:无法读取未定义的属性"jwtoken"在身份验证(C:\Users\ASUS\Desktop\mern\server\middleware\Authentication.js:6:33)在Layer.handle[as-handle_request](C:\Users\ASUS\Desktop\mern\server\node_modules\express\lib\router\Layer.js:95:5)在next(C:\Users\ASUS\Desktop\mern\server\node_modules\express\lib\router\route.js:137:13)在Route.dispatch(C:\Users\ASUS\Desktop\mern\server\node_modules\express\lib\router\Route.js:112:3)在Layer.handle[as-handle_request](C:\Users\ASUS\Desktop\mern\server\node_modules\express\lib\router\Layer.js:95:5)位于C:\Users\ASUS\Desktop\mern\server\node_modules\express\lib\router\index.js:281:22位于Function.prrocess_params(C:\Users\ASUS\Desktop\mern\server\node_modules\express\lib\router\index.js:335:12)在next(C:\Users\ASUS\Desktop\mern\server\node_modules\express\lib\router\index.js:275:10)位于Function.handle(C:\Users\ASUS\Desktop\mern\server\node_modules\express\lib\router\index.js:174:3)在路由器(C:\Users\ASUS\Desktop\mern\server\node_modules\express\lib\router\index.js:47:12)
首先,您需要在服务器文件夹中安装cookie解析器
npm i cookie-parser
然后,需要cookie解析器初始化express 的.js文件
const cookieParser = require('cookie-parser')
之后,在const app = express();
下面只写
app.use(cookieParser())
这是完整的代码:-
const express = require('express');
const cookieParser = require('cookie-parser')
const app = express();
app.use(cookieParser())
您是否碰巧在express代码中的任何位置解析传入请求的cookie?
req.cookies
对象是undefined
,这让我相信您可能没有解析cookie请求,或者在调用Authentication
处理程序之前没有进行解析。
供参考:express cookie解析器
这也适用:
const router = express.Router()
const cookieParser = require("cookie-parser")
router.use(cookieParser())