Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Commit

Permalink
feat($gateway): authentication module has coming now
Browse files Browse the repository at this point in the history
Whats new:
- Now we can login after registration
- Now we can detecting for any action using queryMap based on userToken
- Now client side can integrating from gateway
- Preparing for intl integration

Whats next:
- 2FA in testing stage
- Resetting and changing password on the queue

Whats still needed:
- Setting up mailer service (DevOps)
- Setting up sqs or equal service for tasks queue (DevOps)
- Text for License Agreement (Marketing)
- Admin dashboard (Design)
- ACL list and features for each (Marketing)

Signed-off-by: Andrew Ghostuhin <[email protected]>

## For normal working still need working subdomains on 80 port
  • Loading branch information
TorinAsakura committed Nov 14, 2018
1 parent 2fdf1db commit ba74844
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@nestjs/common": "^5.2.2",
"@nestjs/core": "^5.2.2",
"@nestjs/graphql": "^3.0.0",
"@nestjs/jwt": "^0.1.3",
"@nestjs/jwt": "^0.2.0",
"@nestjs/typeorm": "^5.2.0",
"accesscontrol": "^2.2.1",
"bcryptjs": "^2.4.3",
Expand Down
12 changes: 10 additions & 2 deletions packages/auth/src/commands/handlers/GenerateTokenHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export class GenerateTokenHandler implements ICommandHandler<GenerateTokenComman
async execute(command: GenerateTokenCommand) {
const user = await this.userRepository
.createQueryBuilder('user')
.where('LOWER(phone) = LOWER(:phone)', { phone: command.phone })
.where('LOWER(email) = LOWER(:email)', { email: command.email })
.getOne()

if (!user) {
return {
errors: {
phone: messages.phoneNotFound,
email: messages.invalidEmailOrPassword,
},
}
}
Expand All @@ -38,6 +38,14 @@ export class GenerateTokenHandler implements ICommandHandler<GenerateTokenComman
}
}

if (!user.isVerified) {
return {
errors: {
email: 'Not verified',
},
}
}

const token = this.jwtService.sign({ id: user.id })

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/commands/impl/GenerateTokenCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ICommand } from '@nestjs/cqrs'

export class GenerateTokenCommand implements ICommand {
constructor(
public readonly phone: string,
public readonly email: string,
public readonly password: string,
) {}
}
7 changes: 1 addition & 6 deletions packages/auth/src/dto/LoginDto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { Transform } from 'class-transformer'
import { normalizePhone } from '@er/common'

export class LoginDto {
@Transform(normalizePhone)
readonly phone: string

readonly email: string
readonly password: string
}
2 changes: 1 addition & 1 deletion packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OnModuleInit, Module } from '@nestjs/common'
import { ModuleRef } from '@nestjs/core'
import { TypeOrmModule } from '@nestjs/typeorm'
import { JwtModule } from '@nestjs/jwt'
import { CQRSModule, EventBus, CommandBus } from '@av/cqrs'
import { CQRSModule, EventBus, CommandBus } from '@er/cqrs'
import { User } from '@er/users'
import { Resolvers } from './resolvers'
import { CommandHandlers } from './commands/handlers'
Expand Down
10 changes: 3 additions & 7 deletions packages/auth/src/messages.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { defineMessages } from '@er/intl'

export default defineMessages({
phoneNotFound: {
id: 'api.auth.phone_not_found',
defaultMessage: 'Пользователь с таким номером телефона не зарегистрирован',
},
invalidPassword: {
id: 'api.auth.invalid_password',
defaultMessage: 'Неверный пароль',
invalidEmailOrPassword: {
id: 'api.invalid_email_or_password',
defaultMessage: 'Невалидный Email адрес или пароль',
},
})
4 changes: 2 additions & 2 deletions packages/auth/src/resolvers/AuthQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export class AuthQueries {

@Query()
@MapParams(LoginDto)
login(request, { phone, password }) {
login(request, { email, password }) {
return this.commandBus.execute(
new GenerateTokenCommand(phone, password),
new GenerateTokenCommand(email, password),
)
}
}
4 changes: 2 additions & 2 deletions packages/auth/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type JwtToken {
}

type LoginErrors {
phone: String
email: String
password: String
}

Expand All @@ -14,5 +14,5 @@ type LoginResponse {
}

type Query {
login (phone: String!, password: String!) : LoginResponse!
login (email: String!, password: String!) : LoginResponse!
}
5 changes: 5 additions & 0 deletions packages/swt-gateway/ormconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cli": {
"migrationsDir": "migrations"
}
}
38 changes: 38 additions & 0 deletions packages/swt-gateway/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@er/swt-gateway",
"version": "1.0.0",
"main": "src/index.ts",
"license": "MIT",
"scripts": {
"dev": "nodemon --exec node -r ts-node/register --watch ../users/src --watch ../auth/src --watch ../verify/src --watch src src/index.ts",
"typecheck": "tsc --target es2015 --module es2015 --moduleResolution node",
"lint": "tslint --project tsconfig.json -c tslint.json -t codeFrame src/**/*.ts{,x}"
},
"dependencies": {
"@nestjs/common": "^5.2.2",
"@nestjs/core": "^5.2.2",
"@nestjs/graphql": "^3.0.0",
"@nestjs/typeorm": "^5.2.0",
"apollo-server-express": "^1.3.2",
"apollo-upload-server": "^7.0.0",
"class-validator": "^0.9.1",
"graphql": "^0.13.2",
"graphql-iso-date": "^3.5.0",
"graphql-playground-middleware-express": "^1.7.3",
"graphql-tools": "^3.1.1",
"luxon": "^1.3.3",
"pg": "^7.4.3",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.1.0",
"typeorm": "^0.2.7"
},
"devDependencies": {
"@types/graphql": "^0.13.4",
"@types/node": "^10.9.3",
"intl-tools": "^0.1.12",
"nodemon": "^1.18.4",
"ts-node": "^7.0.1",
"tslint": "^5.9.1",
"typescript": "^3.0.3"
}
}
64 changes: 64 additions & 0 deletions packages/swt-gateway/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { CoreModule } from './core'
import { GraphQLModule, GraphQLFactory } from '@nestjs/graphql'
import { UsersModule } from '@er/users'
import { AccountModule } from '@er/account'
import { GraphQLDate, GraphQLTime, GraphQLDateTime } from 'graphql-iso-date'
import { graphqlExpress } from 'apollo-server-express'
import graphqlPlayground from 'graphql-playground-middleware-express'
import { AuthModule, AuthMiddleware } from '@er/auth'
import { VerifyModule } from '@er/verify'

@Module({
imports: [
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.DB_HOST || 'db',
database: process.env.DB_NAME || 'er',
username: process.env.DB_USERNAME || 'root',
password: process.env.DB_PASSWORD || 'root',
entities: [
'../**/src/**/entities/**.ts',
],
migrations: [
'../**/migrations/**.ts',
],
migrationsRun: false,
synchronize: true,
logging: false,
}),
CoreModule,
GraphQLModule,
AuthModule,
AccountModule,
UsersModule,
VerifyModule,
],
providers: [],
})
export class ApplicationModule implements NestModule {
constructor(
private readonly graphQLFactory: GraphQLFactory,
private readonly authMiddleware: AuthMiddleware,
) {}

configure(consumer: MiddlewareConsumer) {
const schema = this.graphQLFactory.createSchema({
typeDefs: this.graphQLFactory.mergeTypesByPaths('../../**/*.graphql'),
resolvers: {
Date: GraphQLDate,
Time: GraphQLTime,
DateTime: GraphQLDateTime,
},
})

consumer
.apply(this.authMiddleware.resolve())
.forRoutes('/graphql')
.apply(graphqlExpress(request => ({ schema, context: { user: request.user }, rootValue: request })))
.forRoutes('/graphql')
.apply(graphqlPlayground({ endpoint: '/graphql' }))
.forRoutes('/graphiql')
}
}
13 changes: 13 additions & 0 deletions packages/swt-gateway/src/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module, Global } from '@nestjs/common'
import { GlobalPubSub } from '@er/cqrs'

@Global()
@Module({
providers: [
GlobalPubSub,
],
exports: [
GlobalPubSub,
],
})
export class CoreModule {}
18 changes: 18 additions & 0 deletions packages/swt-gateway/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NestFactory } from '@nestjs/core'
import { useContainer } from 'class-validator'
import { MapErrorCodes } from '@er/common'
import { ApplicationModule } from './app'

async function bootstrap() {
const app = await NestFactory.create(ApplicationModule)

app.enableCors()

useContainer(app, { fallback: true })

app.useGlobalInterceptors(new MapErrorCodes())

await app.listen(process.env.PORT || 3000)
}

bootstrap()
3 changes: 3 additions & 0 deletions packages/swt-gateway/src/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
scalar DateTime
scalar Date
scalar Time
6 changes: 6 additions & 0 deletions packages/swt-gateway/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
}
}
16 changes: 16 additions & 0 deletions packages/swt-gateway/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": ["tslint:latest"],
"rules": {
"quotemark": [true, "single"],
"semicolon": [true, "never"],
"object-literal-sort-keys": false,
"object-literal-key-quotes": [true, "as-needed"],
"member-access": false,
"ordered-imports": false,
"no-submodule-imports": false,
"no-implicit-dependencies": false,
"interface-name": false,
"no-unused-variable": true,
"arrow-parens": [true, "ban-single-arg-parens"]
}
}

0 comments on commit ba74844

Please sign in to comment.