Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
feat: auth jwt provider, redis connection
Browse files Browse the repository at this point in the history
  • Loading branch information
vitJR1 committed Oct 14, 2024
1 parent aa018c4 commit 98ee5df
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 3 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@
},
"dependencies": {
"@nestjs/apollo": "^12.2.0",
"@nestjs/cache-manager": "^2.2.2",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.3",
"@nestjs/core": "^10.0.0",
"@nestjs/graphql": "^12.2.0",
"@nestjs/jwt": "^10.2.0",
"@nestjs/microservices": "^10.3.10",
"@nestjs/platform-express": "^10.0.0",
"@pieceowater-dev/lotof.lib.broadcaster": "^1.0.33",
"amqp-connection-manager": "^4.1.14",
"amqplib": "^0.10.4",
"apollo-server-express": "^3.13.0",
"cache-manager-redis-store": "^3.0.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"graphql": "^16.9.0",
"redis": "^4.7.0",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
},
Expand Down
4 changes: 4 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { CacheModule } from '@nestjs/cache-manager';

import { join } from 'path';
import { HealthModule } from './core/health/health.module';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './services/users/user/users.module';
import { FriendshipModule } from './services/users/friendship/friendship.module';
import { AuthModule } from './services/users/auth/auth.module';
import { RedisOptions } from './core/redis';

// noinspection TypeScriptValidateTypes
@Module({
Expand All @@ -18,6 +21,7 @@ import { AuthModule } from './services/users/auth/auth.module';
isGlobal: true,
envFilePath: '.env',
}),
CacheModule.registerAsync(RedisOptions),
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: join(process.cwd(), 'schema.gql'),
Expand Down
19 changes: 19 additions & 0 deletions src/core/jwt/jwt.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { JwtModule } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import { Global, Module } from '@nestjs/common';

@Global()
@Module({
imports: [
JwtModule.registerAsync({
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
global: true,
secret: configService.get<string>('secret'),
signOptions: { expiresIn: '1d' },
}),
}),
],
exports: [JwtModule],
})
export class GlobalJwtModule {}
3 changes: 3 additions & 0 deletions src/core/jwt/types/token.payload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class TokenPayload {
uuid: string;
}
20 changes: 20 additions & 0 deletions src/core/redis/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { CacheModuleAsyncOptions } from '@nestjs/common/cache';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { redisStore } from 'cache-manager-redis-store';

export const RedisOptions: CacheModuleAsyncOptions = {
isGlobal: true,
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => {
const store = await redisStore({
socket: {
host: configService.get<string>('REDIS_HOST'),
port: parseInt(configService.get<string>('REDIS_PORT')!),
},
});
return {
store: () => store,
};
},
inject: [ConfigService],
};
38 changes: 38 additions & 0 deletions src/services/users/auth/auth.jwt.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Injectable } from '@nestjs/common';
import { TokenPayload } from '../../../core/jwt/types/token.payload';
import { JwtService } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class AuthJwtProvider {
constructor(
private readonly jwtService: JwtService,
private readonly configService: ConfigService,
) {}

signAccessKey(tokenPayload: TokenPayload): string {
return this.jwtService.sign(tokenPayload, {
expiresIn: '10m',
secret: this.configService.get<string>('SECRET_ACCESS'),
});
}

verifyAccessKey(accessToken: string): TokenPayload {
return this.jwtService.verify(accessToken, {
secret: this.configService.get<string>('SECRET_ACCESS'),
});
}

signRefreshKey(tokenPayload: TokenPayload): string {
return this.jwtService.sign(tokenPayload, {
expiresIn: '30d',
secret: this.configService.get<string>('SECRET_REFRESH'),
});
}

verifyRefreshKey(refreshToken: string): TokenPayload {
return this.jwtService.verify(refreshToken, {
secret: this.configService.get<string>('SECRET_REFRESH'),
});
}
}
6 changes: 4 additions & 2 deletions src/services/users/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthResolver } from './auth.resolver';
import { UsersModule } from '../user/users.module';
import { GlobalJwtModule } from '../../../core/jwt/jwt.module';
import { AuthJwtProvider } from './auth.jwt.provider';

@Module({
imports: [UsersModule],
providers: [AuthResolver, AuthService],
imports: [UsersModule, GlobalJwtModule],
providers: [AuthResolver, AuthService, AuthJwtProvider],
exports: [AuthService],
})
export class AuthModule {}
6 changes: 5 additions & 1 deletion src/services/users/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import { RegistrationInput } from './dto/registration.input';
import { LoginInput } from './dto/login.input';
import { UsersMicroservicesProvider } from '../user/users.microservices-provider';
import { User } from '../user/entities/user.entity';
import { AuthJwtProvider } from './auth.jwt.provider';

@Injectable()
export class AuthService {
constructor(private usersMicroservicesProvider: UsersMicroservicesProvider) {}
constructor(
private usersMicroservicesProvider: UsersMicroservicesProvider,
private authJwtProvider: AuthJwtProvider,
) {}

async login(loginInput: LoginInput) {
return this.usersMicroservicesProvider.sendWithTimeout<User, LoginInput>(
Expand Down

0 comments on commit 98ee5df

Please sign in to comment.