I'm just starting to learn Angular 2 and Firebase and have gotten stuck on a basic problem.
I'm using the Firebase createUserWithEmailAndPassword
method to register a new user account using HTML form data, which is stored in a custom login class and then through the method.
This works, but I can't work out how to transfer any error data I get in the error object out to my HTML template for display. I've added variables to the login class, but I cant access it inside the createUserWithEmailAndPassword
method.
I know this is probably something fundamental but any help would be really useful. Thanks!
LoginComponent.ts
import { Component } from "@angular/core";
import { Router } from "@angular/router";
import { Login } from "./login";
@Component({
selector: 'login-component',
templateUrl: 'app/login/login.component.html',
styleUrls:[
'app/login/login.component.css'
]
})
export class LoginComponent{
login = new Login('James', 'SuperSecret', false, '');
submitted = false;
onSubmit(){this.submitted= true};
active = true;
newUser(){
firebase.auth().createUserWithEmailAndPassword(this.login.email, this.login.pswd).catch(function(error) {
if(error){
console.log('The error code: ' + error.code + '\nThe error message' + error.message);
this.login.error = true;
this.login.errorMsg = error.message;
}else{
this.login.error=false;
this.login.errorMsg='';
}
});
}
}
LoginComponent.html
<div class="container">
<div [hidden]="submitted">
<h1>Login Form</h1>
<form *ngIf="active" (ngSubmit)="onSubmit()" #loginForm="ngForm">
<!-- Email Input -->
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" required
[(ngModel)]="login.email" name="email" #email="ngModel">
<div [hidden]="email.valid || email.pristine"
class="alert alert-danger">
Email is required
</div>
</div>
<!-- Password Input -->
<div class="form-group">
<label for="pswd">Password</label>
<input type="password" class="form-control" id="pswd" [(ngModel)]="login.pswd" name="pswd" required>
<!-- EXAMPLE ERROR DISPLAY -->
<div [hidden]="!login.error" class="alert alert-danger">{{login.errorMsg}}</div>
</div>
<!-- Submit Buttons -->
<button type="submit" class="btn btn-default" [disabled]="!loginForm.form.valid">Login</button>
<button type="button" class="btn btn-default" (click)="newUser()">Register</button>
</form>
</div>
</div>
Login.ts (Login class)
export class Login{
constructor(
public email: string,
public pswd: string,
public error: boolean,
public errorMsg: string
){}
}