sst / ion

SST v3
https://sst.dev
MIT License
1.88k stars 221 forks source link

Auth not working for cloudflare #484

Closed TheKnightCoder closed 3 months ago

TheKnightCoder commented 4 months ago

I get the following error when trying to deploy sst Auth to cloudflare using "sst": "^3.0.32",

|  Creating    AuthAuthenticator sst:cloudflare:Worker → AuthAuthenticatorScript cloudflare:index:WorkerScript
|  Error       AuthAuthenticator sst:cloudflare:Worker → AuthAuthenticatorScript cloudflare:index:WorkerScript Error: Dynamic require of "node:util" is not supported
|  at worker.mjs:15:9
|  at worker.mjs:8316:23 in ../../../node_modules/.pnpm/openid-client@5.6.4/node_modules/openid-client/lib/issuer.js
|  at worker.mjs:21:51 in __require2
|  at worker.mjs:8648:19 in ../../../node_modules/.pnpm/openid-client@5.6.4/node_modules/openid-client/lib/index.js
|  at worker.mjs:21:51 in __require2
|  at worker.mjs:11361:28
|  (10021)

×  Failed
   AuthAuthenticator sst:cloudflare:Worker → AuthAuthenticatorScript cloudflare:index:WorkerScript
   Error: Dynamic require of "node:util" is not supported
   at worker.mjs:15:9
   at worker.mjs:8316:23 in ../../../node_modules/.pnpm/openid-client@5.6.4/node_modules/openid-client/lib/issuer.js
   at worker.mjs:21:51 in __require2
   at worker.mjs:8648:19 in ../../../node_modules/.pnpm/openid-client@5.6.4/node_modules/openid-client/lib/index.js
   at worker.mjs:21:51 in __require2
   at worker.mjs:11361:28
   (10021)

See src code here

Handler code

import { AuthHandler } from 'sst/auth'
import { sessions } from '../lib/sessions'
import { GoogleAdapter } from 'sst/auth/adapter'

const app = AuthHandler({
  session: sessions,
  providers: {
    google: GoogleAdapter({
      mode: 'oidc',
      clientID: process.env.GOOGLE_CLIENT_ID!,
    }),
  },
  callbacks: {
    auth: {
      async error(error, req) {
        return new Response('ok', {
          status: 302,
          headers: {
            Location: `auth/signin?error=unauthorized`,
          },
        })
      },
      async allowClient(clientID, redirect, req) {
        return true
      },
      async success(ctx, input, req) {
        const HOURS = 60 * 60 * 1000
        return ctx.session({
          type: 'user',
          expiresIn: 30 * 24 * HOURS,
          properties: {
            userId: 'user.data.userId',
            email: 'user.data.email',
          },
        })
      },
    },
  },
})

export default app

sst.config.ts

/// <reference path="./.sst/platform/config.d.ts" />

export default $config({
  app(input) {
    return {
      name: 'bootstrap-solidjs',
      removal: input?.stage === 'production' ? 'retain' : 'remove',
      home: 'cloudflare',
    }
  },
  async run() {
    const auth = new sst.cloudflare.Auth('Auth', {
      authenticator: {
        handler: 'apps/auth/api/session-create.ts',
      },
    })
  },
})
thdxr commented 3 months ago

we haven't released sst.Auth yet and it seems like openid issuer has compatibility issues with cloudflare workers

going to close for now until we officially release