Skip to content

Commit

Permalink
Merge pull request #25 from spknetwork/docs-fix
Browse files Browse the repository at this point in the history
Fix env var related error when using JWT dervice
  • Loading branch information
sisygoboom authored Jun 26, 2024
2 parents 704b55b + 62ab4a0 commit 1b9a822
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 19 deletions.
12 changes: 10 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'dotenv/config';
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { TasksModule } from './services/tasks/tasks.module';
import { ScheduleModule } from '@nestjs/schedule';
import { VideoModule } from './repositories/video/video.module';
import { ConfigModule } from '@nestjs/config';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { PublishingModule } from './services/publishing/publishing.module';
import { CreatorModule } from './repositories/creator/creator.module';
import { UserModule } from './repositories/user/user.module';
Expand Down Expand Up @@ -72,7 +73,14 @@ const mongoUrl = process.env.CORE_MONGODB_URL || 'mongodb://mongo:27017';
UserAccountModule,
ApiModule,
VotingModule,
JwtModule,
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
secretOrPrivateKey: configService.get<string>('JWT_PRIVATE_KEY'),
signOptions: { expiresIn: '30d' },
}),
}),
],
controllers: [],
providers: [],
Expand Down
12 changes: 9 additions & 3 deletions src/services/api/api.module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dotenv/config';
import { Module } from '@nestjs/common';
import { ApiController } from './api.controller';
import { AuthModule } from '../auth/auth.module';
Expand All @@ -8,6 +9,7 @@ import { EmailModule } from '../email/email.module';
import { LinkedAccountModule } from '../../repositories/linked-accounts/linked-account.module';
import { RequireHiveVerify } from './utils';
import { JwtModule } from '@nestjs/jwt';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
imports: [
Expand All @@ -17,9 +19,13 @@ import { JwtModule } from '@nestjs/jwt';
HiveModule,
LinkedAccountModule,
EmailModule,
JwtModule.register({
privateKey: process.env.JWT_PRIVATE_KEY,
signOptions: { expiresIn: '30d' },
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
secretOrPrivateKey: configService.get<string>('JWT_PRIVATE_KEY'),
signOptions: { expiresIn: '30d' },
}),
}),
],
controllers: [ApiController],
Expand Down
13 changes: 11 additions & 2 deletions src/services/api/dto/LoginSingleton.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { IsNotEmpty } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import crypto from 'crypto';
import { PrivateKey } from '@hiveio/dhive';

const privateKey = PrivateKey.fromSeed(crypto.randomBytes(32).toString('hex'));
const message = { account: 'sisygoboom', ts: Date.now() };
const signature = privateKey
.sign(crypto.createHash('sha256').update(JSON.stringify(message)).digest())
.toString();

/**
* Data Transfer Object (DTO) for login singleton hive
Expand All @@ -16,14 +24,15 @@ export class LoginSingletonHiveDto {
ts: {
type: 'number',
description: 'Timestamp of the proof payload',
example: 1625158800,
example: Date.now(),
},
account: {
type: 'string',
description: 'Account associated with the proof payload',
example: 'user123',
},
},
example: message,
})
proof_payload: {
ts: number;
Expand All @@ -37,7 +46,7 @@ export class LoginSingletonHiveDto {
@ApiProperty({
description: 'Proof string for authentication',
type: 'string',
example: 'proofString123',
example: signature,
})
proof: string;
}
Expand Down
20 changes: 13 additions & 7 deletions src/services/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'dotenv/config';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { AuthService } from './auth.service';
import { PassportModule } from '@nestjs/passport';
import { JwtModule, JwtService } from '@nestjs/jwt';
import { JwtModule } from '@nestjs/jwt';
import { JwtStrategy, LocalStrategy } from './auth.strategy';
import { UserModule } from '../../repositories/user/user.module';
import { UserAccountModule } from '../../repositories/userAccount/user-account.module';
Expand All @@ -19,18 +18,25 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
ConfigModule,
UserModule,
UserAccountModule,
UserModule,
HiveAccountModule,
HiveModule,
EmailModule,
SessionModule,
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
privateKey: process.env.JWT_PRIVATE_KEY,
signOptions: { expiresIn: '30d' },
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const key = configService.get<string>('JWT_PRIVATE_KEY');
console.log(key);
return {
secretOrPrivateKey: key,
signOptions: { expiresIn: '30d' },
};
},
}),
],
providers: [AuthService, LocalStrategy, JwtService, ConfigService, JwtStrategy],
providers: [AuthService, LocalStrategy, JwtStrategy, ConfigService],
controllers: [AuthController],
exports: [AuthService],
})
Expand Down
5 changes: 3 additions & 2 deletions src/services/auth/auth.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from './auth.service';
import { ExtractJwt } from 'passport-jwt';
import 'dotenv/config';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class LocalStrategy extends PassportStrategy(StrategyLocal) {
Expand All @@ -23,8 +24,8 @@ export class LocalStrategy extends PassportStrategy(StrategyLocal) {

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
const jwtPrivateKey = process.env.JWT_PRIVATE_KEY;
constructor(configService: ConfigService) {
const jwtPrivateKey = configService.get<string>('JWT_PRIVATE_KEY');
if (!jwtPrivateKey) throw new Error('Missing JWT_PRIVATE_KEY in .env');
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
Expand Down
11 changes: 8 additions & 3 deletions src/services/uploader/uploading.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PublishingModule } from '../publishing/publishing.module';
import { HiveModule } from '../../repositories/hive/hive.module';
import { JwtModule } from '@nestjs/jwt';
import { RequireHiveVerify, UserDetailsInterceptor } from '../api/utils';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
imports: [
Expand All @@ -17,9 +18,13 @@ import { RequireHiveVerify, UserDetailsInterceptor } from '../api/utils';
IpfsModule,
PublishingModule,
HiveModule,
JwtModule.register({
privateKey: process.env.JWT_PRIVATE_KEY,
signOptions: { expiresIn: '30d' },
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
secretOrPrivateKey: configService.get<string>('JWT_PRIVATE_KEY'),
signOptions: { expiresIn: '30d' },
}),
}),
],
controllers: [UploadingController],
Expand Down

0 comments on commit 1b9a822

Please sign in to comment.