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

Add support of Cloudflare Workers #49

Closed
4 tasks done
edmundhung opened this issue Nov 18, 2021 · 2 comments
Closed
4 tasks done

Add support of Cloudflare Workers #49

edmundhung opened this issue Nov 18, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@edmundhung
Copy link
Contributor

edmundhung commented Nov 18, 2021

As per discussion in the discord channel, we are looking at how to help making remix-auth works with the Cloudflare Workers runtime. Currently there are a few blockers we noticed:

Can we get around it?

While (1) is critical to be fixed, possibly by replacing it with @remix-run/server-runtime. People can get around issues (2) and (3) by polyfilling crypto themselves:

For example, with esbuild, we can use esbuild-plugin-alias to polyfill the crypto package:

const esbuild = require("esbuild");
const alias = require("esbuild-plugin-alias");

esbuild.build({
  // ...
  plugins: [
    alias({
      crypto: require.resolve("./crypto.js"),
    }),
  ],
});
// ./crypto.js
module.exports = {
  // you can either leave it as an empty object or provides a polyfill for some methods from crypto
};

With this setup, depends on the polyfill setup, you should be able to use strategies not relying on crypto.

@roelandmoors
Copy link

Here an example esbuild and crypto:

const esbuild = require("esbuild");
const alias = require("esbuild-plugin-alias");

esbuild
  .build({
    mainFields: ["browser", "module", "main"],
    define: {
      "process.env.NODE_ENV": '"development"',
    },
    logLevel: "info",
    bundle: true,
    outdir: "dist",
    entryPoints: ["./worker"],
    plugins: [
      alias({
        crypto: require.resolve("./crypto.js"),
      }),
    ],
  })
  .catch(() => process.exit(1));
function dec2hex(dec) {
  return dec.toString(16).padStart(2, "0");
}

function randomBytes(len) {
  var arr = new Uint8Array((len || 40) / 2);
  crypto.getRandomValues(arr);
  return Array.from(arr, dec2hex).join("");
}

module.exports = {
  // you can either leave it as an empty object or provides a polyfill for some methods from crypto
  randomBytes,
};

@sergiodxa sergiodxa added the bug Something isn't working label Nov 19, 2021
@sergiodxa sergiodxa self-assigned this Nov 19, 2021
@sergiodxa sergiodxa added enhancement New feature or request and removed bug Something isn't working labels Nov 19, 2021
@sergiodxa
Copy link
Owner

Now that the Remix Auth package doesn't come with strategies the package itself supports CF Workers just fine and it's up to the strategies to support CF Workers or not.

The new remix-auth-email-link strategy based on the KCDStrategy has support for CF Workers.

The BasicStrategy is not published as an individual package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants