当前位置: 首页> 汽车> 维修 > 怎么接app推广的单子_成都微商城开发公司_2021年十大热点事件_长春网站seo

怎么接app推广的单子_成都微商城开发公司_2021年十大热点事件_长春网站seo

时间:2025/8/2 21:46:30来源:https://blog.csdn.net/weixin_64684095/article/details/145902474 浏览次数: 0次
怎么接app推广的单子_成都微商城开发公司_2021年十大热点事件_长春网站seo

使用 `User.findOne({ username })` 根据用户名查找用户,如果用户不存在,返回 404 错误。调用 `user.comparePassword(password)` 方法比较用户输入的密码和数据库中存储的加密密码,如果密码不匹配,返回 401 错误。

// 处理用户登录的路由app.post("/login", async (req, res) => {try {const { username, password } = req.body;// 根据用户名查找用户const user = await User.findOne({ username });if (!user) {return res.status(404).json({ message: "User not found" });}// 比较密码const isPasswordValid = await user.comparePassword(password);if (!isPasswordValid) {return res.status(401).json({ message: "Invalid password" });}// 生成 JWTconst token = jwt.sign({ userId: user._id, username: user.username },secretKey,{expiresIn: "1h", // 令牌有效期为 1 小时,可根据需求调整});res.json({ message: "Login successful", token });} catch (error) {console.error("Error during login:", error);res.status(500).json({ error: "Internal Server Error" });}});

关键字:怎么接app推广的单子_成都微商城开发公司_2021年十大热点事件_长春网站seo

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: