middleware icon indicating copy to clipboard operation
middleware copied to clipboard

Auth.js Middleware

Open ivoilic opened this issue 3 years ago • 11 comments

Hi!

This PR adds middleware for a Auth.js Server, it's still a WIP.

There's an issue with Jest and the @auth/core imports that's making it impossible to test currently. @yusukebe if you have suggestions please let me know.

ivoilic avatar Feb 19 '23 21:02 ivoilic

⚠️ No Changeset found

Latest commit: 0b30e088e3cf2ba1a645d424cf6e5cb2f1e950c3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Feb 19 '23 21:02 changeset-bot[bot]

OK, I'll check it later!

yusukebe avatar Feb 19 '23 23:02 yusukebe

Hi @ivoilic

This problem seems to be the Auth.js matter because the minimum project following will fail.

  • https://github.com/yusukebe/jest-authjs
SS

yusukebe avatar Feb 20 '23 15:02 yusukebe

This is not an Auth.js problem, but a config issue, see the solution explained here: https://github.com/nextauthjs/next-auth/issues/6822#issuecomment-1447347641

Here is the actual sub-module being published correctly: https://unpkg.com/browse/@auth/[email protected]/providers/github.js, https://unpkg.com/browse/@auth/[email protected]/package.json

Hope this helps! :pray:

(PS: We would welcome this to be co-maintained from our official repo as @auth/hono if interested. :+1:)

balazsorban44 avatar Feb 28 '23 00:02 balazsorban44

Hi @balazsorban44 !

Thanks for your comment.

This is not an Auth.js problem, but a config issue

I'm sorry I doubted Auth.js!

@ivoilic

Could you check it?

yusukebe avatar Mar 06 '23 11:03 yusukebe

@yusukebe I tried to follow @balazsorban44's (Thanks so much for taking the time to help with this!) example and made changes at the package level. This resolved the current issue but now I'm getting the error: ReferenceError: exports is not defined. I also tried making these changes at the root of the repo and was getting the same error with other imported packages. I'm not familiar enough with resolving these sort of packaging problems to make meaningful progress on this currently.

PS @balazsorban44 Love the idea of this being co-maintained, didn't know if it made more sense to add there or here. It would be great to have more client agnostic options for using auth.js!

ivoilic avatar Mar 11 '23 20:03 ivoilic

ReferenceError: exports is not defined means you have CJS code that is being interpreted as ESM, since the CommonJS globals exports or module are not available in ES modules.

If you are fine with dropping CJS (some will argue but I recommend this), you can check out https://github.com/nextauthjs/next-auth/blob/main/packages/core/package.json

Basically you only need type: "modules" and the exports field in the package.json. The rest is legacy or non-spec properties.

balazsorban44 avatar Mar 12 '23 10:03 balazsorban44

Hi @ivoilic !

I've updated the jest version in the main branch. Can you merge it? After that, to avoid the error, it would be good not to use jest-environment-miniflare. So, set jest.config.js as follows according to @balazsorban44's advice.

module.exports = {
  testMatch: ['**/test/**/*.+(ts|tsx|js)'],
  transform: {
    '^.+\\.(ts|tsx)$': ['ts-jest', { useESM: true }],
  },
  extensionsToTreatAsEsm: ['.ts'],
}

Then we cannot use crypto, so set it to global in the test code.

import crypto from 'crypto'

// ...

globalThis.crypto = crypto

And this may work.

We can't test without a GitHub token, so I would like to know if there is another good way to test that doesn't use third-party tokens.

yusukebe avatar Mar 21 '23 23:03 yusukebe

does any one have a problem with

node:internal/modules/esm/resolve:303
  return new ERR_PACKAGE_PATH_NOT_EXPORTED(
         ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/bruger/Documents/Github/Semester_project_trustworthy/hub_server/node_modules/@auth/core/package.json

Fnux8890 avatar Mar 07 '24 20:03 Fnux8890

Hello everyone, can I take over this branch and start a new PR or is @ivoilic still available to have a look ?

neolectron avatar Oct 12 '24 15:10 neolectron