从'@ angular / core'导入{Component,OnInit};导入{ “ @ angular / fire / auth”中的AngularFireAuth}从“ firebase / app”中导入{auth}
从'@ ionic / angular'导入{AlertController}导入{Router} 来自“ @ angular /路由器”
enter code here
@Component({
selector: 'app-register',
templateUrl: './register.page.html',
styleUrls: ['./register.page.scss'],
})
export class RegisterPage implements OnInit {
username: string= ''
password: string= ''
cpassword: string= ''
constructor(public afAuth: AngularFireAuth, public alert: AlertController, public router: Router ) { }
ngOnInit() {
}
async register() {
const {username, password, cpassword } = this
if(password != cpassword) {
this.showAlert("Error!"," Password don't match")
return console.error("Password don't match")
}
try {
// kind of a hack.
const res = await this.afAuth.auth.createUserWithEmailAndPassword(username , password)
console.log(res)
this.showAlert("Success","Welcome")
this.router.navigate([ '/login'])
}
catch(error) {
console.dir(error)
this.showAlert("Error!",error.message)
}
}
async showAlert(header: string, message: string) {
const alert = await this.alert.create( {
header, message, buttons: ["Ok"]
})
await alert.present()
}
}