抱歉这个简单的问题,我一直在寻找“如何使用它们”而不是“何时使用它们”的例子
基本上我已经完成了创建refreshToken
和销毁它的代码
// Compare token in the database and create a new access token
Player.prototype.validateRefreshToken = function(username, refreshToken) {
return new Promise(async (resolve, reject) => {
try {
let player = await this.col.findOneAsync({ username, refreshToken});
if (player) {
let token = jwt.sign(
{
id: player._id,
username: player.username,
email: player.email,
roles: player.role || "user"
},
globals.jwtSecret,
{
expiresIn: "300"
}
);
return resolve(token);
} else {
return resolve(null);
}
} catch(err) {
console.log("[ERROR]: There was an error trying to validateRefreshToken");
console.log(err);
return reject(err);
}
});
}
// Destroy users refreshToken by generating a new one and not delivering
// it to the client
Player.prototype.rejectToken = function (refreshToken) {
return new Promise(async (resolve, reject) => {
try {
let player = await this.col.findOneAndUpdateAsync(
{ refreshToken },
{ $set: { refreshToken: randtoken.uid(256) },
});
if (player) {
return resolve(true);
} else {
return resolve(false);
}
} catch(err) {
console.log("[ERROR]: There was an error trying to rejectToken");
console.log(err);
return reject(err);
}
});
}
// API Routes
// Check Refresh Token
router.post("/token", async (ctx, next) => {
let username = _.get(ctx.request.body, "username");
let refreshToken = _.get(ctx.request.body, "refreshToken");
if (refreshToken) {
try {
let token = ctx.models.player.validateRefreshToken(username, refreshToken);
if (token) {
ctx.body = { success: true, token };
} else {
ctx.body = { success: false, errors: ["You need to reauthenticate yourself their was an issue getting your refresh token"] };
}
} catch(err) {
console.log(err);
ctx.body = { success: false, errors: ["Internal Server Error"] };
}
} else {
ctx.body = { success: false, errors: ["You are not authenticated"] };
}
});
// Destroy refresh token
router.post("/token/reject", async (ctx, next) => {
let refreshToken = _.get(ctx.request.body, "refreshToken");
if (refreshToken) {
try {
let result = await ctx.models.player.rejectToken(refreshToken);
if (result) {
ctx.body = { success: true };
} else {
ctx.body = { success: false, errors: ["You are not authenticated"] };
}
} catch(err) {
console.log(err);
ctx.body = { success: false, errors: ["Internal Server Error"] };
}
}
});
但我的问题是我不知道我应该什么post
时候/token
例如,有人post
向他们自己的个人资料发出请求,/profile/me
但得到一个权限错误,现在是什么,我该如何自动化/token
如果它不要求我澄清,我希望这是有道理的