我正在嘗試獲取用戶 ID,該 mongoose 將為用戶架構創建,作為“/post”api 中的 clientId,以便我可以將用戶 ID 作為票證上的 clientId
用戶架構.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {
type: String,
maxlength: 50,
required: true
},
company: {
type: String,
maxlength: 50,
required: true
},
address: {
type: String,
maxlength: 100,
},
phone: {
type: Number,
maxlength: 11,
},
email: {
type: String,
maxlength: 50,
required: true,
},
password: {
type: String,
minLength: 8,
maxlength: 100,
required: true
},
refreshJWT: {
token: {
type: String,
maxLength: 500,
default: "",
},
addedAt: {
type: Date,
required: true,
default: Date.now(),
},
},
isVerified: {
type: Boolean,
required: true,
default: false
},
});
module.exports = {
UserSchema: mongoose.model("User", UserSchema),
};
票證.schema.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const TicketSchema = new Schema({
clientId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
subject: {
type: String,
maxLength: 100,
required: true,
default: ""
},
openAt: {
type: Date,
required: true,
default: Date.now(),
},
status: {
type: String,
maxLength: 30,
required: true,
default: "Pending operator response",
},
conversations: [
{
sender: {
type: String,
maxLength: 50,
required: true,
default: "",
},
message: {
type: String,
maxLength: 1000,
required: true,
default: "",
},
msgAt: {
type: Date,
required: true,
default: Date.now(),
},
},
],
});
module.exports = {
TicketSchema: mongoose.model("Ticket", TicketSchema),
};
Ticket.js - 這是我用來創建帶有客戶端 ID 和其他詳細資訊的票證的路由器。
router.post(
"/",
createNewTicketValidation,
userAuthorization,
async (req, res) => {
try {
const { subject, sender, message } = req.body;
const userId = req.User._id;
const ticketObj = {
clientId: userId,
subject,
conversations: [
{
sender,
message,
},
],
};
const result = await newTicket(ticketObj);
if (result._id) {
return res.json({
status: "success",
message: "New ticket has been created!",
});
}
res.json({
status: "error",
message: "Unable to create the ticket , please try again later",
});
} catch (error) {
res.json({ status: "error", message: error.message });
}
}
);
基本上,我特別堅持這個階段的代碼,因為我無法弄清楚如何在上述路由器中獲取用戶 ID 作為客戶端 ID。
const userId = req.User._id;
const ticketObj = {
clientId: userId,
subject,
conversations: [
{
sender,
message,
},
],
};
任何幫助和建議將不勝感激。
授權.js
const userAuthorization = async(req,res,next) => {
const { authorization } = req.headers;
const decoded = await verifyAccessJWT(authorization);
if (decoded.email){
const userId = await getJWT(authorization);
if (!userId) {
return res.status(403).json({message: "Forbidden"});
}
return next();
}
deleteJWT(authorization);
return res.status(403).json({ message: "forbidden" });
};
uj5u.com熱心網友回復:
您必須在授權時分配整個用戶。這就是我解決這個問題的方法。 Auth.js (中間件)
module.exports = function (req, res, next) {
// Getting token from the header.
const token = req.header("x-auth-token");
// Checking if there is a token.
if (!token) {
return res.status(401).json({ msg: "No token, authorization denied." });
}
try {
const decoded = jwt.verify(token, process.env.REACT_APP_JWT_SECRET);
req.user = decoded.user;
next();
} catch (error) {
res.status(401).json({msg: "Token is not valid."})
}
};
如果您使用 JWT,您應該首先解碼,然后將用戶分配為請求。因為這是一個中間件,所以每條有這個中間件的路由都會有一個用戶。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/356437.html
標籤:javascript 表达 默恩
上一篇:排序陣列中缺少整數
