Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using auth.js as a provider in auth #707

Closed
shreya-khandal opened this issue Mar 8, 2024 · 2 comments
Closed

Error when using auth.js as a provider in auth #707

shreya-khandal opened this issue Mar 8, 2024 · 2 comments
Labels
bug A bug that needs to be resolved closed-by-bot Closed by automation needs-reproduction Action needed: A valid reproduction of the issue needs to be added

Comments

@shreya-khandal
Copy link

Environment

npx nuxi info

Reproduction

I have used authjs as a provider for protecting pages and authentication. This is the auth " auth: {
isEnabled: true,
provider: {
type: 'authjs'
},
globalAppMiddleware: {
isEnabled: true
}
}," snippet I have written in nuxt.config.ts. And on the server side I have made api in api/auth/[...].ts which handles auth as follows:
import { NuxtAuthHandler } from '#auth'
import CredentialsProvider from "next-auth/providers/credentials";
import { NitroFetchOptions } from "nitropack";
import { Messages } from "../../../util/Messages";
import { StatusCode } from "../../../util/Constants";
const config = useRuntimeConfig();

export default NuxtAuthHandler({
secret: 'jvA2sahdchdhhvsahdsdsdcddc' ?? 'test-123',
pages: {
// Change the default behavior to use /login as the path for the sign-in page
signIn: '/login'
},
providers: [
// ... your provider configuration
CredentialsProvider.default({
name: "Credentials",
credentials: {
username: { label: 'Username', type: 'text', placeholder: '(hint: tom)' },
password: { label: 'Password', type: 'password', placeholder: '(hint: asdf1234)' }
},
async authorize(credentials: { username: string, password: string }) {
let serverTokens = (config.serverTokens).split(',');
let serverToken = serverTokens[Math.floor(Math.random() * serverTokens.length)];

                const response = await $fetch<any>(config.apiUrl + "/users/validate",
                    {
                        headers: { "Authorization": "Bearer " + serverToken },
                        method: "POST",
                        body: JSON.stringify({
                            username: credentials.username,
                            password: credentials.password
                        }),
                    } as NitroFetchOptions<any>)

                if (response) {
                    return { name: response.uniqueId };
                } else {
                    return Messages.InvalidCredentials
                }

        }
    })
],

}). I am having an error on server side "[nuxt] [request error] [unhandled] [500] Cannot set headers after they are sent to the client
at __node_internal_captureLargerStackTrace (node:internal/errors:496:5)
at new NodeError (node:internal/errors:405:5)
at ServerResponse.setHeader (node:_http_outgoing:648:11)
at setResponseHeaders (./node_modules/h3/dist/index.mjs:778:20)
at Object.handler (./node_modules/nitropack/dist/runtime/renderer.mjs:31:7)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.handler (./node_modules/h3/dist/index.mjs:1962:19)
at async Server.toNodeHandle (./node_modules/h3/dist/index.mjs:2249:7)"
when authentication process is going. These are the sequence of urls which are calling on the server side "url URL {
href: 'https://localhost:3000/',
origin: 'https://localhost:3000',
protocol: 'https:',
username: '',
password: '',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/',
search: '',
searchParams: URLSearchParams {},
hash: ''
}
url URL {
href: 'https://localhost:3000/api/auth/session?callbackUrl=http:%2F%2Flocalhost:3000%2F',
origin: 'https://localhost:3000',
protocol: 'https:',
username: '',
password: '',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/api/auth/session',
search: '?callbackUrl=http:%2F%2Flocalhost:3000%2F',
searchParams: URLSearchParams { 'callbackUrl' => 'http://localhost:3000/' },
hash: ''
}
url URL {
href: 'https://localhost:3000/api/auth/providers',
origin: 'https://localhost:3000',
protocol: 'https:',
username: '',
password: '',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/api/auth/providers',
search: '',
searchParams: URLSearchParams {},
hash: ''
}
[nuxt] [request error] [unhandled] [500] Cannot set headers after they are sent to the client
at __node_internal_captureLargerStackTrace (node:internal/errors:496:5)
at new NodeError (node:internal/errors:405:5)
at ServerResponse.setHeader (node:_http_outgoing:648:11)
at setResponseHeaders (./node_modules/h3/dist/index.mjs:778:20)
at Object.handler (./node_modules/nitropack/dist/runtime/renderer.mjs:31:7)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.handler (./node_modules/h3/dist/index.mjs:1962:19)
at async Server.toNodeHandle (./node_modules/h3/dist/index.mjs:2249:7)
url URL {
href: 'https://localhost:3000/login',
origin: 'https://localhost:3000',
protocol: 'https:',
username: '',
password: '',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/login',
search: '',
searchParams: URLSearchParams {},
hash: ''
}
url URL {
href: 'https://localhost:3000/api/auth/session?callbackUrl=http:%2F%2Flocalhost:3000%2Flogin',
origin: 'https://localhost:3000',
protocol: 'https:',
username: '',
password: '',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/api/auth/session',
search: '?callbackUrl=http:%2F%2Flocalhost:3000%2Flogin',
searchParams: URLSearchParams { 'callbackUrl' => 'http://localhost:3000/login' },
hash: ''
}
"

Describe the bug

I am using authjs as a provider in auth in nuxt.config.ts and during the initial authentication steps sequence of api calls done "url URL {
href: 'https://localhost:3000/',
origin: 'https://localhost:3000',
protocol: 'https:',
username: '',
password: '',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/',
search: '',
searchParams: URLSearchParams {},
hash: ''
}
url URL {
href: 'https://localhost:3000/api/auth/session?callbackUrl=http:%2F%2Flocalhost:3000%2F',
origin: 'https://localhost:3000',
protocol: 'https:',
username: '',
password: '',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/api/auth/session',
search: '?callbackUrl=http:%2F%2Flocalhost:3000%2F',
searchParams: URLSearchParams { 'callbackUrl' => 'http://localhost:3000/' },
hash: ''
}
url URL {
href: 'https://localhost:3000/api/auth/providers',
origin: 'https://localhost:3000',
protocol: 'https:',
username: '',
password: '',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/api/auth/providers',
search: '',
searchParams: URLSearchParams {},
hash: ''
}
[nuxt] [request error] [unhandled] [500] Cannot set headers after they are sent to the client
at __node_internal_captureLargerStackTrace (node:internal/errors:496:5)
at new NodeError (node:internal/errors:405:5)
at ServerResponse.setHeader (node:_http_outgoing:648:11)
at setResponseHeaders (./node_modules/h3/dist/index.mjs:778:20)
at Object.handler (./node_modules/nitropack/dist/runtime/renderer.mjs:31:7)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.handler (./node_modules/h3/dist/index.mjs:1962:19)
at async Server.toNodeHandle (./node_modules/h3/dist/index.mjs:2249:7)
url URL {
href: 'https://localhost:3000/login',
origin: 'https://localhost:3000',
protocol: 'https:',
username: '',
password: '',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/login',
search: '',
searchParams: URLSearchParams {},
hash: ''
}
url URL {
href: 'https://localhost:3000/api/auth/session?callbackUrl=http:%2F%2Flocalhost:3000%2Flogin',
origin: 'https://localhost:3000',
protocol: 'https:',
username: '',
password: '',
host: 'localhost:3000',
hostname: 'localhost',
port: '3000',
pathname: '/api/auth/session',
search: '?callbackUrl=http:%2F%2Flocalhost:3000%2Flogin',
searchParams: URLSearchParams { 'callbackUrl' => 'http://localhost:3000/login' },
hash: ''
}
" during these calls, I am having an error which you can see. Can you suggest me how to resolve it.

Additional context

I am using nuxt3 for nuxt-auth integration.

Logs

No response

@shreya-khandal shreya-khandal added bug A bug that needs to be resolved pending An issue waiting for triage labels Mar 8, 2024
@shreya-khandal shreya-khandal changed the title Error when auth.js as a provider Error when using auth.js as a provider in auth Mar 8, 2024
@zoey-kaiser zoey-kaiser added the needs-reproduction Action needed: A valid reproduction of the issue needs to be added label Mar 8, 2024
Copy link

github-actions bot commented Mar 8, 2024

Hello 👋

Please provide a reproduction for this issue 🙏

How can I create a reproduction?

Please use one of the following links to reproduce your issue.

Please ensure that the reproduction is as minimal as possible. This will allow us to isolate the issue as best as possible.

Here are some more amazing posts about the importance of reproductions:

Copy link

This issue was closed because it was open for 14 days without a reproduction.

@github-actions github-actions bot added the closed-by-bot Closed by automation label Mar 23, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 23, 2024
@zoey-kaiser zoey-kaiser removed the pending An issue waiting for triage label Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug that needs to be resolved closed-by-bot Closed by automation needs-reproduction Action needed: A valid reproduction of the issue needs to be added
Projects
None yet
Development

No branches or pull requests

2 participants