相关疑难解决方法(0)

将cookie作为node.js请求的一部分传递

我正在使用该request包来创建我的服务器端请求.我编写了身份验证中间件,用于检查所有请求的cookie /会话ID.因此,有没有办法将用户的cookie包含在请求中?这是我目前的代码:

var cookie = parseCookie.parseCookie(req.headers.cookie);

request('http://localhost:3000/users/api', function(error, response, body) {
    console.log(body); //this console.logs my login page since requests w/o valid cookies get redirected to login
    res.render('../views/admin');
});
Run Code Online (Sandbox Code Playgroud)

目前,这会在控制台中返回"找不到cookie".但是,如果我关闭我的身份验证中间件,上面的代码将按预期工作.

附加信息:

我想要的cookie是位于浏览器上的最终用户的cookie.每当用户登录时,最终用户的cookie都由应用程序创建.

更新 - 解决方案尝试1:

我从文档中尝试了这个:

var cookie = parseCookie.parseCookie(req.headers.cookie);
var cookieText = 'sid='+cookie;
var j = request.jar();
        var cookie = request.cookie(cookieText);
        var url = 'http://localhost:3000/users/api';
        j.setCookie(cookie, url);
        request({url: url, jar: j}, function(error, response, body) {
            request('http://localhost:3000/users/api');
        });
Run Code Online (Sandbox Code Playgroud)

但是,控制台仍然返回'找不到cookie'

有人可以帮忙吗?

提前致谢!

javascript cookies request node.js

12
推荐指数
2
解决办法
3万
查看次数

Laravel 的 Next-Auth 凭证提供程序 - 请求失败,状态代码 419

我有一个安装了 Next-Auth 的 NextJS 前端和一个使用 Sanctum 的 Laravel 后端当我尝试使用 Next-Auth 的登录功能登录时,它给了我这个错误:

Request failed with status code 419
Run Code Online (Sandbox Code Playgroud)

419 与 CSRF 令牌有关,但我在调用登录方法之前通过调用 sainttum/csrf-cookie 路由来设置令牌

[...nextauth.js]

Request failed with status code 419
Run Code Online (Sandbox Code Playgroud)

apiClient.js

CredentialsProvider
        ({
            name: 'Email and Password',
            credentials: {
                email: {label: "Email", type: "email", placeholder: "Your Email"},
                password: {label: "Password", type: "Password"}
            },
            async authorize({email, password}, req) {
                await apiClient.get("/sanctum/csrf-cookie");
                const user = await apiClient.post('/customer/login', {
                    email: email,
                    password: password,
                });

                if (user) {
                    return user
                } else {
                    return …
Run Code Online (Sandbox Code Playgroud)

laravel next.js csrf-token laravel-sanctum next-auth

7
推荐指数
2
解决办法
7703
查看次数

Next Auth getSession 在 api 路由中不起作用

所以基本上我使用 getServerSideProps 来调用一些 API。当我在 getServerSideProps() 中调用 getSession 时,我得到一个有效的对象。

export async function getServerSideProps({ req }) {
   const session = await getSession({ req }); // works
Run Code Online (Sandbox Code Playgroud)

但是当我在 getServerSideProps() 函数中调用的 API 中调用它时,我得到 null。

import { getSession } from "next-auth/react";

export default async (req, res) => {
  const { db } = await connectToDatabase();

  const session = await getSession({ req }); // returns null
Run Code Online (Sandbox Code Playgroud)

这是 NextAuth 文档供参考:

在此输入图像描述

api reactjs next.js next-auth

5
推荐指数
1
解决办法
1万
查看次数

如何在 Next.js 中的`getServerSideProps` 方法中使用 cookie?

我必须在端点上发送当前语言。但是从 Cookie 获取语言会返回 undefined inside getServerSideProps

export async function getServerSideProps(context) {
    const lang = await Cookie.get('next-i18next')
    const res = await fetch(`endpoint/${lang}`)
    const data = await res.json()

    return {
        props: { data },
    }
}

export default Index;
Run Code Online (Sandbox Code Playgroud)

将 cookie 放入内部的正确方法是什么getServerSideProps

提前致谢!

i18next next.js

3
推荐指数
4
解决办法
5459
查看次数