From 7c8ed6d4101f0b75777a1e8675f581bc75787a21 Mon Sep 17 00:00:00 2001 From: Giovanna Esmeralda Chavez Espino <79319097+gio-gigi@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:48:27 -0600 Subject: [PATCH 1/2] register user add --- backend/package-lock.json | 159 +++++++++++++++++- backend/package.json | 4 + backend/src/admin/admin.controller.ts | 6 +- backend/src/admin/admin.module.ts | 1 + backend/src/admin/admin.service.ts | 18 +- backend/src/admin/dto/create-admin.dto.ts | 7 +- backend/src/admin/dto/update-admin.dto.ts | 4 +- backend/src/admin/entities/admin.entity.ts | 4 + backend/src/app.module.ts | 11 +- .../auth-user/auth-user.controller.spec.ts | 20 +++ backend/src/auth-user/auth-user.controller.ts | 34 ++++ backend/src/auth-user/auth-user.module.ts | 9 + .../src/auth-user/auth-user.service.spec.ts | 18 ++ backend/src/auth-user/auth-user.service.ts | 63 +++++++ backend/src/auth-user/dto/login-user.dto.ts | 12 ++ backend/src/auth-user/dto/registrer.dto.ts | 26 +++ backend/src/user/dto/create-user.dto.ts | 8 +- backend/src/user/dto/update-user.dto.ts | 4 +- backend/src/user/user.controller.ts | 9 +- backend/src/user/user.module.ts | 5 +- backend/src/user/user.service.ts | 9 +- 21 files changed, 400 insertions(+), 31 deletions(-) create mode 100644 backend/src/auth-user/auth-user.controller.spec.ts create mode 100644 backend/src/auth-user/auth-user.controller.ts create mode 100644 backend/src/auth-user/auth-user.module.ts create mode 100644 backend/src/auth-user/auth-user.service.spec.ts create mode 100644 backend/src/auth-user/auth-user.service.ts create mode 100644 backend/src/auth-user/dto/login-user.dto.ts create mode 100644 backend/src/auth-user/dto/registrer.dto.ts diff --git a/backend/package-lock.json b/backend/package-lock.json index c32687d..9632b1b 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -11,9 +11,13 @@ "dependencies": { "@nestjs/common": "^10.0.0", "@nestjs/core": "^10.0.0", + "@nestjs/jwt": "^10.2.0", "@nestjs/mapped-types": "*", "@nestjs/platform-express": "^10.0.0", "@nestjs/typeorm": "^10.0.2", + "bcryptjs": "^2.4.3", + "class-transformer": "^0.5.1", + "class-validator": "^0.14.1", "dotenv": "^16.4.5", "mysql2": "^3.9.4", "reflect-metadata": "^0.2.0", @@ -1794,6 +1798,18 @@ } } }, + "node_modules/@nestjs/jwt": { + "version": "10.2.0", + "resolved": "https://registry.npmjs.org/@nestjs/jwt/-/jwt-10.2.0.tgz", + "integrity": "sha512-x8cG90SURkEiLOehNaN2aRlotxT0KZESUliOPKKnjWiyJOcWurkF3w345WOX0P4MgFzUjGoZ1Sy0aZnxeihT0g==", + "dependencies": { + "@types/jsonwebtoken": "9.0.5", + "jsonwebtoken": "9.0.2" + }, + "peerDependencies": { + "@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0" + } + }, "node_modules/@nestjs/mapped-types": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/@nestjs/mapped-types/-/mapped-types-2.0.5.tgz", @@ -2194,6 +2210,14 @@ "integrity": "sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==", "dev": true }, + "node_modules/@types/jsonwebtoken": { + "version": "9.0.5", + "resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-9.0.5.tgz", + "integrity": "sha512-VRLSGzik+Unrup6BsouBeHsf4d1hOEgYWTm/7Nmw1sXoN1+tRly/Gy/po3yeahnP4jfnQWWAhQAqcNfH7ngOkA==", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/methods": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/@types/methods/-/methods-1.1.4.tgz", @@ -2210,7 +2234,6 @@ "version": "20.12.7", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.7.tgz", "integrity": "sha512-wq0cICSkRLVaf3UGLMGItu/PtdY7oaXaI/RVU+xliKVOtRna3PRY57ZDfztpDL0n11vfymMUnXv8QwYCO7L1wg==", - "devOptional": true, "dependencies": { "undici-types": "~5.26.4" } @@ -2281,6 +2304,11 @@ "@types/superagent": "^8.1.0" } }, + "node_modules/@types/validator": { + "version": "13.11.9", + "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.11.9.tgz", + "integrity": "sha512-FCTsikRozryfayPuiI46QzH3fnrOoctTjvOYZkho9BTFLCOZ2rgZJHMOVgCOfttjPJcgOx52EpkY0CMfy87MIw==" + }, "node_modules/@types/yargs": { "version": "17.0.32", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.32.tgz", @@ -3019,6 +3047,11 @@ } ] }, + "node_modules/bcryptjs": { + "version": "2.4.3", + "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", + "integrity": "sha512-V/Hy/X9Vt7f3BbPJEi8BdVFMByHi+jNXrYkW3huaybV/kQ0KJg0Y6PkEMbn+zeT+i+SiKZ/HMqJGIIt4LZDqNQ==" + }, "node_modules/binary-extensions": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz", @@ -3189,6 +3222,11 @@ "ieee754": "^1.1.13" } }, + "node_modules/buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==" + }, "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -3353,6 +3391,21 @@ "integrity": "sha512-0TNiGstbQmCFwt4akjjBg5pLRTSyj/PkWQ1ZoO2zntmg9yLqSRxwEa4iCfQLGjqhiqBfOJa7W/E8wfGrTDmlZQ==", "dev": true }, + "node_modules/class-transformer": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz", + "integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==" + }, + "node_modules/class-validator": { + "version": "0.14.1", + "resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.14.1.tgz", + "integrity": "sha512-2VEG9JICxIqTpoK1eMzZqaV+u/EiwEJkMGzTrZf6sU/fwsnOITVgYJ8yojSy6CaXtO9V0Cc6ZQZ8h8m4UBuLwQ==", + "dependencies": { + "@types/validator": "^13.11.8", + "libphonenumber-js": "^1.10.53", + "validator": "^13.9.0" + } + }, "node_modules/cli-cursor": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-3.1.0.tgz", @@ -3929,6 +3982,14 @@ "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", "integrity": "sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==" }, + "node_modules/ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -6224,6 +6285,46 @@ "graceful-fs": "^4.1.6" } }, + "node_modules/jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", + "dependencies": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^7.5.4" + }, + "engines": { + "node": ">=12", + "npm": ">=6" + } + }, + "node_modules/jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "dependencies": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "dependencies": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "node_modules/keyv": { "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", @@ -6264,6 +6365,11 @@ "node": ">= 0.8.0" } }, + "node_modules/libphonenumber-js": { + "version": "1.10.61", + "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.10.61.tgz", + "integrity": "sha512-TsQsyzDttDvvzWNkbp/i0fVbzTGJIG0mUu/uNalIaRQEYeJxVQ/FPg+EJgSqfSXezREjM0V3RZ8cLVsKYhhw0Q==" + }, "node_modules/lines-and-columns": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", @@ -6300,6 +6406,36 @@ "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "dev": true }, + "node_modules/lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==" + }, + "node_modules/lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==" + }, + "node_modules/lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==" + }, + "node_modules/lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==" + }, + "node_modules/lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==" + }, + "node_modules/lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==" + }, "node_modules/lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", @@ -6312,6 +6448,11 @@ "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==", "dev": true }, + "node_modules/lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==" + }, "node_modules/log-symbols": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", @@ -7618,7 +7759,6 @@ "version": "7.6.0", "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.0.tgz", "integrity": "sha512-EnwXhrlwXMk9gKu5/flx5sv/an57AkRplG3hTK68W7FRDN+k+OWBj65M7719OkA82XLBxrcX0KSHj+X5COhOVg==", - "dev": true, "dependencies": { "lru-cache": "^6.0.0" }, @@ -7633,7 +7773,6 @@ "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", - "dev": true, "dependencies": { "yallist": "^4.0.0" }, @@ -7644,8 +7783,7 @@ "node_modules/semver/node_modules/yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", - "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", - "dev": true + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, "node_modules/send": { "version": "0.18.0", @@ -8772,8 +8910,7 @@ "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", - "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", - "devOptional": true + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==" }, "node_modules/universalify": { "version": "2.0.1", @@ -8876,6 +9013,14 @@ "node": ">=10.12.0" } }, + "node_modules/validator": { + "version": "13.11.0", + "resolved": "https://registry.npmjs.org/validator/-/validator-13.11.0.tgz", + "integrity": "sha512-Ii+sehpSfZy+At5nPdnyMhx78fEoPDkR2XW/zimHEL3MyGJQOCQ7WeP20jPYRz7ZCpcKLB21NxuXHF3bxjStBQ==", + "engines": { + "node": ">= 0.10" + } + }, "node_modules/vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", diff --git a/backend/package.json b/backend/package.json index 4a3449a..c42c9b7 100644 --- a/backend/package.json +++ b/backend/package.json @@ -22,9 +22,13 @@ "dependencies": { "@nestjs/common": "^10.0.0", "@nestjs/core": "^10.0.0", + "@nestjs/jwt": "^10.2.0", "@nestjs/mapped-types": "*", "@nestjs/platform-express": "^10.0.0", "@nestjs/typeorm": "^10.0.2", + "bcryptjs": "^2.4.3", + "class-transformer": "^0.5.1", + "class-validator": "^0.14.1", "dotenv": "^16.4.5", "mysql2": "^3.9.4", "reflect-metadata": "^0.2.0", diff --git a/backend/src/admin/admin.controller.ts b/backend/src/admin/admin.controller.ts index 367bac5..0d0350c 100644 --- a/backend/src/admin/admin.controller.ts +++ b/backend/src/admin/admin.controller.ts @@ -13,13 +13,9 @@ export class AdminController { } @Get() - findAll() { - return this.adminService.findAll(); - } - @Get(':id') findOne(@Param('id') id: string) { - return this.adminService.findOne(+id); + return this.adminService.findOne(id.toString()); } @Patch(':id') diff --git a/backend/src/admin/admin.module.ts b/backend/src/admin/admin.module.ts index 2926ee8..fdad709 100644 --- a/backend/src/admin/admin.module.ts +++ b/backend/src/admin/admin.module.ts @@ -5,5 +5,6 @@ import { AdminController } from './admin.controller'; @Module({ controllers: [AdminController], providers: [AdminService], + exports: [AdminService], }) export class AdminModule {} diff --git a/backend/src/admin/admin.service.ts b/backend/src/admin/admin.service.ts index f4dbcf1..4ef4d3a 100644 --- a/backend/src/admin/admin.service.ts +++ b/backend/src/admin/admin.service.ts @@ -4,16 +4,26 @@ import { UpdateAdminDto } from './dto/update-admin.dto'; @Injectable() export class AdminService { - create(createAdminDto: CreateAdminDto) { - return 'This action adds a new admin'; + private readonly admin = [ + { + id: 1, + name: 'Admin prueba', + email: 'admin@gmail.com', + password: 'admin123', + } + ]; + async create(createAdminDto: CreateAdminDto) { + return this.admin.push(createAdminDto); } + findAll() { return `This action returns all admin`; } - findOne(id: number) { - return `This action returns a #${id} admin`; + findOne(email: string) { + return this.admin.find((admin) => admin.email === email); + } update(id: number, updateAdminDto: UpdateAdminDto) { diff --git a/backend/src/admin/dto/create-admin.dto.ts b/backend/src/admin/dto/create-admin.dto.ts index 233a7df..81a9eaa 100644 --- a/backend/src/admin/dto/create-admin.dto.ts +++ b/backend/src/admin/dto/create-admin.dto.ts @@ -1 +1,6 @@ -export class CreateAdminDto {} +export class CreateAdminDto { + id : number; + name: string; + email: string; + password: string; +} diff --git a/backend/src/admin/dto/update-admin.dto.ts b/backend/src/admin/dto/update-admin.dto.ts index 74613de..3b54a6f 100644 --- a/backend/src/admin/dto/update-admin.dto.ts +++ b/backend/src/admin/dto/update-admin.dto.ts @@ -1,4 +1,6 @@ import { PartialType } from '@nestjs/mapped-types'; import { CreateAdminDto } from './create-admin.dto'; -export class UpdateAdminDto extends PartialType(CreateAdminDto) {} +export class UpdateAdminDto extends PartialType(CreateAdminDto) { + isActive: boolean; +} diff --git a/backend/src/admin/entities/admin.entity.ts b/backend/src/admin/entities/admin.entity.ts index 9f47fe0..73dc4f6 100644 --- a/backend/src/admin/entities/admin.entity.ts +++ b/backend/src/admin/entities/admin.entity.ts @@ -12,4 +12,8 @@ export class Admin { @Column() password: string + + @Column({ default: true }) + isActive: boolean } + diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index ed8a642..c288b31 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -7,6 +7,11 @@ import { UserModule } from './user/user.module'; import * as dotenv from 'dotenv'; import { Admin } from './admin/entities/admin.entity'; import { User } from './user/entities/user.entity'; +import { AuthModule } from './auth/auth.module'; +import { AuthService } from './auth/auth.service'; +import { JwtModule } from '@nestjs/jwt'; +import { AuthUserModule } from './auth-user/auth-user.module'; +import { AuthAdminModule } from './auth-admin/auth-admin.module'; dotenv.config() @Module({ imports: [ @@ -22,8 +27,12 @@ dotenv.config() }), AdminModule, UserModule, + AuthModule, + JwtModule, + AuthUserModule, + AuthAdminModule, ], controllers: [AppController], //rutas - providers: [AppService], //funciones para comunicarse en la bd + providers: [AppService, AuthService], //funciones para comunicarse en la bd }) export class AppModule {} diff --git a/backend/src/auth-user/auth-user.controller.spec.ts b/backend/src/auth-user/auth-user.controller.spec.ts new file mode 100644 index 0000000..660785f --- /dev/null +++ b/backend/src/auth-user/auth-user.controller.spec.ts @@ -0,0 +1,20 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { AuthUserController } from './auth-user.controller'; +import { AuthUserService } from './auth-user.service'; + +describe('AuthUserController', () => { + let controller: AuthUserController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [AuthUserController], + providers: [AuthUserService], + }).compile(); + + controller = module.get(AuthUserController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/backend/src/auth-user/auth-user.controller.ts b/backend/src/auth-user/auth-user.controller.ts new file mode 100644 index 0000000..824de47 --- /dev/null +++ b/backend/src/auth-user/auth-user.controller.ts @@ -0,0 +1,34 @@ +import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { AuthUserService } from './auth-user.service'; +import { CreateAuthUserDto } from './dto/registrer.dto'; +import { UpdateAuthUserDto } from './dto/login-user.dto'; + +@Controller('auth-user') +export class AuthUserController { + constructor(private readonly authUserService: AuthUserService) {} + + @Post() + create(@Body() createAuthUserDto: CreateAuthUserDto) { + return this.authUserService.create(createAuthUserDto); + } + + @Get() + findAll() { + return this.authUserService.findAll(); + } + + @Get(':id') + findOne(@Param('id') id: string) { + return this.authUserService.findOne(+id); + } + + @Patch(':id') + update(@Param('id') id: string, @Body() updateAuthUserDto: UpdateAuthUserDto) { + return this.authUserService.update(+id, updateAuthUserDto); + } + + @Delete(':id') + remove(@Param('id') id: string) { + return this.authUserService.remove(+id); + } +} diff --git a/backend/src/auth-user/auth-user.module.ts b/backend/src/auth-user/auth-user.module.ts new file mode 100644 index 0000000..f6884bb --- /dev/null +++ b/backend/src/auth-user/auth-user.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { AuthUserService } from './auth-user.service'; +import { AuthUserController } from './auth-user.controller'; + +@Module({ + controllers: [AuthUserController], + providers: [AuthUserService], +}) +export class AuthUserModule {} diff --git a/backend/src/auth-user/auth-user.service.spec.ts b/backend/src/auth-user/auth-user.service.spec.ts new file mode 100644 index 0000000..044f25d --- /dev/null +++ b/backend/src/auth-user/auth-user.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { AuthUserService } from './auth-user.service'; + +describe('AuthUserService', () => { + let service: AuthUserService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [AuthUserService], + }).compile(); + + service = module.get(AuthUserService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/backend/src/auth-user/auth-user.service.ts b/backend/src/auth-user/auth-user.service.ts new file mode 100644 index 0000000..6edd747 --- /dev/null +++ b/backend/src/auth-user/auth-user.service.ts @@ -0,0 +1,63 @@ +import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { RegisterDto } from './dto/registrer.dto'; +import { LoginUserDto } from './dto/login-user.dto'; +import { UserService } from 'src/user/user.service'; +import { JwtService } from '@nestjs/jwt'; +import { CreateUserDto } from 'src/user/dto/create-user.dto'; +import * as bcryptjs from "bcryptjs"; + +@Injectable() +export class AuthUserService { + constructor(private readonly userService: UserService, + ) {} + + async register({ name, lastname, email, phone, password }: RegisterDto) { + const hashedPassword = await bcryptjs.hash(password, 10); + const userExists = await this.userService.findOne(email); + if (userExists) { + throw new UnauthorizedException('User already exists'); + }else{ + const user = await this.userService.create({ + name, + lastname, + email, + phone, + password: hashedPassword, + }); + return user; + } + + + } + + /* async login({ email, password }: LoginUserDto) { + const user = await this.userService.findOne(email); + if (!user) { + throw new Error('User not found'); + } + const isPasswordValid = await bcryptjs.compare(password, user.password); + if (!isPasswordValid) { + throw new Error('Invalid password'); + } + return user; + }*/ + create(createAuthUserDto: CreateUserDto) { + return 'This action adds a new authUser'; + } + + findAll() { + return `This action returns all authUser`; + } + + findOne(id: number) { + return `This action returns a #${id} authUser`; + } + + update(id: number, registrerUser: RegisterDto) { + return `This action updates a #${id} authUser`; + } + + remove(id: number) { + return `This action removes a #${id} authUser`; + } +} diff --git a/backend/src/auth-user/dto/login-user.dto.ts b/backend/src/auth-user/dto/login-user.dto.ts new file mode 100644 index 0000000..8ecfcd0 --- /dev/null +++ b/backend/src/auth-user/dto/login-user.dto.ts @@ -0,0 +1,12 @@ +import { Transform } from "class-transformer"; +import { IsEmail, IsString, MinLength } from "class-validator"; + +export class LoginUserDto { + @IsEmail() + email: string; + + @IsString() + @MinLength(6) + @Transform(({ value }) => value.trim()) + password: string; +} diff --git a/backend/src/auth-user/dto/registrer.dto.ts b/backend/src/auth-user/dto/registrer.dto.ts new file mode 100644 index 0000000..24bc86a --- /dev/null +++ b/backend/src/auth-user/dto/registrer.dto.ts @@ -0,0 +1,26 @@ +import { Transform } from "class-transformer"; +import { IsEmail, IsNumber, IsString, MinLength } from "class-validator"; + +export class RegisterDto { + @IsString() + @MinLength(1) + name: string; + + @IsString() + @MinLength(1) + lastname: string; + + @IsEmail() + email: string; + + @IsNumber() + @MinLength(1) + phone: number; + + @IsString() + @MinLength(6) + @Transform(({ value }) => value.trim()) + password: string; + + +} diff --git a/backend/src/user/dto/create-user.dto.ts b/backend/src/user/dto/create-user.dto.ts index 0311be1..b9f5a75 100644 --- a/backend/src/user/dto/create-user.dto.ts +++ b/backend/src/user/dto/create-user.dto.ts @@ -1 +1,7 @@ -export class CreateUserDto {} +export class CreateUserDto { + name: string; + lastname: string; + password: string; + email: string; + phone: number; +} diff --git a/backend/src/user/dto/update-user.dto.ts b/backend/src/user/dto/update-user.dto.ts index dfd37fb..8211c30 100644 --- a/backend/src/user/dto/update-user.dto.ts +++ b/backend/src/user/dto/update-user.dto.ts @@ -1,4 +1,6 @@ import { PartialType } from '@nestjs/mapped-types'; import { CreateUserDto } from './create-user.dto'; -export class UpdateUserDto extends PartialType(CreateUserDto) {} +export class UpdateUserDto extends PartialType(CreateUserDto) { + +} diff --git a/backend/src/user/user.controller.ts b/backend/src/user/user.controller.ts index 995519c..cbc95ae 100644 --- a/backend/src/user/user.controller.ts +++ b/backend/src/user/user.controller.ts @@ -13,13 +13,9 @@ export class UserController { } @Get() - findAll() { - return this.userService.findAll(); - } - @Get(':id') findOne(@Param('id') id: string) { - return this.userService.findOne(+id); + return this.userService.findOne(id.toString()); } @Patch(':id') @@ -29,6 +25,7 @@ export class UserController { @Delete(':id') remove(@Param('id') id: string) { - return this.userService.remove(+id); + + return this.userService.remove(+id); } } diff --git a/backend/src/user/user.module.ts b/backend/src/user/user.module.ts index e21d51f..c20f6ad 100644 --- a/backend/src/user/user.module.ts +++ b/backend/src/user/user.module.ts @@ -1,9 +1,12 @@ import { Module } from '@nestjs/common'; import { UserService } from './user.service'; import { UserController } from './user.controller'; - +import { TypeOrmModule } from '@nestjs/typeorm'; +import { User } from './entities/user.entity'; @Module({ + imports: [TypeOrmModule.forFeature([User])], controllers: [UserController], providers: [UserService], + exports: [UserService], }) export class UserModule {} diff --git a/backend/src/user/user.service.ts b/backend/src/user/user.service.ts index 95707d7..5a3f9fe 100644 --- a/backend/src/user/user.service.ts +++ b/backend/src/user/user.service.ts @@ -5,15 +5,15 @@ import { UpdateUserDto } from './dto/update-user.dto'; @Injectable() export class UserService { create(createUserDto: CreateUserDto) { - return 'This action adds a new user'; + } findAll() { return `This action returns all user`; } - findOne(id: number) { - return `This action returns a #${id} user`; + findOne(email: string) { + return `This action returns a #${email} user`; } update(id: number, updateUserDto: UpdateUserDto) { @@ -23,4 +23,7 @@ export class UserService { remove(id: number) { return `This action removes a #${id} user`; } + save(newUser: any) { + throw new Error('Method not implemented.'); + } } -- GitLab From 18b4e08cf7141c30ea3ad8bee78194d2831ed7d7 Mon Sep 17 00:00:00 2001 From: Giovanna Esmeralda Chavez Espino <79319097+gio-gigi@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:50:02 -0600 Subject: [PATCH 2/2] register user add --- backend/src/auth-user/entities/auth-user.entity.ts | 1 + 1 file changed, 1 insertion(+) create mode 100644 backend/src/auth-user/entities/auth-user.entity.ts diff --git a/backend/src/auth-user/entities/auth-user.entity.ts b/backend/src/auth-user/entities/auth-user.entity.ts new file mode 100644 index 0000000..336160d --- /dev/null +++ b/backend/src/auth-user/entities/auth-user.entity.ts @@ -0,0 +1 @@ +export class AuthUser {} -- GitLab