如何获取 userId 作为 ClientId?
How to get userId as ClientId?
我正在尝试获取用户 ID,mongoose 将为用户模式创建,作为“/post”中的 clientId api 这样我就可以将用户 ID 作为 clientId 在票证上
User.schema.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),
};
Ticket.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,
},
],
};
如有任何帮助和建议,我们将不胜感激。
authorization.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" });
};
授权时必须指定整个用户。
这就是我解决这个问题的方法。
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,您应该首先解码并将用户指定为请求。因为那是一个中间件,所以每条有这个中间件的路由都会有一个用户。
我正在尝试获取用户 ID,mongoose 将为用户模式创建,作为“/post”中的 clientId api 这样我就可以将用户 ID 作为 clientId 在票证上
User.schema.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),
};
Ticket.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,
},
],
};
如有任何帮助和建议,我们将不胜感激。
authorization.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" });
};
授权时必须指定整个用户。 这就是我解决这个问题的方法。 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,您应该首先解码并将用户指定为请求。因为那是一个中间件,所以每条有这个中间件的路由都会有一个用户。