Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import FirebaseAuth
- class SignInViewController: UIViewController {
- @IBOutlet weak var emailField: UITextField!
- @IBOutlet weak var passwordField: UITextField!
- override func viewDidAppear(animated: Bool) {
- if let user = FIRAuth.auth()?.currentUser { //error here
- self.signedIn(user)
- }
- }
- @IBAction func didTapSignIn(sender: AnyObject) {
- // Sign In with credentials.
- let email = emailField.text
- let password = passwordField.text
- FIRAuth.auth()?.signInWithEmail(email!, password: password!) { //error here (user, error) in
- if let error = error {
- print(error.localizedDescription)
- return
- }
- self.signedIn(user!)
- }
- }
- @IBAction func didTapSignUp(sender: AnyObject) {
- let email = emailField.text
- let password = passwordField.text
- FIRAuth.auth()?.createUserWithEmail(email!, password: password!) { // error here(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!.componentsSeparatedByString("@")[0]
- changeRequest.commitChangesWithCompletion(){ (error) in
- if let error = error {
- print(error.localizedDescription)
- return
- }
- self.signedIn(FIRAuth.auth()?.currentUser) //error here
- }
- }
- @IBAction func didRequestPasswordReset(sender: AnyObject) {
- let prompt = UIAlertController.init(title: nil, message: "Email:", preferredStyle: UIAlertControllerStyle.Alert)
- let okAction = UIAlertAction.init(title: "OK", style: UIAlertActionStyle.Default) { (action) in
- let userInput = prompt.textFields![0].text
- if (userInput!.isEmpty) {
- return
- }
- FIRAuth.auth()?.sendPasswordResetWithEmail(userInput!) { //error here (error) in
- if let error = error {
- print(error.localizedDescription)
- return
- }
- }
- }
- prompt.addTextFieldWithConfigurationHandler(nil)
- prompt.addAction(okAction)
- presentViewController(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
- NSNotificationCenter.defaultCenter().postNotificationName(Constants.NotificationKeys.SignedIn, object: nil, userInfo: nil)
- // performSegueWithIdentifier(Constants.Segues.SignInToFp, sender: nil)
- }
- }
- pod 'Firebase/Auth'
- import Firebase
- AppState.sharedInstance.displayName = user?.displayName ?? user?.email
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement