Hey there, I'm using supertokens in my NestJS app. I'm looking to have 2 different auth strategies, ...
g

gp01683

over 2 years ago
Hey there, I'm using supertokens in my NestJS app. I'm looking to have 2 different auth strategies, one for admin users and one for nomal users. I would like to have 2 different routes and 2 different supertokens service to I can customize sign in / signup options on both independently. Here's what I got at the moment: My abstracted identity module, following the supertokens doc:
export class IdentityAndAccessModule implements NestModule, OnModuleInit {
  ...

  static forRoot({ connectionURI, apiKey, appInfo, caslFactory }) {
    const providers: Provider[] = [
     ...
      {
        useValue: {
          appInfo,
          connectionURI,
          apiKey,
        },
        provide: ConfigInjectionToken,
      },
      SupertokensService,
      PrismaService,
    ];

    return {
      module: IdentityAndAccessModule,
      providers: providers,
      exports: providers,
    };
  }

...
}
Then my 2 auth modules, one for normal users and one for admin:
@Module({
  imports: [
    PrismaModule,
    // TODO: config
    IdentityAndAccessModule.forRoot({
      connectionURI: 'http://localhost:3567',
      appInfo: {
        appName: 'WC APP',
        apiDomain: 'http://localhost:3001',
        websiteDomain: 'http://localhost:3000',
        apiBasePath: '/auth',
        websiteBasePath: '/auth',
      },
      caslFactory: AppCaslFactory,
    }),
  ],
  providers: [AuthService, ...oauthProviders],
  exports: [AuthService, IdentityAndAccessModule],
  controllers: [],
})
export class CustomerAuthModule {}
and another one identical but using /admin/auth as base path for both API and website. My problem is that the first module registered in app.module.ts, will be available but not the other one. So /auth/signin works but /admin/auth/signin always return 404. Any idea? I guess it comes from my module being setup as singleton but can't find any way to have multiple instances using its own config
NestJS Cors Error CorsMiddleware/SuperTokensMiddleware
r

romanco8751

over 2 years ago
Hi SuperTokens 🚀, I ran into an issue with CORS when I tried to integrate your ThirdPartyEmailPassword recipe to my application. My Vue.js frontend app is running at "http://localhost:8888" and the NestJS backend api server at "http://localhost:3000". I followed thoroughly your integration guide for NestJS, however after long hours of search, I cannot figure out how to make the CorsMiddleware run before the Supertoken middleware. I get the following CORS error when a request is sent to one of the supertoken core endpoint. Access to fetch at "http://localhost:3000/auth/signup/email/exists?email=johndoe@gmail.com" from origin "http://localhost:8888" has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard ' * ' when the request's credentials mode is 'include'. Here is my main.ts code: async function bootstrap() { const app = await NestFactory.create(AppModule); app.enableCors({ origin: ["http://localhost:8888"], allowedHeaders: ['content-type', ...supertokens.getAllCORSHeaders()], credentials: true, }); app.useGlobalFilters(new SupertokensExceptionFilter()); await app.listen(3000); } bootstrap(); Supertoken config in the **app.module.ts**: @Module({ imports: [ AuthModule.forRoot({ connectionURI: "http://localhost:3567", appInfo: { appName: "IMAT Planner", apiDomain: "http://localhost:3000", websiteDomain: "http://localhost:8888", }, }), ], controllers: [AppController], providers: [AppService, Client], exports: [AppService], }) export class AppModule {} **auth.module.ts**: export class AuthModule implements NestModule { configure(consumer: MiddlewareConsumer) { consumer.apply(AuthMiddleware).forRoutes("*"); } ... } Can you give me an hint on how to make the CORS middleware run before the Supertoken one ?