diff --git a/package-lock.json b/package-lock.json index b1f5c55..51ece84 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "@nestjs/passport": "^11.0.5", "@nestjs/platform-express": "^11.0.1", "@nestjs/typeorm": "^11.0.0", + "bcrypt": "^6.0.0", "mysql2": "^3.15.2", "passport": "^0.7.0", "passport-google-oauth20": "^2.0.0", @@ -30,6 +31,7 @@ "@nestjs/cli": "^11.0.0", "@nestjs/schematics": "^11.0.0", "@nestjs/testing": "^11.0.1", + "@types/bcrypt": "^6.0.0", "@types/express": "^5.0.0", "@types/jest": "^30.0.0", "@types/node": "^22.10.7", @@ -2663,6 +2665,15 @@ "@babel/types": "^7.28.2" } }, + "node_modules/@types/bcrypt": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/@types/bcrypt/-/bcrypt-6.0.0.tgz", + "integrity": "sha512-/oJGukuH3D2+D+3H4JWLaAsJ/ji86dhRidzZ/Od7H/i8g+aCmvkeCc6Ni/f9uxGLSQVCRZkX2/lqEFG2BvWtlQ==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/body-parser": { "version": "1.19.6", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.6.tgz", @@ -4002,6 +4013,19 @@ "baseline-browser-mapping": "dist/cli.js" } }, + "node_modules/bcrypt": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-6.0.0.tgz", + "integrity": "sha512-cU8v/EGSrnH+HnxV2z0J7/blxH8gq7Xh2JFT6Aroax7UohdmiJJlxApMxtKfuI7z68NvvVcmR78k2LbT6efhRg==", + "hasInstallScript": true, + "dependencies": { + "node-addon-api": "^8.3.0", + "node-gyp-build": "^4.8.4" + }, + "engines": { + "node": ">= 18" + } + }, "node_modules/bl": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", @@ -7660,6 +7684,14 @@ "integrity": "sha512-AGK2yQKIjRuqnc6VkX2Xj5d+QW8xZ87pa1UK6yA6ouUyuxfHuMP6umE5QK7UmTeOAymo+Zx1Fxiuw9rVx8taHQ==", "dev": true }, + "node_modules/node-addon-api": { + "version": "8.5.0", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-8.5.0.tgz", + "integrity": "sha512-/bRZty2mXUIFY/xU5HLvveNHlswNJej+RnxBjOMkidWfwZzgTbPG1E3K5TOxRLOR+5hX7bSofy8yf1hZevMS8A==", + "engines": { + "node": "^18 || ^20 || >= 21" + } + }, "node_modules/node-emoji": { "version": "1.11.0", "resolved": "https://registry.npmjs.org/node-emoji/-/node-emoji-1.11.0.tgz", @@ -7669,6 +7701,16 @@ "lodash": "^4.17.21" } }, + "node_modules/node-gyp-build": { + "version": "4.8.4", + "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.4.tgz", + "integrity": "sha512-LA4ZjwlnUblHVgq0oBF3Jl/6h/Nvs5fzBLwdEF4nuxnFdsfajde4WfxtJr3CaiH+F6ewcIB/q4jQ4UzPyid+CQ==", + "bin": { + "node-gyp-build": "bin.js", + "node-gyp-build-optional": "optional.js", + "node-gyp-build-test": "build-test.js" + } + }, "node_modules/node-int64": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz", diff --git a/package.json b/package.json index b562b06..b1078c0 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@nestjs/passport": "^11.0.5", "@nestjs/platform-express": "^11.0.1", "@nestjs/typeorm": "^11.0.0", + "bcrypt": "^6.0.0", "mysql2": "^3.15.2", "passport": "^0.7.0", "passport-google-oauth20": "^2.0.0", @@ -41,6 +42,7 @@ "@nestjs/cli": "^11.0.0", "@nestjs/schematics": "^11.0.0", "@nestjs/testing": "^11.0.1", + "@types/bcrypt": "^6.0.0", "@types/express": "^5.0.0", "@types/jest": "^30.0.0", "@types/node": "^22.10.7", diff --git a/src/persona/password.ts b/src/persona/password.ts new file mode 100644 index 0000000..08137ba --- /dev/null +++ b/src/persona/password.ts @@ -0,0 +1,10 @@ +export default function passwordRand(length: number): string { + const characters = + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + let pass = ''; + for (let i = 0; i < length; i++) { + pass += characters.charAt(Math.floor(Math.random() * characters.length)); + } + + return pass; +} diff --git a/src/persona/persona.service.ts b/src/persona/persona.service.ts index 19dc39a..75d1fa1 100644 --- a/src/persona/persona.service.ts +++ b/src/persona/persona.service.ts @@ -3,6 +3,9 @@ import { JwtService } from '@nestjs/jwt'; import { Repository } from 'typeorm'; import { InjectRepository } from '@nestjs/typeorm'; import { Persona } from './entities/persona.entity'; +import * as bcrypt from 'bcrypt'; + +import passwordRand from './password'; @Injectable() export class PersonaService { constructor( @@ -36,7 +39,7 @@ export class PersonaService { let persona = await this.findByEmail(data.email); if (!persona) { - const apellidoArray = data.apellido.trim().split(' '); + const apellidoArray = data.apellido.split(' '); const apellidoP = data.apellido[0] || ''; const apellidoM = data.apellido[1] || ''; const nombre = apellidoArray.slice(2).join(' ') || ''; @@ -44,19 +47,21 @@ export class PersonaService { // NĂºmero identificador del correo const numeroIdentificar = data.email.split('@')[0]; + const password = await passwordRand(12); + let hash = await bcrypt.hashSync(password, 13); + persona = await this.create({ nombre: nombre || '', apellidoP: apellidoP || '', apellidoM: apellidoM || '', - password: '', - passwordTem: '', + password: hash, fechaPasswordTem: new Date(), correo: data.email, - carreraAds: '', + carreraAds: 'Mac', cantidadCuenta: 0, - usuario: '', + usuario: numeroIdentificar || '', numeroIdentificar: numeroIdentificar || '', - tipoUsuario: '', + tipoUsuario: 'Alumno', correoEnviado: false, fechaRegistro: new Date(), cambioPasswordReq: true,