import { Component } from '@angular/core' ; import { FormBuilder , FormGroup , Validators } from "@angular/forms" ; import { AlertController , Loading , LoadingController , NavController , NavParams } from 'ionic-angular' ; import 'rxjs/add/operator/first' ; import { AuthService } from './../../providers/auth.service' ; import { HomePage } from './../home/home' ; import { UserService } from './../../providers/user.service' ; import * as firebase from 'firebase/app' ; @ Component ( { selector : 'page-signup' , templateUrl : 'signup.html' } ) export class SignupPage { signupForm : FormGroup ; constructor ( public alertCtrl , public authService , public formBuilder , public loadingCtrl , public navCtrl , public navParams , public userService ) { let emailRegex = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i ; this . signupForm = this . formBuilder . group ( { name : [ '' , [ Validators . required , Validators . minLength ( 3 ) ] ] , username : [ '' , [ Validators . required , Validators . minLength ( 3 ) ] ] , email : [ '' , Validators . compose ( [ Validators . required , Validators . pattern ( emailRegex ) ] ) ] , password : [ '' , [ Validators . required , Validators . minLength ( 6 ) ] ] , } ) ; } onSubmit ( ) { let loading = this . showLoading ( ) ; let formUser = this . signupForm . value ; let username = formUser . username ; this . userService . userExists ( username ) . first ( ) . subscribe ( ( userExists ) => { if ( ! userExists ) { this . authService . createAuthUser ( { email : formUser . email , password : formUser . password } ) . then ( ( authUser ) => { delete formUser . password ; let uuid = authUser . uid ; this . userService . create ( formUser , uuid ) . then ( ( ) => { console . log ( 'Usuario cadastrado!' ) ; this . navCtrl . setRoot ( HomePage ) ; loading . dismiss ( ) ; } ) . catch ( ( error ) => { console . log ( error ) ; loading . dismiss ( ) ; this . showAlert ( error ) ; } ) ; } ) . catch ( ( error ) => { console . log ( error ) ; loading . dismiss ( ) ; this . showAlert ( error ) ; } ) ; } else { this . showAlert ( `O username ${ username } já está sendo usado em outra conta!` ) ; loading . dismiss ( ) ; } } ) ; } private showLoading ( ) { let loading = this . loadingCtrl . create ( { content : 'Please wait...' } ) ; loading . present ( ) ; return loading ; } private showAlert ( message ) { this . alertCtrl . create ( { message : message , buttons : [ 'Ok' ] } ) . present ( ) ; } }