Hi,
I'm new to SuperTokens and try to implement SuperTokens in NestJS. But NestJS showed error like below
ERROR [ExceptionHandler] Nest can't resolve dependencies of the AuthGuard (?). Please make sure that the argument Object at index [0] is available in the AppModule context.
Below is my auth.guard.ts codes
Copy code
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Error as STError } from 'supertokens-node';
import { verifySession } from 'supertokens-node/recipe/session/framework/express';
import { VerifySessionOptions } from 'supertokens-node/recipe/session';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private readonly verifyOptions?: VerifySessionOptions) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const ctx = context.switchToHttp();
let err = undefined;
const resp = ctx.getResponse();
// You can create an optional version of this by passing {sessionRequired: false} to verifySession
await verifySession(this.verifyOptions)(ctx.getRequest(), resp, (res) => {
err = res;
});
if (resp.headersSent) {
throw new STError({
message: 'RESPONSE_SENT',
type: 'RESPONSE_SENT'
});
}
if (err) {
throw err;
}
return true;
}
}
Did I missed something?
Thanks
r
rp_st
10/01/2022, 2:09 PM
Hey! Did you follow our guide for nestjs?
t
tommykhumarga3422
10/01/2022, 2:33 PM
Yes, I followed your guide
r
rp_st
10/01/2022, 2:53 PM
Maybe @porcellus can help out.
rp_st
10/01/2022, 2:53 PM
Since it’s the weekend, expect help by Monday please.
p
porcellus
10/02/2022, 10:10 AM
Hi, you can use this version of the
AuthGuard
as
Copy code
@UseGuards(new AuthGuard())
I'll update the code in the guide in a bit to be a bit more flexible, which will enable usage without instantiating it yourself.
r
rp_st
10/02/2022, 3:51 PM
Also, checkout the nestjs example app that we have. You can set it up by running npx create-supertokens-app
SuperTokens is an open source authentication solution offering features like: Different types of login: Email / password, Passwordless (OTP or Magic link based).