【问题标题】:How to parse JWT token from request [Next-Auth]如何从请求中解析 JWT 令牌 [Next-Auth]
【发布时间】:2021-10-18 16:06:42
【问题描述】:

如何从即将到来的请求中解析 next-AUTH JWT 会话令牌。

我想从另一台服务器提取 JWT 令牌,以便在那里进行验证。

我正在使用 nextJs 和 next-AUTH

【问题讨论】:

    标签: reactjs api jwt next.js next-auth


    【解决方案1】:

    简短的回答你可以这样解析:

    const getRawJwt = (req) => {
        try {
            if (!req.headers.cookie) {
                console.log("no cookie");
                return null
            } else {
                const rawCookieString = req.headers.cookie // raw cookie string, possibly multiple cookies
                const rawCookiesArr = rawCookieString.split(';')
    
                for (let i = 0; i < rawCookiesArr.length; i++) {
                    let cookieArr = rawCookiesArr[i].trim().split('=')[0]
                    if (cookieArr.includes("next-auth.session-token")) {
                        return rawCookiesArr[i].trim().split('=')[1]
                    }
                }
                return null
            }
        } catch (err) {
            return null
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-06
      • 1970-01-01
      • 2019-01-14
      • 2016-02-28
      • 1970-01-01
      • 2021-12-21
      • 2020-07-22
      • 2017-10-21
      • 2023-03-28
      相关资源
      最近更新 更多