I have a login function with different alerts and loaders.
I now upgraded to ionic 4 from ionic 3 and the alert and loader functions
don’t work like before. I tried to update my function, but I’m stuck at one point.
My function is:
async signIn() {
//// check to confirm the username and password fields are filled
if (this.username.value == "") {
let alert = await this.alertCtrl.create({
header: "ATTENTION",
subHeader: "Username field is empty",
buttons: ['OK']
});
return await alert.present();
} else
if (this.password.value == "") {
let alert = await this.alertCtrl.create({
header: "ATTENTION",
subHeader: "Password field is empty",
buttons: ['OK']
});
return await alert.present();
} else
{
var headers = new Headers();
headers.append("Accept", 'application/json');
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({
headers: headers
});
let data = {
username: this.username.value,
password: this.password.value
};
let loader = await this.loading.create({
message: 'Processing please wait',
});
return await loader.present().then(() => {
//alerts stop working here, because they are not inside an async function
//without the alerts the script works
this.http.post('localhost/login.php', data, options)
.pipe(
map(res => res.json()))
.subscribe(res => {
console.log(res)
loader.dismiss()
if (res == "Your Login success") {
let alert = await this.alertCtrl.create({
header: "CONGRATS",
subHeader: (res),
buttons: ['OK']
});
this.authService.login();
return await alert.present();
} else
{
let alert = await this.alertCtrl.create({
header: "ERROR",
subHeader: "Your Login Username or Password is invalid",
buttons: ['OK']
});
return await alert.present();
}
});
});
}
}
I hope someone can help me to solve this problem and help me to change my function.
Thx a lot.