Skip to content

Instantly share code, notes, and snippets.

@rabimba
Created October 19, 2016 03:27
Show Gist options
  • Save rabimba/c82e2175d2e3722d9f0d910083fb7e2e to your computer and use it in GitHub Desktop.
Save rabimba/c82e2175d2e3722d9f0d910083fb7e2e to your computer and use it in GitHub Desktop.
For Comp446
override func viewDidAppear(_ animated: Bool) {
if let user = FIRAuth.auth()?.currentUser {
self.signedIn(user)
}
}
@IBAction func didTapSignIn(_ sender: AnyObject) {
// Sign In with credentials.
guard let email = emailField.text, let password = passwordField.text else { return }
FIRAuth.auth()?.signIn(withEmail: email, password: password) { (user, error) in
if let error = error {
print(error.localizedDescription)
return
}
self.signedIn(user!)
}
}
@IBAction func didTapSignUp(_ sender: AnyObject) {
guard let email = emailField.text, let password = passwordField.text else { return }
FIRAuth.auth()?.createUser(withEmail: email, password: password) { (user, error) in
if let error = error {
print(error.localizedDescription)
return
}
self.setDisplayName(user!)
}
}
func setDisplayName(_ user: FIRUser) {
let changeRequest = user.profileChangeRequest()
changeRequest.displayName = user.email!.components(separatedBy: "@")[0]
changeRequest.commitChanges(){ (error) in
if let error = error {
print(error.localizedDescription)
return
}
self.signedIn(FIRAuth.auth()?.currentUser)
}
}
@IBAction func didRequestPasswordReset(_ sender: AnyObject) {
let prompt = UIAlertController.init(title: nil, message: "Email:", preferredStyle: .alert)
let okAction = UIAlertAction.init(title: "OK", style: .default) { (action) in
let userInput = prompt.textFields![0].text
if (userInput!.isEmpty) {
return
}
FIRAuth.auth()?.sendPasswordReset(withEmail: userInput!) { (error) in
if let error = error {
print(error.localizedDescription)
return
}
}
}
prompt.addTextField(configurationHandler: nil)
prompt.addAction(okAction)
present(prompt, animated: true, completion: nil);
}
func signedIn(_ user: FIRUser?) {
MeasurementHelper.sendLoginEvent()
AppState.sharedInstance.displayName = user?.displayName ?? user?.email
AppState.sharedInstance.photoURL = user?.photoURL
AppState.sharedInstance.signedIn = true
let notificationName = Notification.Name(rawValue: Constants.NotificationKeys.SignedIn)
NotificationCenter.default.post(name: notificationName, object: nil, userInfo: nil)
performSegue(withIdentifier: Constants.Segues.SignInToFp, sender: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment