nowaythatworked / auth-astro

Community maintained Astro integration of @auth/core
289 stars 43 forks source link

auth-astro causes build to fail when building for cloudflare #43

Open its-jman opened 9 months ago

its-jman commented 9 months ago

Repro: https://github.com/its-jman/auth-astro-repro

When running astro build with auth() included as an integration, the build fails saying The package "path" wasn't found on the file system....

There seems to be a import "path"; injected at the beginning of the build output which causes this error

If you have ideas I'm happy to investigate further, but I haven't found any solutions so far.

astro.config.mjs:

import { defineConfig } from 'astro/config';
import cloudflare from "@astrojs/cloudflare";
import auth from "auth-astro";

export default defineConfig({
  integrations: [auth()],
  output: "server",
  adapter: cloudflare()
});
its-jman commented 9 months ago

I've narrowed it down: injectEndpoints: false no longer errors, which makes sense since that code calls path.dirname and path.join, although that code is not included in the built file.

This is the first few lines of the built file that seems to be causing issues. ./dist/$server_build/chunks/pages/{hash}.mjs:

import 'path';
import { Auth } from '@auth/core';
import { splitCookiesString, parseString } from 'set-cookie-parser';
import { serialize } from 'cookie';
import Google from '@auth/core/providers/google';

const defineConfig = (config) => {
  config.prefix ??= "/api/auth";
  return config;
};

const authConfig = defineConfig({
  trustHost: true,
  secret: "1234",
  providers: [
    Google({
      clientId: "1234",
      clientSecret: "1234"
    })
  ]
});
its-jman commented 9 months ago

Found a resolution by removing the path import in [...auth].ts and replacing it with a package import as recommended here. I did it using my packages name, but I'll create a draft pr to show the diff at least

https://github.com/its-jman/auth-astro