我希望能够使用魔法登录登录 Payload 管理面板。 问题在于有效负载文档对于如何实施 Passport 策略并不是非常具体。此外,有效负载的公开演示在任何地方都没有提到护照。
https://payloadcms.com/docs/authentication/config#strategies
https://github.com/payloadcms/public-demo
我使用
npx create-payload-app
创建了一个新的有效负载应用程序并选择了博客模板。然后我 npm 安装了包 passport
和 passport-magic-login
并编辑了 Users.ts 文件,以便有效负载使用 MagicLoginStrategy 进行身份验证。这就是我目前陷入的状态,因为尝试运行该项目会抛出一堆显然与 webpack 和 polyfills 相关的错误。
用户.ts
import { CollectionConfig } from "payload/types";
import MagicLoginStrategy from "passport-magic-login";
const sendEmail = (a, b, c, d) => {
console.log("sendEmail");
console.log(a, b, c, d);
};
const verify = (a, b) => {
console.log("verify");
console.log(a, b);
};
const Users: CollectionConfig = {
slug: "users",
auth: {
tokenExpiration: 7200,
maxLoginAttempts: 5,
lockTime: 600 * 1000,
strategies: [
{
strategy: new MagicLoginStrategy({
secret: "my-secret",
callbackUrl: `api/auth/magiclink/callback`,
sendMagicLink: async (destination, href, _, req) => {
await sendEmail(destination, href, _, req);
},
verify: async (payload, callback) => {
verify(payload, callback);
},
}),
},
],
},
admin: {
useAsTitle: "email",
},
access: {
read: () => true,
},
fields: [
// Email added by default
{
name: "name",
type: "text",
},
],
};
export default Users;
错误日志
ERROR in ./node_modules/jwa/index.js 5:11-26
Module not found: Error: Can't resolve 'util' in '/Users/mathias/Documents/coding/payload-passport-test2/node_modules/jwa'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'
- install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "util": false }
ERROR in ./node_modules/jws/lib/data-stream.js 3:13-30
Module not found: Error: Can't resolve 'stream' in '/Users/mathias/Documents/coding/payload-passport-test2/node_modules/jws/lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
- install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "stream": false }
ERROR in ./node_modules/jws/lib/data-stream.js 4:11-26
Module not found: Error: Can't resolve 'util' in '/Users/mathias/Documents/coding/payload-passport-test2/node_modules/jws/lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'
- install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "util": false }
ERROR in ./node_modules/jws/lib/sign-stream.js 5:13-30
Module not found: Error: Can't resolve 'stream' in '/Users/mathias/Documents/coding/payload-passport-test2/node_modules/jws/lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
- install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "stream": false }
ERROR in ./node_modules/jws/lib/sign-stream.js 7:11-26
Module not found: Error: Can't resolve 'util' in '/Users/mathias/Documents/coding/payload-passport-test2/node_modules/jws/lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'
- install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "util": false }
ERROR in ./node_modules/jws/lib/verify-stream.js 5:13-30
Module not found: Error: Can't resolve 'stream' in '/Users/mathias/Documents/coding/payload-passport-test2/node_modules/jws/lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
- install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "stream": false }
ERROR in ./node_modules/jws/lib/verify-stream.js 7:11-26
Module not found: Error: Can't resolve 'util' in '/Users/mathias/Documents/coding/payload-passport-test2/node_modules/jws/lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'
- install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "util": false }
webpack compiled with 7 errors
对我来说,为每个依赖项安装 polyfills 很有帮助。例如,
npm install util
。
在我看来,无论是 Webpack 在编译期间,还是 PayloadCMS 本身,都试图在浏览器环境中使用来自
passport-*some-strategy*
的依赖项。