hey, I am using a NestJS backend (with Fastify ada...
# support-questions
m
hey, I am using a NestJS backend (with Fastify adapter). I've followed the full guide here - https://supertokens.com/docs/session/nestjs/guide (with a self hosted supertokens core). I have the setup mostly working but running into CORS issues. (api listening on
http://localhost:3001
and ui running on
http://localhost:3002
)
Copy code
app.enableCors({
    origin: ['http://localhost:3002'],
    allowedHeaders: ['content-type', ...supertokens.getAllCORSHeaders()],
    credentials: true,
  });
It looks like the CORS policies are not being applied correctly to the endpoints that supertokens middleware adds, ex. /auth/session/refresh CORS is applied correctly to the rest of my NestJs endpoints and my react UI at localhost:3002 can query all my regular endpoints but any requests to supertoken middleware endpoints fail with the error below (even though the preflight to /auth/session/refresh seems to be successful 204
Copy code
Access to fetch at 'http://localhost:3001/auth/session/refresh' from origin 'http://localhost:3002' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
r
hey!
You need to make sure that your CORS middleware is running before the supertokens middleware
m
hey thanks for the reply. This is my bootstrap function currently
Copy code
async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
app.enableCors({
    origin: ['http://localhost:3002'],
    allowedHeaders: ['content-type', ...supertokens.getAllCORSHeaders()],
    credentials: true,
  });
  app.useGlobalFilters(new SupertokensExceptionFilter());
  await app.listen(process.env.API_LISTEN_PORT);
}
bootstrap();
r
and where does the supertokens middleware get added?
m
I believe it would get added in the AppModule, apologies this is the first time I am using supertokens, and I've followed this guide pretty much exactly - https://supertokens.com/docs/emailpassword/nestjs/guide#5-update-cors-settings
The
AppModule
is the entrypoint for my NestFactory.create per the guide and all the supertoken related stuff gets setup like so (within the AppModule)
Copy code
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';

@Module({
  imports: [
    AuthModule.forRoot({
      connectionURI: process.env.SUPERTOKENS_CORE_URI,
      apiKey: process.env.SUPERTOKENS_API_KEY,
      appInfo: {
        appName: process.env.SUPERTOKENS_APPNAME,
        apiDomain: process.env.SUPERTOKENS_APIDOMAIN,
        websiteDomain: process.env.SUPERTOKENS_WEBDOMAIN,
        apiBasePath: process.env.SUPERTOKENS_APIBASE_PATH,
        websiteBasePath: process.env.SUPERTOKENS_WEBBASE_PATH,
      },
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
I think the only difference in my app compared to the official supertoken guide is that I am using the FastifyAdapter versus the express adapter in the guide
r
@porcellus can you help out please?
p
hi
Yeah, that could be the difference. I think the normal
enableCors
thing doesn't work for fastify
m
ah I see
I might try defining a custom middleware and use consumer.apply
p
I could find a few issues like this: https://github.com/nestjs/nest/issues/3939
m
can I define a custom CORS middleware myself before the supertoken middleware
Copy code
configure(consumer: MiddlewareConsumer) {
    consumer.apply(AuthMiddleware).forRoutes('*');
  }
would that work?
just before the consumer.apply(AuthMiddleware)
p
hmm, I think that should work, but there should be a generic solution
m
I've also tried
Copy code
app.register(fastifyCors, {
    origin: process.env.CORS_ORIGIN,
    allowedHeaders: ['content-type', ...supertokens.getAllCORSHeaders()],
    credentials: true,
  });
from the
import fastifyCors from '@fastify/cors';
which also does not seem to work
same effect, CORS is applied to all my regular endpoints
but not for supertoken middleware endpoints
I also found a similar issue here which seems to suggest CORS is a bit problematic with the fastify adapter https://github.com/nestjs/nest/issues/9510
p
huh, sorry, I missed that cors is working correctly for your other endpoints. I'll check this out in a few mins
m
nws. I am trying out a custom middleware in the mean time. Will let you know how I go
thanks again for both your help and responses btw
highly appreciate it
hmm, no luck. I've put a pin in this since I am spending way too much time on it. For the moment, I've switched out fastify with express and CORS seems to work now.
I will revisit fastify once I have everything setup
8 Views