You are on page 1of 5

import { Component, OnInit } from '@angular/core';

import { Usuario } from 'src/app/usuario';


import { Router, NavigationEnd} from '@angular/router';
import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from
'@angular/forms';

import { OnExit } from './../../guardianes/exit.guard';


import Swal from 'sweetalert2';
import { UsuarioAuthService } from './../../services/usuario-auth.service';
//import { Auth } from '@angular/fire/auth';

@Component({
selector: 'app-registro',
templateUrl: './registro.component.html',
styleUrls: ['./registro.component.css']
})
export class RegistroComponent implements OnInit, OnExit {

miUsuario:Usuario;

/*claveAux:String;

constructor(private router:Router) {
this.miUsuario=new Usuario();
//this.claveAux="";

/*VerificaClave ():void{

if (this.miUsuario.mail != null && this.miUsuario.password != null)


{

if(this.miUsuario.password==this.claveAux)
{
this.miUsuario.registrar();

this.router.navigate(['login']);
}
else{
alert("Las claves no son iguales, verificalas")
this.router.navigate(['registro']);
}
}
else {
alert ("Todos los campos son obligatorios");
}
}

Registrar()
{
console.log (this.miUsuario.nombre);
console.log (this.miUsuario.password);
console.log (this.miUsuario.mail) // console info me muestra todos los
atributos
// console error para ver que estamos mostrando
if (this.miUsuario.nombre == "admin" && this.miUsuario.password == "124")
{
this.router.navigate (['bienvenido']);
}
else{
this.router.navigate (['registro']);
}
}*/

public contactForm: FormGroup;

// tslint:disable-next-line: max-line-length
private emailPattern: any = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]
+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-
9]+\.)+[a-zA-Z]{2,}))$/;

constructor(private router:Router, private fb: FormBuilder, private


miServiceUsuario:UsuarioAuthService ) {
this.miUsuario = new Usuario();
// this.contactForm = new FormGroup({
// email: new FormControl(),
// password: new FormControl()
// });

ngOnInit(): void {
this.contactForm = this.fb.group({
'nombre': ['', [Validators.required, this.spacesValidator]],
'email': ['', [Validators.required, Validators.email]],
'password': ['', Validators.required],
'passwordRep': ['', Validators.required]
});
}

VerificaClave (){
this.miServiceUsuario.registrarUsuario(this.contactForm.value);
}

//VerificaClave (){
// if(this.contactForm.valid){
// if(this.contactForm.get('password').value ==
this.contactForm.get('passwordRep').value)
// {
// this.miUsuario.nombre=this.contactForm.get('nombre').value;
// this.miUsuario.mail=this.contactForm.get('email').value;
// this.miUsuario.password=this.contactForm.get('password').value;

// this.miUsuario.registrar();

// this.router.navigate(['login']);
// }
// else{
// alert("Las claves no son iguales, verificalas")
// this.router.navigate(['registro']);
// }
// }

//}

onExit(){
if (this.contactForm.valid){
location.href = 'login';
}
if(this.contactForm.dirty){
Swal.fire({
title: 'Seguro que desea Salir?',
text: "Estabas completando el registro",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Aceptar',
cancelButtonText: "Cancelar",
}).then((result) => {
if(result.value){
Swal.fire(
'SALIDA ACEPTADA',
'TE REDIRIGIMOS',
'success',
).then(function () {
location.href= 'bienvenido'
// this.router.navigate(['login'])
})
}else if (result.dismiss === Swal.DismissReason.cancel) {
Swal.fire(
'Cancelado',
'podes seguir completando el form',
'error'
)
}
})
return false;
}else{
return true;
}
}

private spacesValidator(control: AbstractControl): null


| object {
const nombre = <string>control.value;
const spaces = nombre.includes(' ');

return spaces
? { containsSpaces: true }
: null;
}

--------------------login---------------------------------------------------------
import { Component, OnInit } from '@angular/core';
import { Usuario } from 'src/app/usuario';
import { Router, NavigationEnd } from '@angular/router';
import { collection } from 'firebase/firestore';
import { StorageChatService } from './../../services/storage-chat.service';

@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
})
export class LoginComponent implements OnInit
{
//public collection;
miUsuario: Usuario;

constructor(private router: Router) //public miservicio:StorageChatService


{
this.miUsuario = new Usuario();
//this.miservicio.traerColeccion().then(e=>this.collection);

Mostrar()
{
console.log(this.miUsuario.nombre);
console.log(this.miUsuario.password); // console info me muestra todos los
atributos
// console error para ver que estamos mostrando
if (this.miUsuario.nombre == 'admin' && this.miUsuario.password == '124') {
this.router.navigate(['bienvenido']);
}
else
{
this.router.navigate(['registro']);
}
}

ngOnInit(): void {}

validaLogin():void{
if(typeof localStorage.getItem("users")!=null){
var users = JSON.parse(localStorage.getItem("users"))
}
let encontrado = 0;
for(let i in users ){
//window.alert(users[i]["mail"]+ " " +this.miUsuario.mail + " "+
this.miUsuario.password);
if(users[i]["mail"] == this.miUsuario.mail){
encontrado = 1;
if(users[i]["password"] == this.miUsuario.password){
//window.alert("Bienvenido " + users[i]["nombre"]);
this.miUsuario.guardaJugador();
this.router.navigate(["inicio"]);
break;
}
else{
this.router.navigate(['registro']);
}
}
}
if(!encontrado){
//window.alert("Usuario no existe");
this.router.navigate(["registro"]);
}

onSetDefault(){
this.miUsuario.nombre="Tester";
this.miUsuario.mail="prueba@tester";
this.miUsuario.password="123";
}

You might also like