-2
I’m starting now with the angular and I’m having a doubt, I made a canvas of login.page.html, after the back-end confirms the login and password the user goes to the screen home.page.html on this screen the user will have access to a query menu, each user will make his query with his login/password, but the angle by which I saw works with classes and if I set the login and password in loginpage class and instantiate that class in the class Homepage the value of var login and var password if you lose, I read that there are several ways to do injection, service but I could not apply or understand any of these concepts, someone to explain me?
import { Component, Inject, OnInit } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { ActivatedRoute, Router } from '@angular/router';
import { AlertController, MenuController } from '@ionic/angular';
@Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
public folder: string;
public login: string;
public senha: string;
constructor(
//private activatedRoute: ActivatedRoute,
private http: HttpClient,
private alertController: AlertController,
private menu: MenuController,
private router: Router
){
}
async ngOnInit() {
}
async alert(mensagem: string){
const alert = this.alertController.create({
header: 'Aviso', message: mensagem,buttons: ['Ok']
});
(await alert).present();
}
public conectar() {
let postData = { "log_login": this.login, "log_senha": this.senha };
this.http.post<any>( // Requisição Ajax
"http://localhost/appBTR/conectar.php", // URL
JSON.stringify(postData)) // Parametros
.subscribe( // Retorno
async data => { // Caso success
if( data == 0){ // Login e/ou Senha inválidos!
this.alert("Login e/ou Senha inválidos!"); // Aviso na tela
}
else{ this.telaInicio(postData); } // Caso login ok
},
async (error: HttpErrorResponse) => { // Caso error
this.alert(error.error.text);
}
);
}
telaInicio(postData: object){
// AQUI EU PRECISO DAR UM JEITO DE OU ENVIAR O posData para a home.page.ts
// OU TRANSFORMA-LO EM GLOBAL
this.menu.enable(true,'main-menu');
this.router.navigate(['home']);
}
}
I don’t know if you are taking a course (I recommend), or learning on your own, but this part of login goes much deeper in a real application. Generally (if not totally) the user data (login, password, etc...) is stored in the user session generating an authentication token, jwt and all that stuff, then when you need this data you get it at the front by the API, security issue.
– LeAndrade
Who negative could say in words what is wrong in the question?
– SneepS NinjA