Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Conversation

joebailey26
Copy link

@joebailey26 joebailey26 commented Jan 2, 2024

πŸ”— Linked issue

Resolves #137
Resolves #138

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

NuxtAuthHandler accepts options and runtimeConfig in order to build a response as part of the /api/auth/[...] endpoint.

Some adapters, such as D1Adapter, need access to the event in order to connect to the database.

The current solution of passing options as an object does not expose the event.

This PR allows passing options as an async function, which takes event as its argument. That way, users of this package, can extend it, and add any adapter, or other logic that they want.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

joebailey26 added a commit to joebailey26/TV-Shows that referenced this pull request Jan 2, 2024
Copy link
Owner

@Hebilicious Hebilicious left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@sultan-xh
Copy link

I have tried this code:

import Credentials from "@auth/core/providers/credentials"
import type { AuthConfig } from "@auth/core/types"
import { D1Adapter } from "@auth/d1-adapter"
import { NuxtAuthHandler } from "#auth"

const runtimeConfig = useRuntimeConfig()

export async function authOptions(event) {
  const authOptions: AuthConfig = {
    secret: runtimeConfig.authJs.secret,
    providers: [
      Credentials({
        credentials: {
          username: { label: "Username" },
          password: { label: "Password", type: "password" },
        },
        authorize: async (credentials) => {
          const user = { id: "1", name: credentials.username, email: "[email protected]", role: "admin" }
          if (user) {
            return user
          } else {
            return null
          }
        },
      })
    ],
    adapter: D1Adapter(event.context.cloudflare.env.D1)
  }
  return authOptions
}

export default NuxtAuthHandler(authOptions, runtimeConfig)

And I am getting this error:


 ERROR  [auth][error][MissingSecret]: Please define a secret.. Read more at https://errors.authjs.dev#missingsecret


 ERROR      at assertConfig (file:///E:/Projects/Nuxt/test/auth/node_modules/@auth/core/lib/assert.js:46:16)
    at Auth (file:///E:/Projects/Nuxt/test/auth/node_modules/@auth/core/index.js:70:29)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at Object.handler (E:\Projects\Nuxt\test\auth\node_modules\@hebilicious\authjs-nuxt\dist\runtime\lib\server.mjs:23:22)
    at async file:///E:/Projects/Nuxt/test/auth/node_modules/h3/dist/index.mjs:1975:19
    at async Object.callAsync (file:///E:/Projects/Nuxt/test/auth/node_modules/unctx/dist/index.mjs:72:16)
    at async toNodeHandle (file:///E:/Projects/Nuxt/test/auth/node_modules/h3/dist/index.mjs:2266:7)
    at async ufetch (file:///E:/Projects/Nuxt/test/auth/node_modules/unenv/runtime/fetch/index.mjs:9:17)
    at async $fetchRaw2 (file:///E:/Projects/Nuxt/test/auth/node_modules/ofetch/dist/shared/ofetch.37386b05.mjs:222:26)
    at async $fetch2 (file:///E:/Projects/Nuxt/test/auth/node_modules/ofetch/dist/shared/ofetch.37386b05.mjs:268:15)


 ERROR  [auth][error][MissingSecret]: Please define a secret.. Read more at https://errors.authjs.dev#missingsecret


 ERROR      at assertConfig (file:///E:/Projects/Nuxt/test/auth/node_modules/@auth/core/lib/assert.js:46:16)
    at Auth (file:///E:/Projects/Nuxt/test/auth/node_modules/@auth/core/index.js:70:29)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at Object.handler (E:\Projects\Nuxt\test\auth\node_modules\@hebilicious\authjs-nuxt\dist\runtime\lib\server.mjs:23:22)
    at async file:///E:/Projects/Nuxt/test/auth/node_modules/h3/dist/index.mjs:1975:19
    at async Object.callAsync (file:///E:/Projects/Nuxt/test/auth/node_modules/unctx/dist/index.mjs:72:16)
    at async toNodeHandle (file:///E:/Projects/Nuxt/test/auth/node_modules/h3/dist/index.mjs:2266:7)
    at async ufetch (file:///E:/Projects/Nuxt/test/auth/node_modules/unenv/runtime/fetch/index.mjs:9:17)
    at async $fetchRaw2 (file:///E:/Projects/Nuxt/test/auth/node_modules/ofetch/dist/shared/ofetch.37386b05.mjs:222:26)
    at async $fetchRaw2 (file:///E:/Projects/Nuxt/test/auth/node_modules/ofetch/dist/shared/ofetch.37386b05.mjs:263:14)

@sultan-xh
Copy link

I think your doc is not up to date with the latest changes. The Nuxt module still has old code. Can you please verify?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants