postgresql - 比较postgresql中的日期时间
问题描述
对于重置密码,我正在检查令牌是否过期,有效期为一小时。为此,我将“inOneHour”日期时间保存在“resetPasswordExpires”列 (db) 中,并在重置密码请求时与“现在”日期时间进行比较。但它失败了。
时刻.js
export const now = moment().format();
export const inOneHour = moment()
.add(1, 'hours')
.format();
PostgreSQL
table.datetime('resetPasswordExpires', { useTz: false });
auth.js
exports.forgotPassword = (req, res) => {
const { username, email, inOneHour } = req.body;
return knex('users')
.where({ username, email })
.first()
.then(async user => {
...
return knex('users')
.where({ username })
.first()
.update({
resetPasswordToken: token,
resetPasswordExpires: inOneHour,
})
.then(() => {
...
});
};
exports.resetPasswordWithToken = (req, res) => {
const { newPassword, now } = req.body;
const { token } = req.params;
knex('users')
.where({ resetPasswordToken: token })
.andWhere('resetPasswordExpires', '>', now)
.first()
.then(user => {
if (user) {
const id = user.id;
return setPassword(id, newPassword)
.then(() =>
knex('users')
.where({
id,
})
.first()
.update({
resetPasswordToken: null,
resetPasswordExpires: null,
}),
)
.then(() => res.status(200).json())
.catch(err => res.status(409).json(err));
}
return res.status(409).json('Access token is invalid or has expired');
})
.catch(err => res.status(409).json(err));
};
当 moment.format()
// passing datetime to update 'resetPasswordExpires' column
console.log(inOneHour) // 2019-07-19T01:19:16+09:00
// 'resetPasswordExpires' value after updated
console.log(resetPasswordExpires) // 2019-07-18T16:19:16.000Z
// passing datetime to compare
console.log(now) // 2019-07-19T00:20:02+09:00
当 moment.format('MMMM DD YYYY, HH:mm:ss')
console.log(inOneHour) // July 19 2019, 01:16:17
console.log(resetPasswordExpires) // 2019-07-18T16:16:17.000Z
console.log(now) // July 19 2019, 00:17:38
两种不同的日期时间格式以相同的格式保存在数据库中。即使当我使用 Timezone 设置“resetPasswordExpired”日期时间类型并使用 moment.format() 时,它仍然不起作用。我该如何解决这个问题?
当 moment.format() && table.datetime('resetPasswordExpires');
console.log(inOneHour) // July 19 2019, 01:16:17
console.log(resetPasswordExpires) // 2019-07-18T16:16:17.000Z
console.log(now) // July 19 2019, 00:17:38
解决方案
我重新打开我的笔记本电脑。不知何故,这段代码现在正在工作;;但我意识到我实际上不需要在数据库中保存令牌的过期时间,因为 JWT 令牌已经生成了过期时间。
推荐阅读
- git - 如何诊断 git cat-file 声称缺少对象?
- c++ - 带有 std::thread 类的 C++ 堆栈泄漏
- java - 使用 @EmbeddedId 生成无效 SQL 的 CriteriaQuery selectDistinct
- compiler-construction - Given a recursive descent parser, how should I modify it for Syntax Analysis?
- reactjs - 如何将焦点移到reactjs中的错误字段
- c - 使用 memcpy 连接两个 int 数组
- youtube-iframe-api - 在 Iframe 内,Youtube 播放器取消静音功能停止工作
- javascript - 不要隐藏切换的 div
- composer-php - 有没有办法在 Windows 中设置作曲家缓存目录?
- sum - 求和查询