首页 > 解决方案 > 在 Azure 中使用 Cookie 进行身份验证 - React Web App

问题描述

我正在尝试将我的 React-Web-Application 从普通网络服务器迁移到 Azure Wep-App。该应用程序应该通过电子邮件+密码登录到我的后端。这行得通。后端发送带有身份验证令牌的响应,该令牌曾经保存在 cookie 中。令牌在响应中,但没有保存。它不会在浏览器、前端日志或后端日志上引发任何错误或警告。我不知道该怎么做,我不知道从哪里开始调试。也许我用错了整个 Azure 的东西?

这是后端的代码,它将邮件和密码与数据库进行比较,并在一切正常时发送令牌:

async (
    req: ValidatedRequest<AuthenticateRequest>,
    res: Response<AuthenticateResponse>,
    next: NextFunction
  ): Promise<void> => {
    try {
      const mail = req.validData.mail;
      const password = req.validData.password;
      const user = await User.findByEmail(mail);
      if (user !== null && await user.comparePassword(password)) {
        const userRole = user.association;
        // create the token
        const token = issueToken(userRole, user._id);
        
        res.cookie("cookieToken", token, {httpOnly: false});
        res.send({ user: await transformUser(user), token, });
      }
      else {
        throw new ErrorGeneric(errorCodes.ERROR_ACCOUNT_NOT_FOUND);
      }
    }
    catch (error) {
      next(error);
    }
  }

任何想法或帮助表示赞赏。:)

真诚的

标签: node.jsreactjsazureauthenticationcookies

解决方案


推荐阅读