qwik: [๐Ÿž] Cannot use environment variables in Auth plugin on Vercel Edge Functions

Which component is affected?

Qwik City (routing)

Describe the bug

env.get() is not able to read enviroment variables set in Vercelโ€™s UI. I only tested this in serverAuth$, the qwik-auth helper method. Not sure what the scope is exactly.

I also tested process.env while I was at it and it prints nothing in Vercel, but loads of stuff in my local machine (although that might be intended by Vercel and/or QwikCity).

Reproduction

https://github.com/samijaber/qwik-env-var-vercel

Steps to reproduce

  • npm create qwik@latest
  • pnpm install
  • pnpm qwik add auth
  • pnpm qwik add vercel-edge
  • add if (!env.get("AUTH_SECRET")) throw new Error("Missing AUTH_SECRET"); to plugin@auth.ts
import { serverAuth$ } from "@builder.io/qwik-auth";
import GitHub from "@auth/core/providers/github";
import type { Provider } from "@auth/core/providers";

export const { onRequest, useAuthSession, useAuthSignin, useAuthSignout } =
  serverAuth$(({ env }) => {
+    if (!env.get("AUTH_SECRET")) throw new Error("Missing AUTH_SECRET");
    return {
      secret: env.get("AUTH_SECRET"),
      trustHost: true,
      providers: [
        GitHub({
          clientId: env.get("GITHUB_ID")!,
          clientSecret: env.get("GITHUB_SECRET")!,
        }),
      ] as Provider[],
    };
  });

env.get() is returning undefined

System Info

System:
    OS: macOS 12.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 4.16 GB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 18.13.0 - ~/.asdf/installs/nodejs/18.13.0/bin/node
    Yarn: 1.22.19 - ~/.asdf/installs/nodejs/18.13.0/bin/yarn
    npm: 8.19.3 - ~/.asdf/plugins/nodejs/shims/npm
    Watchman: 2022.02.28.00 - /opt/homebrew/bin/watchman
  Browsers:
    Brave Browser: 112.1.50.121
    Chrome: 112.0.5615.137
    Firefox: 109.0.1
    Safari: 15.2
  npmPackages:
    @builder.io/qwik: 0.105.0 => 0.105.0 
    @builder.io/qwik-auth: ~0.1.0 => 0.1.0 
    @builder.io/qwik-city: ~0.104.0 => 0.104.0 
    undici: 5.22.0 => 5.22.0 
    vite: 4.3.3 => 4.3.3

Additional Information

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 25 (14 by maintainers)

Most upvoted comments

I find vercel env variables super buggy, might need to take a look, i beat itโ€™s a vercel bug

@stepaniukm we will soon release a changelog ๐Ÿ˜ƒ

Having the ability to use env vars like that process.env['<env_name>'] is good enough to close this issue. I will create a PR to add this information in the Qwik docs.

I spent an entire evening pulling my hair on this issue but with vercel postgres. @vercel/postgres uses process.env.POSTGRES_URL (which is undefined in production). This worked for me (explicitly setting the connection string instead of relying on implicit behavior):

import { drizzle } from "drizzle-orm/vercel-postgres";
import { createPool } from "@vercel/postgres";
import * as schema from "./schema";

const pool = createPool({
  connectionString: process.env["POSTGRES_URL"],  // Use bracket syntax, not dot notation! 
});

export const db = drizzle(pool, { schema, });

Yeah, I donโ€™t know what he did on the Vercel site, but now the behavior is actually predictable, as when logging process.env I consistently get all needed env variables.

Just letting you know, cause I yesterday revisited the problem and suddenly code worked on a Vercel with me changing nothing at all, only thing Iโ€™ve done was redeploying my code.

I have the same experience, their env system is buggy, not sure why it works sometimes and sometimes it doesnt, without changing anything