aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/tvOSSample/tvOSSample/AuthViewController.swift
blob: 72351d315403871fc47935c2d1787693d72c3308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2017 Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import UIKit
import FirebaseAuth

class AuthViewController: UIViewController {

  // MARK: - User Interface

  /// A stackview containing all of the buttons to providers (Email, OAuth, etc).
  @IBOutlet weak var providers: UIStackView!

  /// A stackview containing a signed in label and sign out button.
  @IBOutlet weak var signedIn: UIStackView!

  /// A label to display the status for the signed in user.
  @IBOutlet weak var signInStatus: UILabel!

  // MARK: - User Actions

  @IBAction func signOutButtonHit(_ sender: UIButton) {
    // Sign out via Auth and update the UI.
    try? Auth.auth().signOut()

    setUserSignedIn(nil)
  }

  // MARK: - View Controller Lifecycle

  override func viewDidLoad() {
    super.viewDidLoad()

    // Update the UI based on the current user (if there is one).
    setUserSignedIn(Auth.auth().currentUser)
  }

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destination = segue.destination
    if let emailVC = destination as? EmailLoginViewController {
      emailVC.delegate = self
    }
  }

  // MARK: - Internal Helpers

  private func setUserSignedIn(_ user: User?) {
    if let user = user {
      providers.isHidden = true
      signedIn.isHidden = false

      signInStatus.text = "User is signed in via \(user.providerID) and the UID \(user.uid)"
    } else {
      // User is signed out, hide the signed in state and show the providers.
      providers.isHidden = false
      signedIn.isHidden = true
    }
  }
}

// MARK: - EmailLoginDelegate conformance.

extension AuthViewController: EmailLoginDelegate {
  func emailLogin(_ controller: EmailLoginViewController, signedInAs user: User) {
    setUserSignedIn(user)
    dismiss(animated: true)
  }

  func emailLogin(_ controller: EmailLoginViewController, failedWithError error: Error) {
    print("Fail..... \(error)")
    DispatchQueue.main.async {
      controller.presentError(with: "There was an issue logging in. Please try again.")
    }
  }
}