Controller:
import { IsNotEmpty } from 'class-validator';
import { Body, JsonController, Post, Req, Res } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { Logger, LoggerInterface } from '../../decorators/Logger';
import { AuthService } from '../services/AuthService';
// import { UserService } from '../services/UserService';
import { UserResponse } from './UserController';
class LoginBody {
@IsNotEmpty()
public username: string;
Middleware: import jwt from 'jsonwebtoken'; import { ExpressMiddlewareInterface, Middleware } from 'routing-controllers';
import { Logger, LoggerInterface } from '../../decorators/Logger';
@Middleware({ type: 'before' }) export class JwtAuthMiddleware implements ExpressMiddlewareInterface {
}
Controller: import { IsNotEmpty } from 'class-validator'; import { Body, JsonController, Post, Req, Res } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { Logger, LoggerInterface } from '../../decorators/Logger'; import { AuthService } from '../services/AuthService'; // import { UserService } from '../services/UserService'; import { UserResponse } from './UserController';
class LoginBody { @IsNotEmpty() public username: string;
}
@JsonController('/auth') @OpenAPI({ security: [{ basicAuth: [] }] }) export class AuthController {
}
Service import bcrypt from 'bcrypt'; import jwt from 'jsonwebtoken'; import { Service } from 'typedi';
import { Logger, LoggerInterface } from '../../decorators/Logger'; import { User } from '../models/User'; import { UserService } from './UserService';
@Service() export class AuthService {
}
Repository import knex, { Knex } from 'knex'; import { EntityRepository, Repository } from 'typeorm';
import knexConfig from '../../../src/knexfile'; import { User } from '../models/User';
const environment = process.env.NODE_ENV || 'development'; const DB: Knex = knex(knexConfig[environment]); @EntityRepository(User) export class UserRepository extends Repository {
public async findOneByUsername(username: string): Promise<User | undefined> {
const user = await DB('user')
.where({ username })
.first();
}
But I always failed