您好,我在将 Deno 服务器部署到 Deno Deploy 时遇到问题。 我想这与 Bcrypt 库有关,但我不知道如何用另一个库或部署设置来解决这个问题?
我使用 Oak 作为我的中间件:
import { Application } from "https://deno.land/x/[email protected]/mod.ts";
import router from "./controllers/routes/routes.ts";
const app = new Application();
const PORT = 8080;
app.use(router.routes());
app.use(router.allowedMethods());
console.log(`Application is listening on port: ${PORT}`);
await app.listen({ port: PORT });
例如此代码具有登录功能。 API 工作正常,但每当涉及到 Bcrypt 时,它都会给出错误“ReferenceWorker - Worker 未定义” 这是我使用 Bcrypt 库的示例:
import db from "./database/connectDB.ts";
import * as bcrypt from "https://deno.land/x/[email protected]/mod.ts";
import { pbkdf2 } from "https://deno.land/x/[email protected]/src/auth/pbkdf2.ts";
import { UserSchema } from "./schema/user.ts";
import { create, verify } from "https://deno.land/x/[email protected]/mod.ts";
import { key } from "./utils/apiKey.ts";
import { passwordTest, validateEmail } from "./utils/filters.ts";
const users = db.collection<UserSchema>("users");
export const signin = async ({ req, res }: { req: any; res: any }) => {
const body = await req.body();
const { username, password } = await body.value;
if (!username || !password) {
res.status = 400;
res.body = {
message: `Please provide all required fields: username and password.`,
};
return;
}
const user = await users.findOne({ username });
if (!user) {
res.status = 404;
res.body = {
message: `Invalid credentials`,
};
return;
}
const passwordMatch = await bcrypt.compare(password, user.password);
if (!passwordMatch) {
res.status = 401;
res.body = {
message: "Invalid credentials",
};
return;
}
const payload = {
id: user._id,
username: user.username,
};
const jwt = await create({ alg: "HS512", typ: "JWT" }, { payload }, key);
if (jwt) {
res.status = 200;
res.body = {
message: "User logged in",
data: {
_id: user._id,
token: jwt,
username: user.username,
},
};
} else {
res.status = 500;
res.body = {
message: "Internal server error",
};
}
};
Deno Deploy 不支持 Web Workers API。查看 bcrypt 的 documentation 有不使用 Web Workers 的同步方法。
用
bcrypt.compare
替换 bcrypt.compareSync
应该可以:
- const passwordMatch = await bcrypt.compare(password, user.password);
+ const passwordMatch = bcrypt.compareSync(password, user.password);