Skip to content

Commit

Permalink
fix(auth): Remove authConfig, change it to coreConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
EndyKaufman committed Sep 22, 2018
1 parent c56bbbd commit 7cbc128
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 133 deletions.
16 changes: 8 additions & 8 deletions src/libs/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@ import { services } from './services';
@Module({
imports: [
HttpModule,
TypeOrmModule.forFeature([...coreEntities, ...entities]),
CoreModule.forFeature()
CoreModule,
TypeOrmModule.forFeature([...coreEntities, ...entities])
],
controllers: [...controllers],
providers: [...configs, ...services],
exports: [...services]
exports: [...configs, ...services]
})
export class AuthModule implements NestModule {
static forFeature(): DynamicModule {
return {
module: AuthModule,
imports: [HttpModule, CoreModule.forFeature()],
imports: [HttpModule, CoreModule],
providers: [...services],
exports: [...services]
exports: [...configs, ...services]
};
}
static forRoot(options: { providers: Provider[] }): DynamicModule {
return {
module: AuthModule,
imports: [
HttpModule,
TypeOrmModule.forFeature([...coreEntities, ...entities]),
CoreModule.forFeature()
CoreModule,
TypeOrmModule.forFeature([...coreEntities, ...entities])
],
controllers: [...controllers],
providers: [
Expand All @@ -49,7 +49,7 @@ export class AuthModule implements NestModule {
...services,
...passportStrategies
],
exports: [...services]
exports: [...configs, ...services]
};
}
public configure(consumer: MiddlewareConsumer) {
Expand Down
11 changes: 0 additions & 11 deletions src/libs/auth/configs/auth.config.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/libs/auth/configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ import {
GOOGLE_PLUS_CONFIG_TOKEN
} from '../configs/google-plus.config';
import { defaultJwtConfig, JWT_CONFIG_TOKEN } from '../configs/jwt.config';
import { defaultAuthConfig, AUTH_CONFIG_TOKEN } from './auth.config';

export const configs = [
{
provide: AUTH_CONFIG_TOKEN,
useValue: defaultAuthConfig
},
{
provide: JWT_CONFIG_TOKEN,
useValue: defaultJwtConfig
Expand Down
28 changes: 8 additions & 20 deletions src/libs/auth/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import {
Body,
Controller,
Get,
HttpCode,
HttpStatus,
Inject,
Logger,
Post,
Req
} from '@nestjs/common';
import { Body, Controller, Get, HttpCode, HttpStatus, Inject, Logger, Post, Req } from '@nestjs/common';
import { ApiResponse, ApiUseTags } from '@nestjs/swagger';
import { OutAccountDto } from '@rucken/core-nestjs';
import { CORE_CONFIG_TOKEN, ICoreConfig, OutAccountDto } from '@rucken/core-nestjs';
import { plainToClass } from 'class-transformer';
import { JsonWebTokenError } from 'jsonwebtoken';
import { AUTH_CONFIG_TOKEN } from '../configs/auth.config';
import { FacebookSignInDto } from '../dto/facebook-signIn.dto';
import { FacebookTokenDto } from '../dto/facebook-token.dto';
import { GooglePlusSignInDto } from '../dto/google-plus-signIn.dto';
Expand All @@ -22,7 +11,6 @@ import { SignInDto } from '../dto/sign-in.dto';
import { SignUpDto } from '../dto/sign-up.dto';
import { TokenDto } from '../dto/token.dto';
import { UserTokenDto } from '../dto/user-token.dto';
import { IAuthConfig } from '../interfaces/auth-config.interface';
import { IJwtPayload } from '../interfaces/jwt-payload.interface';
import { AuthService } from '../services/auth.service';
import { TokenService } from '../services/token.service';
Expand All @@ -31,10 +19,10 @@ import { TokenService } from '../services/token.service';
@Controller('/api/auth')
export class AuthController {
constructor(
@Inject(AUTH_CONFIG_TOKEN) private readonly authConfig: IAuthConfig,
@Inject(CORE_CONFIG_TOKEN) private readonly coreConfig: ICoreConfig,
private readonly authService: AuthService,
private readonly tokenService: TokenService
) {}
) { }
@HttpCode(HttpStatus.OK)
@Post('signin')
@ApiResponse({
Expand Down Expand Up @@ -106,7 +94,7 @@ export class AuthController {
AuthController.name + ':requestFacebookRedirectUrl#origin'
);
return this.authService.requestFacebookRedirectUri(
req.get('origin') || this.authConfig.protocol + '://' + req.get('host')
req.get('origin') || this.coreConfig.protocol + '://' + req.get('host')
);
}
@HttpCode(HttpStatus.OK)
Expand All @@ -125,7 +113,7 @@ export class AuthController {
);
return this.authService.facebookSignIn(
facebookSignInDto.code,
req.get('origin') || this.authConfig.protocol + '://' + req.get('host')
req.get('origin') || this.coreConfig.protocol + '://' + req.get('host')
);
}
@HttpCode(HttpStatus.OK)
Expand Down Expand Up @@ -153,7 +141,7 @@ export class AuthController {
AuthController.name + ':requestGoogleRedirectUri#origin'
);
return this.authService.requestGoogleRedirectUri(
req.get('origin') || this.authConfig.protocol + '://' + req.get('host')
req.get('origin') || this.coreConfig.protocol + '://' + req.get('host')
);
}
@HttpCode(HttpStatus.OK)
Expand All @@ -169,7 +157,7 @@ export class AuthController {
Logger.log(req.get('origin'), AuthController.name + ':googleSignIn#origin');
return this.authService.googleSignIn(
googleSignInDto.code,
req.get('origin') || this.authConfig.protocol + '://' + req.get('host')
req.get('origin') || this.coreConfig.protocol + '://' + req.get('host')
);
}
@HttpCode(HttpStatus.OK)
Expand Down
10 changes: 0 additions & 10 deletions src/libs/auth/interfaces/auth-config.interface.ts

This file was deleted.

44 changes: 15 additions & 29 deletions src/libs/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,34 @@
import {
BadRequestException,
ConflictException,
HttpService,
Inject,
Injectable,
Logger
} from '@nestjs/common';
import {
CustomError,
GroupsService,
User,
UsersService
} from '@rucken/core-nestjs';
import { BadRequestException, ConflictException, HttpService, Inject, Injectable, Logger } from '@nestjs/common';
import { CORE_CONFIG_TOKEN, CustomError, GroupsService, ICoreConfig, User, UsersService } from '@rucken/core-nestjs';
import { plainToClass } from 'class-transformer';
import { stringify } from 'querystring';
import { map } from 'rxjs/operators';
import { AUTH_CONFIG_TOKEN } from '../configs/auth.config';
import { FACEBOOK_CONFIG_TOKEN } from '../configs/facebook.config';
import { GOOGLE_PLUS_CONFIG_TOKEN } from '../configs/google-plus.config';
import { RedirectUriDto } from '../dto/redirect-uri.dto';
import { SignInDto } from '../dto/sign-in.dto';
import { SignUpDto } from '../dto/sign-up.dto';
import { IAuthConfig } from '../interfaces/auth-config.interface';
import { IFacebookConfig } from '../interfaces/facebook-config.interface';
import { IGooglePlusConfig } from '../interfaces/google-plus-config.interface';
@Injectable()
export class AuthService {
private localUri: string;

constructor(
@Inject(AUTH_CONFIG_TOKEN) private readonly authConfig: IAuthConfig,
@Inject(CORE_CONFIG_TOKEN) private readonly coreConfig: ICoreConfig,
@Inject(FACEBOOK_CONFIG_TOKEN) private readonly fbConfig: IFacebookConfig,
@Inject(GOOGLE_PLUS_CONFIG_TOKEN)
private readonly googlePlusConfig: IGooglePlusConfig,
private readonly httpService: HttpService,
private readonly usersService: UsersService,
private readonly groupsService: GroupsService
) {
if (this.authConfig.port) {
this.localUri = `http://${this.authConfig.domain}:${
this.authConfig.port
}`;
if (this.coreConfig.port) {
this.localUri = `http://${this.coreConfig.domain}:${
this.coreConfig.port
}`;
} else {
this.localUri = `http://${this.authConfig.domain}`;
this.localUri = `http://${this.coreConfig.domain}`;
}
}
async info(options: { id: number }) {
Expand Down Expand Up @@ -77,7 +63,7 @@ export class AuthService {
throw new ConflictException(
`User with email "${options.email}" is exists`
);
} catch (error) {}
} catch (error) { }
}
if (options.username) {
try {
Expand All @@ -87,7 +73,7 @@ export class AuthService {
throw new ConflictException(
`User with username "${options.username}" is exists`
);
} catch (error) {}
} catch (error) { }
}
const group = this.groupsService.getGroupByName({ name: 'user' });
const newUser = await plainToClass(User, options).setPassword(
Expand All @@ -104,7 +90,7 @@ export class AuthService {
];
const redirect_uri: string = `${
this.fbConfig.login_dialog_uri
}?${queryParams.join('&')}`.replace('{host}', host);
}?${queryParams.join('&')}`.replace('{host}', host);
Logger.log(redirect_uri, AuthService.name + ':requestFacebookRedirectUri');
return {
redirect_uri
Expand Down Expand Up @@ -161,9 +147,9 @@ export class AuthService {
);
throw new BadRequestException(
error &&
error.response &&
error.response.data &&
error.response.data.error
error.response &&
error.response.data &&
error.response.data.error
? error.response.data.error.message
: error.message
);
Expand All @@ -178,7 +164,7 @@ export class AuthService {
];
const redirect_uri: string = `${
this.googlePlusConfig.login_dialog_uri
}?${queryParams.join('&')}`.replace('{host}', host);
}?${queryParams.join('&')}`.replace('{host}', host);
Logger.log(redirect_uri, AuthService.name + ':requestGoogleRedirectUri');
return {
redirect_uri
Expand Down
13 changes: 6 additions & 7 deletions src/libs/auth/services/oauth-tokens-accesstokens.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { Inject, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { AUTH_CONFIG_TOKEN } from '../configs/auth.config';
import { OauthTokensAccesstoken } from '../entities/oauth-tokens-accesstoken.entity';
import { IAuthConfig } from '../interfaces/auth-config.interface';
import { CORE_CONFIG_TOKEN, ICoreConfig } from '@rucken/core-nestjs';

@Injectable()
export class OauthTokensAccesstokensService {
constructor(
@Inject(AUTH_CONFIG_TOKEN) private readonly authConfig: IAuthConfig,
@Inject(CORE_CONFIG_TOKEN) private readonly coreConfig: ICoreConfig,
@InjectRepository(OauthTokensAccesstoken)
private readonly repository: Repository<OauthTokensAccesstoken>
) {}
) { }
async create(options: { item: OauthTokensAccesstoken }) {
try {
options.item = await this.repository.save(options.item);
Expand Down Expand Up @@ -79,9 +78,9 @@ export class OauthTokensAccesstokensService {
}
options.sort =
options.sort &&
new OauthTokensAccesstoken().hasOwnProperty(
options.sort.replace('-', '')
)
new OauthTokensAccesstoken().hasOwnProperty(
options.sort.replace('-', '')
)
? options.sort
: '-id';
const field = options.sort.replace('-', '');
Expand Down
6 changes: 3 additions & 3 deletions src/libs/core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { services } from './services';
imports: [TypeOrmModule.forFeature([...entities])],
controllers: [...controllers],
providers: [...configs, ...services],
exports: [...services]
exports: [...configs, ...services]
})
export class CoreModule {
static forFeature(): DynamicModule {
return {
module: CoreModule,
providers: [...services],
exports: [...services]
exports: [...configs, ...services]
};
}
static forRoot(options: { providers: Provider[] }): DynamicModule {
Expand All @@ -25,7 +25,7 @@ export class CoreModule {
imports: [TypeOrmModule.forFeature([...entities])],
controllers: [...controllers],
providers: [...configs, ...options.providers, ...services],
exports: [...services]
exports: [...configs, ...services]
};
}
}
23 changes: 3 additions & 20 deletions src/main.hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,12 @@ if (NODE_ENV !== 'develop') {
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import {
appFilters as authAppFilters,
defaultFacebookConfig,
defaultJwtConfig,
FACEBOOK_CONFIG_TOKEN,
IFacebookConfig,
IJwtConfig,
JWT_CONFIG_TOKEN,
IGooglePlusConfig,
defaultGooglePlusConfig,
GOOGLE_PLUS_CONFIG_TOKEN
} from '@rucken/auth-nestjs';
import {
appFilters,
appPipes,
CORE_CONFIG_TOKEN,
defaultCoreConfig,
ICoreConfig
} from '@rucken/core-nestjs';
import { AppModule } from './apps/demo/app.module';
import { appFilters as authAppFilters, defaultFacebookConfig, defaultGooglePlusConfig, defaultJwtConfig, FACEBOOK_CONFIG_TOKEN, GOOGLE_PLUS_CONFIG_TOKEN, IFacebookConfig, IGooglePlusConfig, IJwtConfig, JWT_CONFIG_TOKEN } from '@rucken/auth-nestjs';
import { appFilters, appPipes, CORE_CONFIG_TOKEN, defaultCoreConfig, ICoreConfig } from '@rucken/core-nestjs';
import { load } from 'dotenv';
import { accessSync, readFileSync } from 'fs';
import * as path from 'path';
import { AppModule } from './apps/demo/app.module';

declare const module: any;

Expand Down
23 changes: 3 additions & 20 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,12 @@ if (NODE_ENV !== 'develop') {
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import {
appFilters as authAppFilters,
defaultFacebookConfig,
defaultJwtConfig,
FACEBOOK_CONFIG_TOKEN,
IFacebookConfig,
IJwtConfig,
JWT_CONFIG_TOKEN,
IGooglePlusConfig,
defaultGooglePlusConfig,
GOOGLE_PLUS_CONFIG_TOKEN
} from '@rucken/auth-nestjs';
import {
appFilters,
appPipes,
CORE_CONFIG_TOKEN,
defaultCoreConfig,
ICoreConfig
} from '@rucken/core-nestjs';
import { AppModule } from './apps/demo/app.module';
import { appFilters as authAppFilters, defaultFacebookConfig, defaultGooglePlusConfig, defaultJwtConfig, FACEBOOK_CONFIG_TOKEN, GOOGLE_PLUS_CONFIG_TOKEN, IFacebookConfig, IGooglePlusConfig, IJwtConfig, JWT_CONFIG_TOKEN } from '@rucken/auth-nestjs';
import { appFilters, appPipes, CORE_CONFIG_TOKEN, defaultCoreConfig, ICoreConfig } from '@rucken/core-nestjs';
import { load } from 'dotenv';
import { accessSync, readFileSync } from 'fs';
import * as path from 'path';
import { AppModule } from './apps/demo/app.module';

/* hmr
declare const module: any;
Expand Down

1 comment on commit 7cbc128

@vercel
Copy link

@vercel vercel bot commented on 7cbc128 Sep 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now cannot deploy because all the allocated instances are in use.
You can either upgrade your plan or down scale existing deployments.

Please sign in to comment.