@nestjs/jwt#JwtService TypeScript Examples
The following examples show how to use
@nestjs/jwt#JwtService.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: auth.service.ts From svvs with MIT License | 6 votes |
/**
* Inject into AuthService: JwtService, UserService, PasswordService
*
* @param jwtService Implements interaction with JWT
* @param userService Implements interaction with the user entity
* @param passwordService Implements interaction with bcrypt and compare password
*/
constructor(
private readonly jwtService: JwtService,
private readonly userService: UserService,
private readonly passwordService: PasswordService
) {}
Example #2
Source File: auth.service.ts From uniauth-backend with MIT License | 6 votes |
/** initialize a logger with auth context */
constructor(
private jwtService: JwtService,
@Inject(UserService)
private readonly userService: UserService,
@Inject(WINSTON_MODULE_PROVIDER)
private readonly logger = new Logger('auth'),
) {}
Example #3
Source File: auth.controller.spec.ts From nest-js-boilerplate with MIT License | 6 votes |
describe('Auth Controller', () => {
let controller: AuthController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
providers: [
{
provide: AuthService,
useValue: {},
},
{
provide: UsersService,
useValue: {},
},
{
provide: JwtService,
useValue: {},
},
],
}).compile();
controller = module.get<AuthController>(AuthController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
Example #4
Source File: account.service.ts From uniauth-backend with MIT License | 6 votes |
/**
* Initialize account services
*/
constructor(
private jwtService: JwtService,
@Inject(ApplicationService)
private readonly applicationService: ApplicationService,
@Inject(UserService)
private readonly userService: UserService,
@Inject(AuthService)
private readonly authService: AuthService,
@Inject(WINSTON_MODULE_PROVIDER)
private readonly logger = new Logger('accounts'),
) {}
Example #5
Source File: jwtToken.service.spec.template.ts From amplication with Apache License 2.0 | 6 votes |
describe("Testing the TokenServiceBase", () => {
let tokenServiceBase: TokenServiceBase;
const jwtService = mock<JwtService>();
beforeEach(() => {
tokenServiceBase = new TokenServiceBase(jwtService);
jwtService.signAsync.mockClear();
});
describe("Testing the BasicTokenService.createToken()", () => {
it("should create valid token for valid username and password", async () => {
jwtService.signAsync.mockReturnValue(Promise.resolve(SIGN_TOKEN));
expect(
await tokenServiceBase.createToken(
VALID_CREDENTIALS.username,
VALID_CREDENTIALS.password
)
).toBe(SIGN_TOKEN);
});
it("should reject when username missing", () => {
const result = tokenServiceBase.createToken(
//@ts-ignore
null,
VALID_CREDENTIALS.password
);
return expect(result).rejects.toBe(INVALID_USERNAME_ERROR);
});
it("should reject when password missing", () => {
const result = tokenServiceBase.createToken(
VALID_CREDENTIALS.username,
//@ts-ignore
null
);
return expect(result).rejects.toBe(INVALID_PASSWORD_ERROR);
});
});
});
Example #6
Source File: auth.service.ts From amplication with Apache License 2.0 | 6 votes |
constructor(
private readonly jwtService: JwtService,
private readonly passwordService: PasswordService,
private readonly prismaService: PrismaService,
private readonly accountService: AccountService,
private readonly userService: UserService,
@Inject(forwardRef(() => WorkspaceService))
private readonly workspaceService: WorkspaceService
) {}
Example #7
Source File: auth.service.ts From bank-server with MIT License | 5 votes |
constructor(
private readonly _jwtService: JwtService,
private readonly _configService: ConfigService,
private readonly _userService: UserService,
private readonly _userAuthService: UserAuthService,
private readonly _userAuthForgottenPasswordService: UserAuthForgottenPasswordService,
) {}
Example #8
Source File: auth.service.ts From nest-js-quiz-manager with MIT License | 5 votes |
constructor(
private userService: UserService,
private jwtService: JwtService,
) {}
Example #9
Source File: jwtToken.service.template.ts From amplication with Apache License 2.0 | 5 votes |
constructor(protected readonly jwtService: JwtService) {}
Example #10
Source File: auth.service.ts From nestjs-rest-sample with GNU General Public License v3.0 | 5 votes |
constructor(
private userService: UserService,
private jwtService: JwtService,
) { }
Example #11
Source File: auth.service.ts From pandaid with MIT License | 5 votes |
constructor(private readonly usersService: UsersService, private jwtService: JwtService) {}
Example #12
Source File: auth.service.ts From nestjs-starter-rest-api with MIT License | 5 votes |
constructor(
private userService: UserService,
private jwtService: JwtService,
private configService: ConfigService,
private readonly logger: AppLogger,
) {
this.logger.setContext(AuthService.name);
}
Example #13
Source File: auth.service.ts From api with GNU Affero General Public License v3.0 | 5 votes |
constructor(
private userService: UserService,
private jwtService: JwtService,
private passwordService: PasswordService,
private logger: PinoLogger,
) {
logger.setContext(this.constructor.name)
}
Example #14
Source File: auth.service.ts From knests with MIT License | 5 votes |
constructor(
private readonly usersService: UsersService,
private readonly jwtService: JwtService,
) { }
Example #15
Source File: auth.service.spec.ts From MyAPI with MIT License | 5 votes |
describe('AuthService', () => {
let service: AuthService
const userService = { findOne: jest.fn(() => Promise.resolve(new User())) }
const roleService = { findById: jest.fn(() => Promise.resolve(new Role())) }
const jwtService = { sign: jest.fn(({ sub, role }) => sub + role) }
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
JwtService,
UserService,
RoleService,
AuthService
],
})
.overrideProvider(JwtService).useValue(jwtService)
.overrideProvider(UserService).useValue(userService)
.overrideProvider(RoleService).useValue(roleService)
.compile()
service = module.get<AuthService>(AuthService)
})
it('should be defined', () => {
expect(service).toBeDefined()
})
it('should validate user', async () => {
const id = '123'
const password = '111'
await service.validateUser(id, password).catch(() => null)
expect(userService.findOne).toBeCalledWith(id)
})
it('should get access token', async () => {
const userId = '123'
const roleId = 111
const user = new User({ id: userId })
user.role = new Role({ id: roleId })
const accessToken = userId + roleId
const result = await service.getAccessToken(user)
expect(result.access_token).toBe(accessToken)
expect(jwtService.sign).toBeCalled()
})
})
Example #16
Source File: auth.service.ts From MyAPI with MIT License | 5 votes |
constructor(
private readonly jwtService: JwtService,
private readonly userService: UserService,
private readonly roleService: RoleService
) {}
Example #17
Source File: HttpAuthService.ts From typescript-clean-architecture with MIT License | 5 votes |
constructor(
@Inject(UserDITokens.UserRepository)
private readonly userRepository: UserRepositoryPort,
private readonly jwtService: JwtService
) {}
Example #18
Source File: auth.service.ts From nestjs-starter with MIT License | 5 votes |
constructor(private readonly usersService: UsersService, private readonly jwtService: JwtService) {}
Example #19
Source File: login.controller.ts From office-hours with GNU General Public License v3.0 | 5 votes |
constructor(
private connection: Connection,
private loginCourseService: LoginCourseService,
private jwtService: JwtService,
private configService: ConfigService,
) {}
Example #20
Source File: testUtils.ts From office-hours with GNU General Public License v3.0 | 5 votes |
export function setupIntegrationTest(
module: Type<any>,
modifyModule?: ModuleModifier,
): (u?: SupertestOptions) => supertest.SuperTest<supertest.Test> {
let app: INestApplication;
let jwtService: JwtService;
let conn: Connection;
beforeAll(async () => {
let testModuleBuilder = Test.createTestingModule({
imports: [
module,
LoginModule,
TestTypeOrmModule,
TestConfigModule,
RedisModule.register([
{ name: 'pub' },
{ name: 'sub' },
{ name: 'db' },
]),
],
})
.overrideProvider(TwilioService)
.useValue(mockTwilio);
if (modifyModule) {
testModuleBuilder = modifyModule(testModuleBuilder);
}
const testModule = await testModuleBuilder.compile();
app = testModule.createNestApplication();
addGlobalsToApp(app);
jwtService = testModule.get<JwtService>(JwtService);
conn = testModule.get<Connection>(Connection);
await app.init();
});
afterAll(async () => {
await app.close();
await conn.close();
});
beforeEach(async () => {
await conn.synchronize(true);
});
return (options?: SupertestOptions): supertest.SuperTest<supertest.Test> => {
const agent = supertest.agent(app.getHttpServer());
if (options?.userId) {
const token = jwtService.sign({ userId: options.userId });
agent.set('Cookie', [`auth_token=${token}`]);
}
return agent;
};
}
Example #21
Source File: auth.service.ts From nestjs-angular-starter with MIT License | 5 votes |
constructor(private jwtService: JwtService) {}
Example #22
Source File: auth.service.ts From pknote-backend with GNU General Public License v3.0 | 5 votes |
constructor(
@InjectRepository(UserRepository)
private userRepository: UserRepository,
private jwtService: JwtService,
private smsService: SmsService,
private redisClientService: RedisClientService,
) {}
Example #23
Source File: auth.service.ts From 42_checkIn with GNU General Public License v3.0 | 5 votes |
constructor(
private readonly configService: ConfigService,
private readonly jwtService: JwtService,
private readonly httpService: HttpService,
private readonly logger: MyLogger,
) {}
Example #24
Source File: auth.controller.ts From nest-js-boilerplate with MIT License | 5 votes |
constructor(
private readonly authService: AuthService,
private readonly jwtService: JwtService,
private readonly usersService: UsersService,
private readonly configService: ConfigService,
) {}
Example #25
Source File: auth.service.ts From Cromwell with MIT License | 5 votes |
constructor(
private jwtService: JwtService,
) {
authServiceInst = this;
this.init();
}
Example #26
Source File: auth.service.ts From NestJs-youtube with MIT License | 5 votes |
constructor(
public readonly userService: UserService,
private readonly jwtService: JwtService,
) {}
Example #27
Source File: auth.controller.ts From nest-js-boilerplate with MIT License | 5 votes |
constructor(
private readonly authService: AuthService,
private readonly jwtService: JwtService,
private readonly usersService: UsersService,
private readonly configService: ConfigService,
) { }
Example #28
Source File: auth.service.ts From nest-js-boilerplate with MIT License | 5 votes |
constructor(
private readonly jwtService: JwtService,
private readonly usersRepository: UsersRepository,
private readonly authRepository: AuthRepository,
private readonly configService: ConfigService,
) { }
Example #29
Source File: roles.guard.ts From nest-js-boilerplate with MIT License | 5 votes |
constructor(
private reflector: Reflector,
private jwtService: JwtService,
) {}