feat: basic login

This commit is contained in:
Hasta Ragil Saputra 2021-12-08 15:49:13 +07:00
parent 10984f65a5
commit 141f3e0e5c
7 changed files with 18 additions and 48 deletions

View File

@ -8,7 +8,6 @@ import { LoggerModule } from 'nestjs-pino';
import { TransactionModule } from './transaction/transaction.module'; import { TransactionModule } from './transaction/transaction.module';
import { ProductModule } from './product/product.module'; import { ProductModule } from './product/product.module';
import { ConfigurableModule } from './configurable/configurable.module'; import { ConfigurableModule } from './configurable/configurable.module';
// import { AuthModule } from './auth/auth.module';
import { AuthModule } from './auth/auth.module'; import { AuthModule } from './auth/auth.module';
import configuration from './config/configuration'; import configuration from './config/configuration';
@ -53,6 +52,7 @@ import configuration from './config/configuration';
TransactionModule, TransactionModule,
ConfigurableModule, ConfigurableModule,
ProductModule, ProductModule,
AuthModule,
], ],
}) })
export class AppModule {} export class AppModule {}

View File

@ -2,20 +2,12 @@ import { Module } from '@nestjs/common';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
import { UsersModule } from '../users/users.module'; import { UsersModule } from '../users/users.module';
import { PassportModule } from '@nestjs/passport'; import { PassportModule } from '@nestjs/passport';
import { JwtModule, JwtStrategy } from 'passport-jwt'; import { LocalStrategy } from './local.strategy';
import { AuthController } from './auth.controller'; import { AuthController } from './auth.controller';
@Module({ @Module({
imports: [ imports: [UsersModule, PassportModule],
UsersModule, providers: [AuthService, LocalStrategy],
PassportModule.register({
defaultStrategy: 'jwt',
property: 'user',
session: false,
}),
],
controllers: [AuthController], controllers: [AuthController],
providers: [AuthService, JwtStrategy],
exports: [PassportModule, JwtModule],
}) })
export class AuthModule {} export class AuthModule {}

View File

@ -1,9 +0,0 @@
import { IsNotEmpty } from 'class-validator';
export class InputLoginDto {
@IsNotEmpty()
username: string;
@IsNotEmpty()
password: string;
}

View File

@ -1,9 +0,0 @@
import { IsNotEmpty } from 'class-validator';
export class ResponseLoginDto {
@IsNotEmpty()
username: string;
@IsNotEmpty()
jwt: string;
}

View File

@ -2,7 +2,6 @@ import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport'; import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common'; import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
import { User } from '../users/entities/user.entity';
@Injectable() @Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) { export class LocalStrategy extends PassportStrategy(Strategy) {
@ -10,19 +9,13 @@ export class LocalStrategy extends PassportStrategy(Strategy) {
super(); super();
} }
// async validate( async validate(username: string, password: string): Promise<any> {
// username: string, const user = await this.authService.validateUser(username, password);
// password: string,
// ): Promise<Omit<User, 'password'>> { if (!user) {
// const user = await this.authService.validateUser({ throw new UnauthorizedException();
// username, }
// password,
// }); return user;
// }
// if (!user) {
// throw new UnauthorizedException();
// }
//
// return user;
// }
} }

View File

@ -1,4 +1,4 @@
import { IsNotEmpty, IsUUID } from 'class-validator'; import { IsNotEmpty, IsOptional, IsUUID, ValidateIf } from 'class-validator';
export class CreateUserDto { export class CreateUserDto {
@IsNotEmpty() @IsNotEmpty()
@ -10,6 +10,9 @@ export class CreateUserDto {
@IsUUID() @IsUUID()
roleId: string; roleId: string;
@ValidateIf((o) => {
return !!o.superior;
})
@IsUUID() @IsUUID()
superior: string; superior: string;
} }

View File

@ -1504,7 +1504,7 @@ avvio@^7.1.2:
fastq "^1.6.1" fastq "^1.6.1"
queue-microtask "^1.1.2" queue-microtask "^1.1.2"
axios@0.24.0: axios@0.24.0, axios@^0.24.0:
version "0.24.0" version "0.24.0"
resolved "https://registry.npmjs.org/axios/-/axios-0.24.0.tgz" resolved "https://registry.npmjs.org/axios/-/axios-0.24.0.tgz"
integrity sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA== integrity sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==