Skip to content

Commit

Permalink
changes made to register method
Browse files Browse the repository at this point in the history
  • Loading branch information
valerioprds committed Nov 23, 2023
1 parent ab6dca9 commit 0e2c068
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 61 deletions.
3 changes: 2 additions & 1 deletion src/app/modules/modals/modals.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LoginModalComponent } from './login-modal/login-modal.component';
import { SendSolutionModalComponent } from './send-solution-modal/send-solution-modal.component';
import { RestrictedModalComponent } from './restricted-modal/restricted-modal.component';
import { FiltersModalComponent } from './filters-modal/filters-modal.component';
import { ReactiveFormsModule } from '@angular/forms';



Expand All @@ -17,7 +18,7 @@ import { FiltersModalComponent } from './filters-modal/filters-modal.component';
FiltersModalComponent
],
imports: [
CommonModule,
CommonModule, ReactiveFormsModule
],
exports[
RegisterModalComponent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

<div class="modal-header align-items-center justify-content-end pb-0">
<div class="close" data-bs-dismiss="modal" (click)="closeModal()" aria-label="Close">
<span></span>
<span></span>
</div>
</div>
<div class="d-flex justify-content-center">
<form class="d-flex flex-column just-content-center gap-3 col-12 col-md-5">
<div class=" d-flex justify-content-center">
<form [formGroup]="registerForm" class="d-flex flex-column just-content-center gap-3 col-12 col-md-5">
<div class="modal-body d-flex flex-column gap-3 pt-0">
<h2 class="modal-title">Registro</h2>
<div id="sing-up-form" class="d-flex gap-3 flex-column justify-content-start">
Expand All @@ -17,7 +16,7 @@ <h2 class="modal-title">Registro</h2>
</div>
</div>
<div class="modal-footer d-flex flex-column gap-3 mb-3">
<button type="submit" class="btn btn-primary w-100">Registro</button>
<button (click)="register()" type="submit" class="btn btn-primary w-100">Registro</button>
<h3><a (click)="openLoginModal()">¿Tienes cuenta?, acceder</a></h3>
</div>
</form>
Expand Down
67 changes: 52 additions & 15 deletions src/app/modules/modals/register-modal/register-modal.component.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
import { Component } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { LoginModalComponent } from '../login-modal/login-modal.component';

import { Component } from "@angular/core";
import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
import { LoginModalComponent } from "../login-modal/login-modal.component";
import { Validators, FormBuilder } from "@angular/forms";
import { AuthService } from "src/app/services/auth.service";
import { User } from "src/app/models/user.model";

@Component({
selector: 'app-register-modal',
templateUrl: './register-modal.component.html',
styleUrls: ['./register-modal.component.scss']
selector: "app-register-modal",
templateUrl: "./register-modal.component.html",
styleUrls: ["./register-modal.component.scss"],
})
export class RegisterModalComponent {
registerError: string = "";

registerForm = this.formBuilder.group({
dni: ["", Validators.required],
email: ["", Validators.required],
password: ["", Validators.required],
repeatpassword: ["", Validators.required],
});

constructor(private modalService: NgbModal) {}
constructor(
private modalService: NgbModal,
private formBuilder: FormBuilder,
private authService: AuthService
) {}

closeModal() {
this.modalService.dismissAll();
}

openLoginModal(){
this.closeModal();
this.modalService.open(LoginModalComponent, { centered : true, size : 'lg' })
}
register() {
console.log("Valores del formulario:", this.registerForm.value);
if (this.registerForm.valid) {
const user = new User(
this.registerForm.get("dni")?.value ?? "", // Usa '' como valor predeterminado
this.registerForm.get("email")?.value ?? "", // Usa '' como valor predeterminado
this.registerForm.get("password")?.value ?? "",
this.registerForm.get("repeatpassword")?.value ?? ""
);
console.log(user, 'from register-modal********')
this.authService.register(user).subscribe({
next: (userData) => {},
error: (errorData) => {
console.error(errorData);
this.registerError = errorData;
},
});
}
}
closeModal() {
this.modalService.dismissAll();
}

openLoginModal() {
this.closeModal();
this.modalService.open(LoginModalComponent, {
centered: true,
size: "lg",
});
}
}
80 changes: 39 additions & 41 deletions src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import moment from "moment";
import { Injectable } from "@angular/core";
import { HttpClient, HttpErrorResponse } from "@angular/common/http";
import { environment } from '../../environments/environment';
import { environment } from "../../environments/environment";
import { BehaviorSubject, Observable, catchError, map, throwError } from "rxjs";
import { User } from "../models/user.model";
import { Router } from "@angular/router";
Expand Down Expand Up @@ -51,48 +51,46 @@ export class AuthService {
})
); */


return this.http
.get<any>(`${environment.BACKEND_DUMMY_LOGIN}`)
.pipe(
map((authResult: any) => {
this.setLocalStorage(authResult); // Llama a setLocalStorage con el resultado de autenticación
console.log('from auth service login ', authResult);
return authResult; // Devuelve el resultado del registro
}),
catchError((error: HttpErrorResponse) => {
// Maneja el error aquí (muestra un mensaje de error)
console.log('porque da error', error)
return throwError(error);
})
);


return this.http.get<any>(`${environment.BACKEND_DUMMY_LOGIN}`).pipe(
map((authResult: any) => {
this.setLocalStorage(authResult); // Llama a setLocalStorage con el resultado de autenticación
console.log("from auth service login ", authResult);
return authResult; // Devuelve el resultado del registro
}),
catchError((error: HttpErrorResponse) => {
// Maneja el error aquí (muestra un mensaje de error)
console.log("porque da error", error);
return throwError(error);
})
);
}

register(user: User): Observable<void> {
// const userJSON = user
/* console.log('from auth service register', user)
return this.http
.post<void>(
`${environment.BACKEND_ITA_WIKI_BASE_URL}${environment.BACKEND_REGISTER}`,
user // transformar en JSON
)
.pipe(
map((authResult: any) => {
this.setLocalStorage(authResult); // Llama a setLocalStorage con el resultado de autenticación
console.log('from auth service ', authResult);
}),
catchError((error: HttpErrorResponse) => {
// Handle error here (show an error message)
return throwError(error);
})
); */
console.log('from auth service register', user);
//const userJSON = user;
console.log("from auth service register 11111", user);
return this.http
.post<void>(
`${environment.BACKEND_ITA_WIKI_BASE_URL}${environment.BACKEND_REGISTER}`,
user
)
.pipe(
map((authResult: any) => {
if (authResult && authResult.expiresIn) {
this.setLocalStorage(authResult);
console.log("from auth service register", authResult);
} else {
throw new Error('Invalid authentication result');
}
}),
catchError((error: HttpErrorResponse) => {
console.log("Error during registration", error);
return throwError(error);
})
);
}

// Simular la solicitud con datos de un archivo dummy
return this.http
// Simular la solicitud con datos de un archivo dummy
/* return this.http
.get<any>(`${environment.BACKEND_DUMMY_REGISTER}`)
.pipe(
map((authResult: any) => {
Expand All @@ -105,8 +103,8 @@ export class AuthService {
console.log('porque da error', error)
return throwError(error);
})
);
}
); */


private setLocalStorage(authResult: any) {
// Takes the JWT expiresIn value and add that number of seconds
Expand Down

0 comments on commit 0e2c068

Please sign in to comment.