porcellus
09/24/2021, 4:13 PMuser
09/24/2021, 4:16 PMauth.service.ts
ts
import supertokens from 'supertokens-node';
import EmailPassword from 'supertokens-node/recipe/emailpassword';
import Session from 'supertokens-node/recipe/session';
import { Injectable, Logger } from '@nestjs/common';
@Injectable()
export class AuthService {
constructor(private userService: UserService) {
supertokens.init({
supertokens: {
connectionURI: 'http://localhost:3567',
},
appInfo: {
appName: 'My App',
apiDomain: 'http://localhost:3000',
websiteDomain: 'http://localhost:3001',
},
recipeList: [
EmailPassword.init(),
Session.init(),
],
});
}
}
auth.module.ts
ts
import { DynamicModule, MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { UserService } from 'src/user/user.service';
import { AuthMiddleware } from './auth.middleware';
import { AuthService } from './auth.service';
@Module({
providers: [AuthService, UserService],
exports: [],
controllers: [],
})
export class AuthModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(AuthMiddleware).forRoutes('*');
}
static forRoot(): DynamicModule {
return {
providers: [],
exports: [],
imports: [],
module: AuthModule,
};
}
}
user
09/24/2021, 4:18 PMuser
09/24/2021, 4:20 PMrp
09/24/2021, 4:32 PMEmailPassword.init({
override: {
apis: (oI) => {
return {
...oI,
signUpPOST: undefined
}
}
}
})
Finally, you can manually sign up users like this:
let signUpResponse = await EmailPassword.signUp("email", "password");
if (signUpResponse.status === "OK") {
let user = signUpResponse.user;
// TODO:...
} else if (signUpResponse.status === "EMAIL_ALREADY_EXISTS_ERROR") {
// TODO: handle this error
}
And as @User said, the above works because EmailPassword
is a singleton. Thank you @Userporcellus
09/24/2021, 4:33 PMfrederic
09/27/2021, 9:08 AMfrederic
09/27/2021, 9:08 AMfrederic
09/27/2021, 9:08 AMfrederic
09/27/2021, 9:09 AMrp
09/27/2021, 9:10 AMfrederic
09/27/2021, 9:10 AMfrederic
09/27/2021, 9:11 AMfrederic
09/27/2021, 9:11 AMfrederic
09/27/2021, 9:11 AMfrederic
09/27/2021, 9:11 AMfrederic
09/27/2021, 9:12 AMfrederic
09/27/2021, 9:12 AMrp
09/27/2021, 9:12 AMgetSession
as shown here: https://supertokens.io/docs/thirdpartyemailpassword/nextjs/session-verification/in-ssrfrederic
09/27/2021, 9:12 AMrp
09/27/2021, 9:12 AMfrederic
09/27/2021, 9:12 AMrp
09/27/2021, 9:13 AMfrederic
09/27/2021, 9:13 AMrp
09/27/2021, 9:13 AM{ props: { fromSupertokens: 'needs-refresh' } }
is only meant to be handled for when using getServerSideProps
frederic
09/27/2021, 9:14 AMfrederic
09/27/2021, 9:14 AMfrederic
09/27/2021, 9:15 AMfrederic
09/27/2021, 9:15 AMfrederic
09/27/2021, 9:15 AM