Skip to content

Commit 431001f

Browse files
authored
Merge pull request #95 from morganchen12/auth-sample
Auth sample
2 parents fda0519 + d80c5c5 commit 431001f

File tree

6 files changed

+256
-2
lines changed

6 files changed

+256
-2
lines changed

samples/swift/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ This sample uses [email/password](https://firebase.google.com/docs/auth/ios/pass
2424
and [Facebook](https://firebase.google.com/docs/auth/ios/facebook-login)
2525
auth, so make sure those are enabled in Firebase console.
2626

27+
The auth example requires a little more setup (adding url schemes, etc)
28+
since it depends on the various keys and tokens for the different auth
29+
services your app will support. Take a look at the [Auth README](../../FirebaseUI/Auth/README.md)
30+
for more information.
31+

samples/swift/uidemo.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
8DABC98A1D3D82D600453807 /* MenuViewController.swift */,
101101
8DABC9AA1D3D947300453807 /* SampleCell.swift */,
102102
8DABC9A81D3D872C00453807 /* Sample.swift */,
103-
8D16073D1D492B200069E4F5 /* AuthViewController.swift */,
103+
8DD17C951D4BCC6500E850E4 /* AuthSample */,
104104
8D643F0F1D4827F10081F979 /* ChatSample */,
105105
8DABC98C1D3D82D600453807 /* Main.storyboard */,
106106
8DABC98F1D3D82D600453807 /* Assets.xcassets */,
@@ -119,6 +119,14 @@
119119
path = uidemoTests;
120120
sourceTree = "<group>";
121121
};
122+
8DD17C951D4BCC6500E850E4 /* AuthSample */ = {
123+
isa = PBXGroup;
124+
children = (
125+
8D16073D1D492B200069E4F5 /* AuthViewController.swift */,
126+
);
127+
name = AuthSample;
128+
sourceTree = "<group>";
129+
};
122130
/* End PBXGroup section */
123131

124132
/* Begin PBXNativeTarget section */

samples/swift/uidemo/AppDelegate.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import UIKit
1818
import Firebase
19+
import FirebaseAuthUI
1920

2021
@UIApplicationMain
2122
class AppDelegate: UIResponder, UIApplicationDelegate {
@@ -32,5 +33,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3233
FIRApp.configure()
3334
return true
3435
}
36+
37+
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
38+
let sourceApplication = options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String?
39+
if FIRAuthUI.authUI()?.handleOpenURL(url, sourceApplication: sourceApplication) ?? false {
40+
return true
41+
}
42+
// other URL handling goes here.
43+
return false
44+
}
3545
}
3646

samples/swift/uidemo/AuthViewController.swift

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,112 @@
1515
//
1616

1717
import UIKit
18+
import Firebase
19+
import FirebaseAuthUI
20+
import FirebaseGoogleAuthUI
21+
import FirebaseFacebookAuthUI
1822

23+
let kFirebaseTermsOfService = NSURL(string: "https://firebase.google.com/terms/")!
24+
25+
// Your Google app's client ID, which can be found in the GoogleService-Info.plist file
26+
// and is stored in the `clientID` property of your FIRApp options.
27+
// Firebase Google auth is built on top of Google sign-in, so you'll have to add a URL
28+
// scheme to your project as outlined at the bottom of this reference:
29+
// https://developers.google.com/identity/sign-in/ios/start-integrating
30+
let kGoogleAppClientID = (FIRApp.defaultApp()?.options.clientID)!
31+
32+
// Your Facebook App ID, which can be found on developers.facebook.com.
33+
let kFacebookAppID = "your fb app ID here"
34+
35+
/// A view controller displaying a basic sign-in flow using FIRAuthUI.
1936
class AuthViewController: UIViewController {
37+
// Before running this sample, make sure you've correctly configured
38+
// the appropriate authentication methods in Firebase console. For more
39+
// info, see the Auth README at ../../FirebaseUI/Auth/README.md
40+
// and https://firebase.google.com/docs/auth/
41+
42+
private var authStateDidChangeHandle: FIRAuthStateDidChangeListenerHandle?
43+
44+
private(set) var auth: FIRAuth? = FIRAuth.auth()
45+
private(set) var authUI: FIRAuthUI? = FIRAuthUI.authUI()
46+
47+
@IBOutlet private var signOutButton: UIButton!
48+
@IBOutlet private var startButton: UIButton!
49+
50+
@IBOutlet private var signedInLabel: UILabel!
51+
@IBOutlet private var nameLabel: UILabel!
52+
@IBOutlet private var emailLabel: UILabel!
53+
@IBOutlet private var uidLabel: UILabel!
54+
55+
@IBOutlet var topConstraint: NSLayoutConstraint!
56+
57+
override func viewWillAppear(animated: Bool) {
58+
super.viewWillAppear(animated)
59+
60+
// If you haven't set up your authentications correctly these buttons
61+
// will still appear in the UI, but they'll crash the app when tapped.
62+
let providers: [FIRAuthProviderUI] = [
63+
FIRGoogleAuthUI(clientID: kGoogleAppClientID)!,
64+
FIRFacebookAuthUI(appID: kFacebookAppID)!,
65+
]
66+
self.authUI?.signInProviders = providers
67+
68+
// This is listed as `TOSURL` in the objc source,
69+
// but it's `termsOfServiceURL` in the current pod version.
70+
self.authUI?.termsOfServiceURL = kFirebaseTermsOfService
71+
72+
self.authStateDidChangeHandle =
73+
self.auth?.addAuthStateDidChangeListener(self.updateUI(auth:user:))
74+
}
75+
76+
override func viewWillDisappear(animated: Bool) {
77+
super.viewWillDisappear(animated)
78+
if let handle = self.authStateDidChangeHandle {
79+
self.auth?.removeAuthStateDidChangeListener(handle)
80+
}
81+
}
82+
83+
@IBAction func startPressed(sender: AnyObject) {
84+
let controller = self.authUI!.authViewController()
85+
self.presentViewController(controller, animated: true, completion: nil)
86+
}
87+
88+
@IBAction func signOutPressed(sender: AnyObject) {
89+
do {
90+
try self.auth?.signOut()
91+
} catch let error {
92+
// Again, fatalError is not a graceful way to handle errors.
93+
// This error is most likely a network error, so retrying here
94+
// makes sense.
95+
fatalError("Could not sign out: \(error)")
96+
}
97+
}
98+
99+
// Boilerplate
100+
func updateUI(auth auth: FIRAuth, user: FIRUser?) {
101+
if let user = user {
102+
self.signOutButton.enabled = true
103+
self.startButton.enabled = false
104+
105+
self.signedInLabel.text = "Signed in"
106+
self.nameLabel.text = "Name: " + (user.displayName ?? "(null)")
107+
self.emailLabel.text = "Email: " + (user.email ?? "(null)")
108+
self.uidLabel.text = "UID: " + user.uid
109+
} else {
110+
self.signOutButton.enabled = false
111+
self.startButton.enabled = true
112+
113+
self.signedInLabel.text = "Not signed in"
114+
self.nameLabel.text = "Name"
115+
self.emailLabel.text = "Email"
116+
self.uidLabel.text = "UID"
117+
}
118+
}
119+
120+
override func viewWillLayoutSubviews() {
121+
self.topConstraint.constant = self.topLayoutGuide.length
122+
}
123+
20124
static func fromStoryboard(storyboard: UIStoryboard = AppDelegate.mainStoryboard) -> AuthViewController {
21125
return storyboard.instantiateViewControllerWithIdentifier("AuthViewController") as! AuthViewController
22126
}

samples/swift/uidemo/Base.lproj/Main.storyboard

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,113 @@
201201
<view key="view" contentMode="scaleToFill" id="u8r-Hb-5vC">
202202
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
203203
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
204+
<subviews>
205+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="YIe-fR-yWY">
206+
<rect key="frame" x="0.0" y="0.0" width="600" height="124"/>
207+
<subviews>
208+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Not signed in" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="8tB-Nb-Xcd">
209+
<rect key="frame" x="8" y="8" width="101" height="21"/>
210+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
211+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
212+
<nil key="highlightedColor"/>
213+
</label>
214+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="GCa-X7-ESu">
215+
<rect key="frame" x="8" y="37" width="45" height="21"/>
216+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
217+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
218+
<nil key="highlightedColor"/>
219+
</label>
220+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Email" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="iS6-ot-m8Y">
221+
<rect key="frame" x="8" y="66" width="41" height="21"/>
222+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
223+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
224+
<nil key="highlightedColor"/>
225+
</label>
226+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="UID" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="HWC-2D-ngV">
227+
<rect key="frame" x="8" y="95" width="29" height="21"/>
228+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
229+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
230+
<nil key="highlightedColor"/>
231+
</label>
232+
</subviews>
233+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
234+
<constraints>
235+
<constraint firstItem="8tB-Nb-Xcd" firstAttribute="leading" secondItem="YIe-fR-yWY" secondAttribute="leading" constant="8" id="4aU-NI-SWp"/>
236+
<constraint firstItem="GCa-X7-ESu" firstAttribute="leading" secondItem="YIe-fR-yWY" secondAttribute="leading" constant="8" id="G6w-I1-yW8"/>
237+
<constraint firstItem="HWC-2D-ngV" firstAttribute="leading" secondItem="YIe-fR-yWY" secondAttribute="leading" constant="8" id="HpK-MN-Aah"/>
238+
<constraint firstItem="8tB-Nb-Xcd" firstAttribute="top" secondItem="YIe-fR-yWY" secondAttribute="top" constant="8" id="kRM-op-ono"/>
239+
<constraint firstItem="iS6-ot-m8Y" firstAttribute="leading" secondItem="YIe-fR-yWY" secondAttribute="leading" constant="8" id="pfj-AJ-isf"/>
240+
<constraint firstItem="GCa-X7-ESu" firstAttribute="top" secondItem="8tB-Nb-Xcd" secondAttribute="bottom" constant="8" id="sHu-Hu-pCC"/>
241+
<constraint firstItem="HWC-2D-ngV" firstAttribute="top" secondItem="iS6-ot-m8Y" secondAttribute="bottom" constant="8" id="tYm-BL-kX5"/>
242+
<constraint firstAttribute="bottom" secondItem="HWC-2D-ngV" secondAttribute="bottom" constant="8" id="xRL-Is-zaO"/>
243+
<constraint firstItem="iS6-ot-m8Y" firstAttribute="top" secondItem="GCa-X7-ESu" secondAttribute="bottom" constant="8" id="yO5-fo-ZdE"/>
244+
</constraints>
245+
</view>
246+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="3BK-PR-8FT">
247+
<rect key="frame" x="300" y="124" width="300" height="476"/>
248+
<subviews>
249+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="DqE-YA-5iK">
250+
<rect key="frame" x="133" y="223" width="34" height="30"/>
251+
<state key="normal" title="Start"/>
252+
<connections>
253+
<action selector="startPressed:" destination="Vgg-qQ-Fya" eventType="touchUpInside" id="7Sr-nn-gta"/>
254+
</connections>
255+
</button>
256+
</subviews>
257+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
258+
<constraints>
259+
<constraint firstItem="DqE-YA-5iK" firstAttribute="centerX" secondItem="3BK-PR-8FT" secondAttribute="centerX" id="Oon-KV-vcj"/>
260+
<constraint firstItem="DqE-YA-5iK" firstAttribute="centerY" secondItem="3BK-PR-8FT" secondAttribute="centerY" id="wsw-LA-OmB"/>
261+
</constraints>
262+
</view>
263+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="eNG-fu-0tI">
264+
<rect key="frame" x="0.0" y="124" width="300" height="476"/>
265+
<subviews>
266+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="szM-ks-o6D">
267+
<rect key="frame" x="121" y="223" width="57" height="30"/>
268+
<state key="normal" title="Sign out"/>
269+
<connections>
270+
<action selector="signOutPressed:" destination="Vgg-qQ-Fya" eventType="touchUpInside" id="1Ht-pM-WqF"/>
271+
</connections>
272+
</button>
273+
</subviews>
274+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
275+
<constraints>
276+
<constraint firstItem="szM-ks-o6D" firstAttribute="centerX" secondItem="eNG-fu-0tI" secondAttribute="centerX" id="3eB-p7-5Rg"/>
277+
<constraint firstItem="szM-ks-o6D" firstAttribute="centerY" secondItem="eNG-fu-0tI" secondAttribute="centerY" id="gjJ-Vj-I4W"/>
278+
</constraints>
279+
</view>
280+
</subviews>
204281
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
282+
<constraints>
283+
<constraint firstItem="YIe-fR-yWY" firstAttribute="leading" secondItem="u8r-Hb-5vC" secondAttribute="leading" id="AsX-Su-23M"/>
284+
<constraint firstItem="fZA-s2-42O" firstAttribute="top" secondItem="3BK-PR-8FT" secondAttribute="bottom" id="H8Y-aW-eh5"/>
285+
<constraint firstAttribute="trailing" secondItem="3BK-PR-8FT" secondAttribute="trailing" id="NrE-o8-tL3"/>
286+
<constraint firstItem="3BK-PR-8FT" firstAttribute="leading" secondItem="eNG-fu-0tI" secondAttribute="trailing" id="OLY-AY-uJO"/>
287+
<constraint firstItem="eNG-fu-0tI" firstAttribute="top" secondItem="YIe-fR-yWY" secondAttribute="bottom" id="PLX-9b-DWe"/>
288+
<constraint firstItem="fZA-s2-42O" firstAttribute="top" secondItem="eNG-fu-0tI" secondAttribute="bottom" id="RIu-MK-e1w"/>
289+
<constraint firstItem="3BK-PR-8FT" firstAttribute="leading" secondItem="eNG-fu-0tI" secondAttribute="trailing" id="SYR-4B-6DW"/>
290+
<constraint firstItem="eNG-fu-0tI" firstAttribute="top" secondItem="YIe-fR-yWY" secondAttribute="bottom" id="cOi-oH-aAF"/>
291+
<constraint firstItem="3BK-PR-8FT" firstAttribute="top" secondItem="YIe-fR-yWY" secondAttribute="bottom" id="dRr-3V-P72"/>
292+
<constraint firstAttribute="trailing" secondItem="YIe-fR-yWY" secondAttribute="trailing" id="hSP-Ew-sl2"/>
293+
<constraint firstItem="3BK-PR-8FT" firstAttribute="width" secondItem="eNG-fu-0tI" secondAttribute="width" id="kVf-7W-wTI"/>
294+
<constraint firstItem="YIe-fR-yWY" firstAttribute="top" secondItem="u8r-Hb-5vC" secondAttribute="topMargin" id="qLi-uJ-bUD"/>
295+
<constraint firstItem="eNG-fu-0tI" firstAttribute="leading" secondItem="u8r-Hb-5vC" secondAttribute="leading" id="ygi-rX-jiL"/>
296+
</constraints>
205297
</view>
298+
<connections>
299+
<outlet property="emailLabel" destination="iS6-ot-m8Y" id="06e-sc-qW4"/>
300+
<outlet property="nameLabel" destination="GCa-X7-ESu" id="WxF-MQ-yxQ"/>
301+
<outlet property="signOutButton" destination="szM-ks-o6D" id="g3p-ce-KzL"/>
302+
<outlet property="signedInLabel" destination="8tB-Nb-Xcd" id="0lp-Oe-j2G"/>
303+
<outlet property="startButton" destination="DqE-YA-5iK" id="Pr0-kI-YMB"/>
304+
<outlet property="topConstraint" destination="qLi-uJ-bUD" id="L08-SW-Klp"/>
305+
<outlet property="uidLabel" destination="HWC-2D-ngV" id="gMt-EM-7ob"/>
306+
</connections>
206307
</viewController>
207308
<placeholder placeholderIdentifier="IBFirstResponder" id="LWq-2d-DPi" userLabel="First Responder" sceneMemberID="firstResponder"/>
208309
</objects>
209-
<point key="canvasLocation" x="2400" y="1023"/>
310+
<point key="canvasLocation" x="2426" y="1003"/>
210311
</scene>
211312
<!--Navigation Controller-->
212313
<scene sceneID="D29-xs-Qyh">

samples/swift/uidemo/Info.plist

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>LSApplicationQueriesSchemes</key>
6+
<array>
7+
<string>fbapi</string>
8+
<string>fb-messenger-api</string>
9+
<string>fbauth2</string>
10+
<string>fbshareextension</string>
11+
</array>
512
<key>CFBundleDevelopmentRegion</key>
613
<string>en</string>
714
<key>CFBundleExecutable</key>
@@ -18,6 +25,25 @@
1825
<string>1.0</string>
1926
<key>CFBundleSignature</key>
2027
<string>????</string>
28+
<key>CFBundleURLTypes</key>
29+
<array>
30+
<dict>
31+
<key>CFBundleTypeRole</key>
32+
<string>Editor</string>
33+
<key>CFBundleURLSchemes</key>
34+
<array>
35+
<string>REVERSED_CLIENT_ID</string>
36+
</array>
37+
</dict>
38+
<dict>
39+
<key>CFBundleTypeRole</key>
40+
<string>Editor</string>
41+
<key>CFBundleURLSchemes</key>
42+
<array>
43+
<string>FACEBOOK_APP_ID</string>
44+
</array>
45+
</dict>
46+
</array>
2147
<key>CFBundleVersion</key>
2248
<string>1</string>
2349
<key>LSRequiresIPhoneOS</key>

0 commit comments

Comments
 (0)