I'm making a website with angular, for this I need a login and a page to register new users, I'm using a service to save users, in this service I have an arrangement with users, the problem is that when I add a new user through the page and then I go to the login, the new user added does not work, that is as if I had never registered it.
here the service code:
import { Injectable } from '@angular/core';
import { usuario } from './app.component';
@Injectable({
providedIn: 'root'
})
export class UserListService {
constructor() {
}
//cuenta del administrador
admin = new usuario("admin","[email protected]","admin");
// arreglo donde se guardan los usuarios
usuarios = [this.admin];
//metodo para agregar usuarios nuevos
agregar(usuario){
this.usuarios.splice(1,0,usuario);
}
}
here the code of the registry:
import { Component, OnInit } from '@angular/core';
import { usuario } from '../app.component';
import { UserListService } from '../user-list.service'
@Component({
selector: 'app-registrar',
templateUrl: './registrar.component.html',
styleUrls: ['./registrar.component.css']
})
export class RegistrarComponent implements OnInit {
constructor(
public user:UserListService
) { }
nuevo;
ngOnInit() {
}
//metodo para registrar un usuario nuevo
registrar(id,email,password,password2){
if(password == password2){
this.nuevo = new usuario(id,email,password);
this.user.agregar(this.nuevo);
}
}
}
here the login code:
import { Component, OnInit } from '@angular/core';
import {Router} from '@angular/router';
import { usuario } from '../app.component';
import { UserListService } from '../user-list.service'
@Component({
selector: 'app-iniciar-sesion',
templateUrl: './iniciar-sesion.component.html',
styleUrls: ['./iniciar-sesion.component.css']
})
export class IniciarSesionComponent implements OnInit {
constructor(private router:Router,
public user: UserListService) {
}
ngOnInit() {
}
// arreglo que guarda los usuarios registrados
usuarios = this.user.usuarios;
// metodo para el inicio de sesion
redireccion(contra,email){
// se comprueba que si se hallan insertado el email y la contraseña
if(contra != null && contra != "" && email!= null && email != ""){
for(let i of this.usuarios){
// se mira que el usuario ingresado si sea el correcto
if(email == i.email && contra == i.password){
this.router.navigate(["ver"]);
}
}
}
}
}