Last active
April 4, 2021 08:08
-
-
Save KrauserHuang/c03546c618a3e1d5f77b9727e5ca184b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBAction func signUpPressed(_ sender: UIButton) { | |
// Validate the field, 先對textField做判斷 | |
let error = validateTextField() | |
if error != nil { | |
popAlert(title: "Error occured", message: error!, alertTitle: "OK") { (action) in | |
self.emailTextField.text = "" | |
self.passwordTextField.text = "" | |
self.repasswordTextField.text = "" | |
self.userNameTextField.text = "" | |
} | |
} else { | |
// 將textField輸入的文字存入常數中 | |
if let email = emailTextField.text, | |
let password = passwordTextField.text, | |
let userName = userNameTextField.text { | |
// 建立user | |
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in | |
if let error = error { | |
self.popAlert(title: "Error occured", message: error.localizedDescription, alertTitle: "OK") { (action) in | |
self.emailTextField.text = "" | |
self.passwordTextField.text = "" | |
self.repasswordTextField.text = "" | |
self.userNameTextField.text = "" | |
} | |
} else { | |
self.db.collection(Constant.FireStore.users).addDocument(data: [Constant.FireStore.userName: userName, Constant.FireStore.uid: authResult!.user.uid]) { (error) in | |
if let error = error { | |
print(error) | |
} else { | |
if let currentUser = Auth.auth().currentUser?.createProfileChangeRequest() { | |
currentUser.displayName = userName | |
currentUser.commitChanges { (error) in | |
if let error = error { | |
print(error) | |
} else { | |
if let user = Auth.auth().currentUser { | |
self.uid = user.uid | |
self.performSegue(withIdentifier: Constant.Segue.signupSegue, sender: self) | |
print("sign up success!") | |
print(currentUser.displayName!) | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment