小编Sau*_*lla的帖子

使用 next-auth 凭据提供程序时如何发送 httponly cookie 客户端?

我正在创建一个 next js 应用程序,使用 next-auth 来处理身份验证。

我有一个外部后端 api,所以我使用 Credentials Provider。

问题是后端发送 httponly cookie,但当我在客户端发出请求时,这些 cookie 没有附加到浏览器。

在 /pages/api/[...auth].js 中

import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';
import clientAxios from '../../../config/configAxios'

export default NextAuth({
    providers: [
        Providers.Credentials({
            async authorize(credentials) {
                try {
                    const login = await clientAxios.post('/api/login', {
                        username: credentials.username,
                        password: credentials.password,
                        is_master: credentials.is_master
                    })


                    const info = login.data.data.user
                    const token = {
                        accessToken: login.data.data.access_token,
                        expiresIn: login.data.data.expires_in,
                        refreshToken: login.data.data.refresh_token
                    }
                    // I can see cookies here
                    const cookies = login.headers['set-cookie']

                    return …
Run Code Online (Sandbox Code Playgroud)

next.js next-auth

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

标签 统计

next-auth ×1

next.js ×1