我应该如何将令牌发送到用户实体内?
好吧,我正在制作一个“忘记密码”控制器,用户在其中发送电子邮件并在他的邮箱中接收一个令牌,该令牌在两个小时内到期,以便他可以使用它来创建一个新密码,但是我的问题是,避免有了这个用户,他已经拥有多个令牌,因此我打算在我的用户实体中创建布尔列列“ isforgotpasswordtoken”,如果这是真的,这意味着已经为该用户生成了一个令牌。我真正的问题是,我该如何将这个布尔人发送到实体中,因为我该如何在实体内部发送这个令牌,这是一个好习惯呢?还是为此创建实体并以某种方式绑定到用户ID是一种最佳实践?
用户实体
import { BeforeInsert, BeforeUpdate, Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import bcrypt from 'bcryptjs';
@Entity('users')
export class User {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
username: string;
@Column()
email: string;
@Column()
password: string;
@BeforeInsert()
@BeforeUpdate()
hashPassword() {
this.password = bcrypt.hashSync(this.password, 8);
}
}
忘记tasswordcontroller
import { Request, Response } from "express";
import { User } from "../entities/User";
import bcrypt from 'bcryptjs';
import { AppDataSource } from "../database/Config";
import jwt from 'jsonwebtoken';
class forgotPasswordController {
async authenticate(req: Request, res: Response) {
const { email } = req.body;
const secret = process.env.JWT_SEC as string;
try {
const user = await AppDataSource.manager.findOneBy(User, {
email: email
});
if (!user) {
return res.status(401).json('Email not registered!');
};
const token = jwt.sign({ id: user.id }, secret, {
expiresIn: process.env.EXPIRES_LOGIN,
});
if (!token) {
return res.status(401).json('Expired token, please try again.')
}
} catch (err) {
return res.status(500).json(err);
}
}
}
export default new forgotPasswordController();
Well, I'm making a "forgot password" controller, in which the user sends the email and receives in his mailbox a token that expires in two hours so that he can use it to create a new password, but my question is, avoid with that this user generates several tokens while he already has one, so I intend to create a boolean column "isForgotPasswordToken" in my user entity, which if it is true means that a token has already been generated for that user. My real question is, how can I send this boolean inside the entity as false and how can I send this token inside the entity, what would be a good practice for that? Or would it be a best practice to create an entity just for that and somehow bind to the user id?
User entity
import { BeforeInsert, BeforeUpdate, Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import bcrypt from 'bcryptjs';
@Entity('users')
export class User {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
username: string;
@Column()
email: string;
@Column()
password: string;
@BeforeInsert()
@BeforeUpdate()
hashPassword() {
this.password = bcrypt.hashSync(this.password, 8);
}
}
ForgotPasswordController
import { Request, Response } from "express";
import { User } from "../entities/User";
import bcrypt from 'bcryptjs';
import { AppDataSource } from "../database/Config";
import jwt from 'jsonwebtoken';
class forgotPasswordController {
async authenticate(req: Request, res: Response) {
const { email } = req.body;
const secret = process.env.JWT_SEC as string;
try {
const user = await AppDataSource.manager.findOneBy(User, {
email: email
});
if (!user) {
return res.status(401).json('Email not registered!');
};
const token = jwt.sign({ id: user.id }, secret, {
expiresIn: process.env.EXPIRES_LOGIN,
});
if (!token) {
return res.status(401).json('Expired token, please try again.')
}
} catch (err) {
return res.status(500).json(err);
}
}
}
export default new forgotPasswordController();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用您的秘密和用户密码哈希签名JWT令牌。
因此,如果用户使用一个令牌之一更改密码,则用户的所有其他令牌都会无效,因为密码哈希更改。
那是因为您无法用新秘密验证旧的JWT。
我的想法将大致实现:
通过这种方式,用户可以通过有效令牌发送多个电子邮件。
但是,在第一次更改密码之后,所有其他令牌都会无效。
重要的是要注意,当用户更改密码时,密码(和密码障碍)必须与上一张密码不同。
You can sign the jwt token with your secret and the user password hash.
So if a user change the password with one of the tokens, every other token of the user gets invalid because the password hash changes.
Thats because you cant verify the old jwt with the new secret.
My idea would be implemented roughly as:
In this way, the user can be able to send multiple emails with valid tokens.
But after the first time the user change the password all other tokens get invalid.
It is important to note that when the user changes the password, the password (and password-hash) must be different from the previous one.