首页 > 解决方案 > Passport.authenticate 抛出未经授权的错误 iwith cookie 方法

问题描述

Passport.authenticate 使用 cookie 方法抛出未经授权的错误。哪里可能有问题,在护照 js 设置或前端?有趣的是,在另一个开发人员的编译器上它工作正常

节点JS

router.post('/check',passport.authenticate("cookie", { session: false }), async (req, res, next) => {
  try {
    return res.status(200).json({ status: true })
  } catch (error) {
    res.status(400).json(error);
  }
});

前端

export async function isAuthorized(context, commands) {
let token = getCookie('token')
if (token) {
    let settings = {
        "method": 'POST',
        headers: {
            "Content-Type": "application/json",
        },

    }

    try {
        let response = await fetch('/api/auth/check', settings) //here unathorized
        let result = await response.json()
        return result.status
    } catch (error) {
        throw Error
    }
} else {
    return false
}

}

cookie 已成功发送,也许我需要为护照或类似的东西使用一些额外的设置

标签: javascriptnode.jsexpresspassport.js

解决方案


推荐阅读