#passport.js, authentication in nestjs problems

56 messages · Page 1 of 1 (latest)

molten comet
#

@warped oak - Quick look, I think it has something to do with how you are setting up and/ or working with TypeORM. Your User entity is called UserApi

Another question I'd have is why are you breaking up everything into different folders? A module should hold all parts of what makes up the module i.e. entity, services, providers, controllers, etc.

molten comet
#

what do you mean with breaking up?
As an example, you have your user DTOs in a folder called DTOs. That folder can be put into the users module. Same goes for providers. Why not have a database module?

#

I'm not a TypeORM user, so can't help you much with that. But, I'd double check how you are setting it up and using it.

storm wind
#

You use a custom approach to integrating TypeORM, not the TypeOrmModule, so you can't use @InjectRepository()

#

In your UsersService you should use @Inject('USER_REPOSITORY') instead of @InjectRepository(User)

#

You also need to remove the TypeOrmModule.forFeature() that's in your UsersModule

#

USERS_REPOSITORY. Looks like it's plurarl. That token needs to match exactly in the @Inject() and in the provider. Best way to always ensure that is to make it a constant in a separate file and reference it in both places

#

Yeah. It should only be in the AuthModule

warped oak
#

but if i do it there i have a problem there,

storm wind
#

Your AuthModule should have AuthService in the providers. If you need AuthService in other providers outside of the module it should also be in the exports and the other moudle should add AuthModule to the imports array

warped oak
#

Like this, right? but its still a problem

storm wind
#

Okay, personally, any interactions with the user table in the database via the user repository I would do through the UsersService and then have the UsersModule expose the UsersService via the exports array. That way you don't end up having multiple ways to get to the user table, it all has to go through a centralized location

warped oak
#

so i should move everything that talks to database to userservice

storm wind
#

Everything that talks to the users table of the database should have a pathway through the UsersService to the UsersRepository. That's my opinion, but I find it makes it easier to follow how to get to the database and from a glance know what kind of data your service gets

warped oak
#

im gonna do that right now

warped oak
#

i did it, but i am still with the error from authservice

storm wind
#

Does your UsersModule have UsersService in both the providers and exports?
Does your AuthMoudle have the UsersModule in the imports?

warped oak
#

mybad
but i dont know whats happenings its problem after problem
u fixed so many allready

storm wind
#

With everything we've talked about in the chat so far, what do you think the error means?

warped oak
#

i need to provide jwtservice right?

storm wind
#

And where does the JwtService come from?

warped oak
#

i dont understand this question

plucky ventureBOT
warped oak
#

sorry

storm wind
#

JwtService should come from a module, right? Like JwtModule from @nestjs/jwt?

warped oak
#

oh ye the library jwt

#

so basicaly i need to import jwt module

storm wind
#

So yourUsersModule, if it depends on JwtService should have the JwtModule in the imports

storm wind
warped oak
#

This is about the import of Usersmodule in auth module
it says that it is undefined

storm wind
#

What does the UsersModule llok like?

warped oak
#
import { Module } from '@nestjs/common';
import { UsersProviders } from './users.provider';
import { UsersService } from '../users/users.service';
import { DatabaseModule } from '../modules/database.module';
import { JwtModule, JwtService } from '@nestjs/jwt';
import { AuthModule } from '../auth/auth.module';

@Module({
  imports: [DatabaseModule, JwtModule, AuthModule],
  controllers: [],
  providers: [...UsersProviders, UsersService, JwtService],
  exports: [UsersService],
})
export class UsersModule {}
storm wind
#

Okay, can you show the AuthModule now?

#

Also, remove theJwtService from the providers, it's already implicitly added by importing the JwtModule

warped oak
#
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { jwtConstants } from '../../jwt/constants';
import { UsersModule } from '../users/users.module';
import { AuthService } from './auth.service';
@Module({
  imports: [
    JwtModule.registerAsync({
      useFactory: async () => ({
        secret: jwtConstants.secret,
        signOptions: { expiresIn: '1d' },
      }),
    }),
    UsersModule,
  ],

  providers: [AuthService],
})
export class AuthModule {}
storm wind
#

Ah, and there it is. Circularly dependent modules

#

You need to use a forwardRef(() => OtherModule) if you have circularly dependent modules. Or refactor them so they don't depend on each other

warped oak
#

what makes them circularly dependent

storm wind
#

AuthModule imports UsersModule and UsersModule imports AuthMoudle

warped oak
#

ooh

#

thanks for your time
I do really appriciate

warped oak
#
import { forwardRef, Module } from '@nestjs/common';
import { UsersProviders } from './users.provider';
import { UsersService } from '../users/users.service';
import { DatabaseModule } from '../modules/database.module';
import { JwtModule, JwtService } from '@nestjs/jwt';
import { AuthModule } from '../auth/auth.module';

@Module({
  imports: [DatabaseModule, JwtModule, forwardRef(() => AuthModule)],
  controllers: [],
  providers: [...UsersProviders, UsersService],
  exports: [UsersService],
})
export class UsersModule {}

bassicaly like this right?

storm wind
#

Yes. And something I would suggest is removing JwtModule from the imports of the UsersModule and adding it to theexports of the AuthModule (called module re-exporting)

#

This way you only have one imported instance of JwtModule and it's properly configured already

warped oak
warped oak
storm wind
# warped oak

Why does your AuthModule no longer import the UsersModule?

warped oak
#

it gave me the same error

storm wind
#

Did you also forward ref the UsersMoudle inside the AuthModule?

#

Both sides need the forward ref

warped oak
storm wind
#

That's a different error

#

Now it's warning you that the ConfigService can't be found, because the ConfigModule is not imported and I can assume the ConfigModule is not set to be global

warped oak
#

oh i imported it in auth module and now it works
you are a hero
so bassicaly it were just all import mistakes and it made it harder to fix cus 2 services were talking to the user tabel