由于安全性和可用性问题,Buffer() 已弃用。请改用 Buffer.alloc()、Buffer.allocUnsafe() 或 Buffer.from() 方法
2020-10-25
2683
我正在学习使用 expressjs 的 typeorm。在这里,我尝试实现
login
用户功能,如果用户存在,它将发送访问令牌。我已经实现了注册功能,但在添加登录时,控制台中出现错误。我不知道这是什么意思。
(node:8536) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(node:8536) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'isValidPassword' of undefined
at Object.<anonymous> (C:\Users\adity\Desktop\dev\restapi\src\controllers\AuthController.ts:43:15)
at step (C:\Users\adity\Desktop\dev\restapi\src\controllers\AuthController.ts:32:23)
at Object.throw (C:\Users\adity\Desktop\dev\restapi\src\controllers\AuthController.ts:13:53)
at rejected (C:\Users\adity\Desktop\dev\restapi\src\controllers\AuthController.ts:5:65)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:8536) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8536) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
这是我的代码。
AuthController.ts
static login = async (req: Request, res: Response) => {
const { email, password } = req.body;
if (!(email && password)) {
res.status(400).send();
}
const userRepository = getRepository(User);
let user: User;
try {
user = await userRepository.findOneOrFail({ where: email });
} catch (error) {
res.status(401).send(error);
}
if (!user.isValidPassword(password)) {
res.status(401).send("Incorrect Password");
return;
}
const token = jwt.sign({ id: user.id, email: user.email }, "secret", {
expiresIn: "1h",
});
res.send(token);
};
Entity/User.ts
import * as bcrypt from "bcryptjs";
@Entity()
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
email: string;
@Column()
@Length(4, 100)
password: string;
@Column()
@CreateDateColumn()
createdAt: Date;
@Column()
@UpdateDateColumn()
updatedAt: Date;
isValidPassword = (password: string) => {
return bcrypt.compareSync(password, this.password);
};
setPassword = (password: string) => {
return (this.password = bcrypt.hashSync(password, 8));
};
}
2个回答
此 NodeJS 错误通常发生在检测到代码中某处未处理错误时的异步函数。
如果
getRepositry(user)
是异步函数,您可以尝试附加一个 catch 块,如下所示:
const userRepository = await getRepository(User).catch(err=>console.log(err));
这应该可以解决未处理的承诺拒绝错误。
nishkaush
2020-10-25
发生这种情况的原因是,如果
findOneOrFail
无法找到任何用户,它将通过您捕获的错误来处理,但会导致用户值等于未定义。
然后方法
isValidPassword
将不起作用,因为它不是在
undefined
上定义的,而是在
user
模型上定义的
if (!user.isValidPassword(password)) {
res.status(401).send("Incorrect Password");
return;
}
您应该将上面的代码移到 try 块中,或者您可以像这样在前面放置一个类型保护
if (user && !user.isValidPassword(password)) {
res.status(401).send("Incorrect Password");
return;
}
paradox
2020-10-25