I have an html file with an onRegisterSubmit() function to submit a form. My register.ts file looks like this... it adds the user from the submitted form in html and validates and authenticates the user.
onRegisterSubmit(){
const user = {
name: this.name,
username: this.username,
email: this.email,
password: this.password
}
//calling the validate service validate register function
//If the forms are not all filled
if(!this.validateService.validateRegister(user)){
//using flash messages
this.flashMessage.show('Please fill in all fields', {cssClass:
'alert-danger', timeout: 3000});
return false;
}
//validate email
if(!this.validateService.validateEmail(user.email)){
this.flashMessage.show('Please enter a valid email', {cssClass:
'alert-danger', timeout: 3000});
return false;
}
//register User/ talks to authservice/ Since its an observable, we have to
//subscribe to it
//Data is the return info received in users.js
this.authService.registerUser(user).subscribe(data => {
//Receive a true or false based on the add user function in users.js
if(data.success){
this.flashMessage.show('You are now Registered', {cssClass: 'alert-
success', timeout: 3000});
//This will navigate to log in route because registration success
this.router.navigate(['/login']);
} else {
this.flashMessage.show('Something went wrong', {cssClass: 'alert-
danger', timeout: 3000});
this.router.navigate(['/register']);
}
})
}
}
via TexasState Han
No comments:
Post a Comment