add jwt
This commit is contained in:
+8
-3
@@ -3,12 +3,16 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { StudentModule } from './student/student.module';
|
||||
import { StudentModule } from './alumno/student.module';
|
||||
import { UserModule } from './user/user.module';
|
||||
import { User } from './user/entities/user.entity';
|
||||
import { DetalleServicioModule } from './detalle_servicio/detalle_servicio.module';
|
||||
import { PeriodoModule } from './periodo/periodo.module';
|
||||
import { ServicioModule } from './servicio/servicio.module';
|
||||
import { Student } from './alumno/entities/student.entity';
|
||||
import { DetalleServicio } from './detalle_servicio/entities/detalle_servicio.entity';
|
||||
import { Periodo } from './periodo/entities/periodo.entity';
|
||||
import { Servicio } from './servicio/entities/servicio.entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -25,11 +29,12 @@ import { ServicioModule } from './servicio/servicio.module';
|
||||
username: configService.get<string>('DB_USER'),
|
||||
password: configService.get<string>('DB_PASSWORD'),
|
||||
database: configService.get<string>('DB_NAME'),
|
||||
entities: [User],
|
||||
synchronize: false,
|
||||
entities: [User,Student,DetalleServicio,Periodo,Servicio],
|
||||
synchronize: false,//Never change to true in production!
|
||||
}),
|
||||
}),
|
||||
UserModule,
|
||||
StudentModule,
|
||||
DetalleServicioModule,
|
||||
PeriodoModule,
|
||||
ServicioModule,
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { DetalleServicioController } from './detalle_servicio.controller';
|
||||
import { DetalleServicioService } from './detalle_servicio.service';
|
||||
|
||||
describe('DetalleServicioController', () => {
|
||||
let controller: DetalleServicioController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [DetalleServicioController],
|
||||
providers: [DetalleServicioService],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<DetalleServicioController>(DetalleServicioController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -1,18 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { DetalleServicioService } from './detalle_servicio.service';
|
||||
|
||||
describe('DetalleServicioService', () => {
|
||||
let service: DetalleServicioService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [DetalleServicioService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<DetalleServicioService>(DetalleServicioService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
+10
-1
@@ -1,11 +1,20 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
const configService = app.get(ConfigService);
|
||||
|
||||
app.useGlobalPipes(new ValidationPipe());
|
||||
app.enableCors();
|
||||
|
||||
app.enableCors(
|
||||
// {
|
||||
// origin: configService.get<string>('Front_URL'),
|
||||
// }
|
||||
);
|
||||
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { PeriodoController } from './periodo.controller';
|
||||
import { PeriodoService } from './periodo.service';
|
||||
|
||||
describe('PeriodoController', () => {
|
||||
let controller: PeriodoController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [PeriodoController],
|
||||
providers: [PeriodoService],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<PeriodoController>(PeriodoController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -1,18 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { PeriodoService } from './periodo.service';
|
||||
|
||||
describe('PeriodoService', () => {
|
||||
let service: PeriodoService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [PeriodoService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<PeriodoService>(PeriodoService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -1,20 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { ServicioController } from './servicio.controller';
|
||||
import { ServicioService } from './servicio.service';
|
||||
|
||||
describe('ServicioController', () => {
|
||||
let controller: ServicioController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [ServicioController],
|
||||
providers: [ServicioService],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<ServicioController>(ServicioController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -1,18 +0,0 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { ServicioService } from './servicio.service';
|
||||
|
||||
describe('ServicioService', () => {
|
||||
let service: ServicioService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [ServicioService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<ServicioService>(ServicioService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { UserService } from './user.service';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(private configService: ConfigService) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExpiration: false,
|
||||
secretOrKey: configService.get('JWT_SECRET')!,
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: any) {
|
||||
return { userId: payload.id, username: payload.usuario };
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,21 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Request } from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Controller('user')
|
||||
export class UserController {
|
||||
constructor(private readonly userService: UserService) {}
|
||||
constructor(private readonly userService: UserService) { }
|
||||
|
||||
@Post()
|
||||
Login(@Body() user: CreateUserDto) {
|
||||
return this.userService.Login(user);
|
||||
}
|
||||
|
||||
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@Get('/validate-token')
|
||||
getProfile(@Request() req) {
|
||||
return req.user;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,11 +3,15 @@ import { UserService } from './user.service';
|
||||
import { UserController } from './user.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { User } from './entities/user.entity';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { PassportModule } from '@nestjs/passport';
|
||||
import { JwtStrategy } from './jwt.strategy';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([User]),
|
||||
PassportModule.register({ defaultStrategy: 'jwt' }),
|
||||
JwtModule.registerAsync({
|
||||
inject: [ConfigService],
|
||||
useFactory: async (configService: ConfigService) => {
|
||||
@@ -19,11 +23,9 @@ import { JwtModule } from '@nestjs/jwt';
|
||||
},
|
||||
|
||||
}),
|
||||
|
||||
TypeOrmModule.forFeature([User])
|
||||
],
|
||||
controllers: [UserController],
|
||||
providers: [UserService],
|
||||
providers: [UserService,JwtStrategy],
|
||||
exports: [UserService],
|
||||
})
|
||||
export class UserModule { }
|
||||
|
||||
Reference in New Issue
Block a user