有这样一个问题。我希望文章是用用户名创建的。但是,当尝试提出请求时,userId总是等于1。
↓↓↓
邮差
{
"id": 3,
"content": "content-5",
"userId": 1
}模型
const User = sequelize.define(
'users',
{
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
username: { type: DataTypes.STRING, unique: true },
email: { type: DataTypes.STRING, unique: true },
password: { type: DataTypes.STRING },
role: { type: DataTypes.STRING, defaultValue: 'USER' },
},
{ timestamps: false }
);
const Post = sequelize.define(
'posts',
{
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
content: { type: DataTypes.STRING, allowNull: false },
},
{ timestamps: false }
);
User.hasMany(Post, { foreignKey: 'userId' });
Post.belongsTo(User, { foreignKey: 'userId' });PostController
async create(req, res, next) {
try {
const { content } = req.body;
const { id: userId } = req.user;
const post = await Post.create({ content, userId })
return res.json(post)
} catch (err) {
console.error(err.message);
res.status(500).send('Server Error');
}
}发布于 2022-05-04 10:21:31
您的问题似乎是您不知道如何处理JWT来检索会话数据
下面是一个基本的示例使用jwt-简单和类型记录
编辑我已经从代码中删除了TS
因此,应该调用以获得一个令牌,例如
curl --request POST \
--url http://localhost:3000/auth \
--header 'Content-Type: application/json' \
--data '{
"usr": "user1",
"pwd": "123"
}'它将到达一个端点,如下所示:
app.post("/auth", async (req, res) => {
const usr = req.body.usr;
const pwd = req.body.pwd;
//this would be a lookup on your database table not a basic if like i have here
if (usr === "user1" && pwd === "123") {
res.send(
encodeSession(secret, {
id: 7,
dateCreated: Date.now(),
username: usr,
})
);
} else res.send("Invalid login").sendStatus(403);
});结果看起来就像
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpZCI6NywiZGF0ZUNyZWF0ZWQiOjE2NTE2NTg3Njk3NTMsInVzZXJuYW1lIjoidXNlcjEiLCJpc3N1ZWQiOjE2NTE2NTg3Njk3NTMsImV4cGlyZXMiOjE2NTE2NTk2Njk3NTN9.JHY4Es9u-aDp1ZzbX-m5iOzbCqWisjqZQTST2nA2_6XUe5NSUbBSGpaXBd_IAlfsLjahJXAbNrxV6N-02E-h6g",
"issued": 1651658769753,
"expires": 1651659669753
}然后在安全端点中调用
function getUserDetails(req){
const [prefix, token] = req.headers.authorization?.split(" ");
if (prefix === "myToken" && token)
return decodeSession(secret, token));
else
throw new Error("Invalid token");
}这会回来的
{
"type": "valid",
"session": {
"id": 7,
"dateCreated": 1651658769753,
"username": "user1",
"issued": 1651658769753,
"expires": 1651659669753
}
}然后,对安全端点的调用如下所示
curl --request GET \
--url http://localhost:3000/checkauth \
--header 'Authorization: myToken eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpZCI6NywiZGF0ZUNyZWF0ZWQiOjE2NTE2NTg3Njk3NTMsInVzZXJuYW1lIjoidXNlcjEiLCJpc3N1ZWQiOjE2NTE2NTg3Njk3NTMsImV4cGlyZXMiOjE2NTE2NTk2Njk3NTN9.JHY4Es9u-aDp1ZzbX-m5iOzbCqWisjqZQTST2nA2_6XUe5NSUbBSGpaXBd_IAlfsLjahJXAbNrxV6N-02E-h6g' \
--header 'Content-Type: application/json'为了完整起见,这里是JWT-简单实现。
import { encode, decode} from "jwt-simple";
export function encodeSession(
secretKey,
dataToEncode
){
// Always use HS512 to sign the token
const algorithm= "HS512";
// Determine when the token should expire
const issued = Date.now();
const fifteenMinutesInMs = 15 * 60 * 1000;
const expires = issued + fifteenMinutesInMs;
const session = {
...dataToEncode,
issued: issued,
expires: expires,
};
return {
token: encode(session, secretKey, algorithm),
issued: issued,
expires: expires,
};
}
export function decodeSession(
secretKey,
tokenString
){
// Always use HS512 to decode the token
const algorithm= "HS512";
let result={};
try {
result = decode(tokenString, secretKey, false, algorithm);
} catch (e) {
// These error strings can be found here:
// https://github.com/hokaccha/node-jwt-simple/blob/c58bfe5e5bb049015fcd55be5fc1b2d5c652dbcd/lib/jwt.js
if (
e.message === "No token supplied" ||
e.message === "Not enough or too many segments"
) {
return {
type: "invalid-token",
};
}
if (
e.message === "Signature verification failed" ||
e.message === "Algorithm not supported"
) {
return {
type: "integrity-error",
};
}
// Handle json parse errors, thrown when the payload is nonsense
if (e.message.indexOf("Unexpected token") === 0) {
return {
type: "invalid-token",
};
}
throw e;
}
return {
type: "valid",
session: result,
};
}https://stackoverflow.com/questions/72097220
复制相似问题