sergiodxa / remix-auth

Simple Authentication for Remix

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support of Cloudflare Workers

edmundhung opened this issue · comments

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:

  • Usage @remix-run/server-runtime as peerDependencies instead of @remix-run/node
  • #57
  • #58
  • #59

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.

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,
};

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.