[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Auth examples with GoogleSignIn 6.0.0 #1222

Merged
merged 5 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,36 @@ class AccountLinkingViewController: UIViewController, DataSourceProviderDelegate
/// See this class's conformance to `GIDSignInDelegate` below for
/// context on how the linking is made.
private func performGoogleAccountLink() {
// Configure the Google Sign In instance
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()!.options.clientID
GIDSignIn.sharedInstance().delegate = self
guard let clientID = FirebaseApp.app()?.options.clientID else { return }

// Create Google Sign In configuration object.
let config = GIDConfiguration(clientID: clientID)

// Start the sign in flow!
GIDSignIn.sharedInstance()?.presentingViewController = self
GIDSignIn.sharedInstance().signIn()
GIDSignIn.sharedInstance.signIn(with: config, presenting: self) { [unowned self] user, error in

guard error == nil else { return displayError(error) }

guard
let authentication = user?.authentication,
let idToken = authentication.idToken
else {
let error = NSError(
domain: "GIDSignInError",
code: -1,
userInfo: [
NSLocalizedDescriptionKey: "Unexpected sign in result: required authentication data is missing.",
]
)
return displayError(error)
}

let credential = GoogleAuthProvider.credential(withIDToken: idToken,
accessToken: authentication.accessToken)

// Rather than use the credential to sign in the user, we will use it to link to the currently signed in user's account.
linkAccount(authCredential: credential)
}
}

// MARK: - Sign in with Apple Account Linking 🔥
Expand Down Expand Up @@ -414,20 +437,6 @@ extension AccountLinkingViewController: DataSourceProvidable {
}
}

// MARK: - GIDSignInDelegate for Google Sign In

extension AccountLinkingViewController: GIDSignInDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
guard error == nil else { return displayError(error) }

guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
// Rather than use the credential to sign in the user, we will use it to link to the currently signed in user's account.
linkAccount(authCredential: credential)
}
}

// MARK: - Implementing Sign in with Apple with Firebase

extension AccountLinkingViewController: ASAuthorizationControllerDelegate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,41 @@ class AuthViewController: UIViewController, DataSourceProviderDelegate {
// MARK: - Firebase 🔥

private func performGoogleSignInFlow() {
// Configure the Google Sign In instance
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()!.options.clientID
GIDSignIn.sharedInstance().delegate = self
guard let clientID = FirebaseApp.app()?.options.clientID else { return }

// Create Google Sign In configuration object.
let config = GIDConfiguration(clientID: clientID)

// Start the sign in flow!
GIDSignIn.sharedInstance()?.presentingViewController = self
GIDSignIn.sharedInstance().signIn()
GIDSignIn.sharedInstance.signIn(with: config, presenting: self) { [unowned self] user, error in

guard error == nil else { return displayError(error) }

guard
let authentication = user?.authentication,
let idToken = authentication.idToken
else {
let error = NSError(
domain: "GIDSignInError",
code: -1,
userInfo: [
NSLocalizedDescriptionKey: "Unexpected sign in result: required authentication data is missing.",
]
)
return displayError(error)
}

let credential = GoogleAuthProvider.credential(withIDToken: idToken,
accessToken: authentication.accessToken)

Auth.auth().signIn(with: credential) { result, error in
guard error == nil else { return self.displayError(error) }

// At this point, our user is signed in
// so we advance to the User View Controller
self.transitionToUserViewController()
}
}
}

// For Sign in with Apple
Expand Down Expand Up @@ -202,26 +230,6 @@ class AuthViewController: UIViewController, DataSourceProviderDelegate {
}
}

// MARK: - GIDSignInDelegate for Google Sign In

extension AuthViewController: GIDSignInDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
guard error == nil else { return displayError(error) }

guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)

Auth.auth().signIn(with: credential) { result, error in
guard error == nil else { return self.displayError(error) }

// At this point, our user is signed in
// so we advance to the User View Controller
self.transitionToUserViewController()
}
}
}

// MARK: - LoginDelegate

extension AuthViewController: LoginDelegate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@import GoogleSignIn;

// [START signin_delegate]
@interface AppDelegate : UIResponder<UIApplicationDelegate, GIDSignInDelegate>
@interface AppDelegate : UIResponder<UIApplicationDelegate>
// [END signin_delegate]

@property(nonatomic, strong) UIWindow *window;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ - (BOOL)application:(UIApplication *)application
[FIRApp configure];
// [END firebase_configure]

// [START setup_gidsignin]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@morganchen12 I'm not sure if the tags are affecting anything and if I updated them properly. Could you PTAL.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting this snippet is ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SG. Please add me to reviewers for the updated docs to ensure they have correct snippets.

[GIDSignIn sharedInstance].clientID = [FIRApp defaultApp].options.clientID;
[GIDSignIn sharedInstance].delegate = self;
// [END setup_gidsignin]

[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
return YES;
Expand Down Expand Up @@ -99,29 +94,4 @@ - (BOOL)handlePasswordlessSignInWithLink:(nonnull NSURL*)url {
return NO;
}

// [START headless_google_auth]
- (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error {
// [START_EXCLUDE]
MainViewController *controller = (MainViewController*) [GIDSignIn sharedInstance].presentingViewController;
// [END_EXCLUDE]
if (error == nil) {
// [START google_credential]
GIDAuthentication *authentication = user.authentication;
FIRAuthCredential *credential =
[FIRGoogleAuthProvider credentialWithIDToken:authentication.idToken
accessToken:authentication.accessToken];
// [END google_credential]
// [START_EXCLUDE]
[controller firebaseLoginWithCredential:credential];
// [END_EXCLUDE]
} else {
// [START_EXCLUDE]
[controller showMessagePrompt:error.localizedDescription];
// [END_EXCLUDE]
}
}
// [END headless_google_auth]

@end
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ @interface MainViewController (SignInWithApple) <ASAuthorizationControllerDelega

- (void)startSignInWithAppleFlow API_AVAILABLE(ios(13.0));

- (void)startSignInWithGoogleFlow;

@end

@implementation MainViewController {
Expand Down Expand Up @@ -344,10 +346,7 @@ - (void)showAuthPicker: (NSArray<NSNumber *>*) providers {
action = [UIAlertAction actionWithTitle:@"Google"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
// [START setup_gid_uidelegate]
[GIDSignIn sharedInstance].presentingViewController = self;
[[GIDSignIn sharedInstance] signIn];
// [END setup_gid_uidelegate]
[self startSignInWithGoogleFlow];
}];
}
break;
Expand Down Expand Up @@ -1156,4 +1155,35 @@ - (NSString *)currentNonce {
return [_currentNonce copy];
}

#pragma mark - Sign in with Google

- (void)startSignInWithGoogleFlow {
// [START headless_google_auth]
GIDConfiguration *config = [[GIDConfiguration alloc] initWithClientID:[FIRApp defaultApp].options.clientID];

__weak __auto_type weakSelf = self;
[GIDSignIn.sharedInstance signInWithConfiguration:config presentingViewController:self callback:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) {
__auto_type strongSelf = weakSelf;
if (strongSelf == nil) { return; }

if (error == nil) {
// [START google_credential]
GIDAuthentication *authentication = user.authentication;
FIRAuthCredential *credential =
[FIRGoogleAuthProvider credentialWithIDToken:authentication.idToken
accessToken:authentication.accessToken];
// [END google_credential]
// [START_EXCLUDE]
[strongSelf firebaseLoginWithCredential:credential];
// [END_EXCLUDE]
} else {
// [START_EXCLUDE]
[strongSelf showMessagePrompt:error.localizedDescription];
// [END_EXCLUDE]
}
}];

// [END headless_google_auth]
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import FBSDKCoreKit

@UIApplicationMain
// [START signin_delegate]
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
class AppDelegate: UIResponder, UIApplicationDelegate {
// [END signin_delegate]

var window: UIWindow?
Expand All @@ -41,11 +41,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
FirebaseApp.configure()
// [END firebase_configure]

// [START setup_gidsignin]
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
// [END setup_gidsignin]

ApplicationDelegate.shared.application(application,
didFinishLaunchingWithOptions: launchOptions)

Expand Down Expand Up @@ -75,7 +70,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
if handlePasswordlessSignIn(withURL: url) {
return true
}
if GIDSignIn.sharedInstance().handle(url) {
if GIDSignIn.sharedInstance.handle(url) {
return true
}
return ApplicationDelegate.shared.application(application,
Expand Down Expand Up @@ -106,29 +101,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
}
return false
}

// [START headless_google_auth]
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
// [START_EXCLUDE]
guard let controller = GIDSignIn.sharedInstance()
.presentingViewController as? MainViewController else { return }
// [END_EXCLUDE]
if let error = error {
// [START_EXCLUDE]
controller.showMessagePrompt(error.localizedDescription)
// [END_EXCLUDE]
return
}

// [START google_credential]
guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
// [END google_credential]
// [START_EXCLUDE]
controller.firebaseLogin(credential)
// [END_EXCLUDE]
}

// [END headless_google_auth]
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,7 @@ class MainViewController: UITableViewController {
}
case .authGoogle:
action = UIAlertAction(title: "Google", style: .default) { UIAlertAction in
// [START setup_gid_uidelegate]
GIDSignIn.sharedInstance()?.presentingViewController = self
GIDSignIn.sharedInstance().signIn()
// [END setup_gid_uidelegate]
self.startSignInWithGoogleFlow()
}
case .authTwitter:
action = UIAlertAction(title: "Twitter", style: .default) { UIAlertAction in
Expand Down Expand Up @@ -985,6 +982,44 @@ class MainViewController: UITableViewController {
tableView.reloadData()
}

// MARK: - Sign in with Google

func startSignInWithGoogleFlow() {
// [START headless_google_auth]
guard let clientID = FirebaseApp.app()?.options.clientID else { return }

// Create Google Sign In configuration object.
let config = GIDConfiguration(clientID: clientID)

// Start the sign in flow!
GIDSignIn.sharedInstance.signIn(with: config, presenting: self) { [unowned self] user, error in

if let error = error {
// [START_EXCLUDE]
self.showMessagePrompt(error.localizedDescription)
// [END_EXCLUDE]
return
}

// [START google_credential]
guard
let authentication = user?.authentication,
let idToken = authentication.idToken
else {
return
}

let credential = GoogleAuthProvider.credential(withIDToken: idToken,
accessToken: authentication.accessToken)
// [END google_credential]

// [START_EXCLUDE]
self.firebaseLogin(credential)
// [END_EXCLUDE]
}
// [END headless_google_auth]
}

// MARK: - Sign in with Apple

// Unhashed nonce.
Expand Down
6 changes: 3 additions & 3 deletions authentication/LegacyAuthQuickstart/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ PODS:
- GoogleUtilities/Environment (~> 7.2)
- nanopb (~> 2.30908.0)
- PromisesObjC (< 3.0, >= 1.2)
- GoogleSignIn (5.0.2):
- AppAuth (~> 1.2)
- GoogleSignIn (6.0.0):
- AppAuth (~> 1.4)
- GTMAppAuth (~> 1.0)
- GTMSessionFetcher/Core (~> 1.1)
- GoogleUtilities/AppDelegateSwizzler (7.5.0):
Expand Down Expand Up @@ -154,7 +154,7 @@ SPEC CHECKSUMS:
FirebaseInstallations: b69db7680870dbac17bba7a51fc0b5c4b365baa7
GoogleAppMeasurement: de70802583dedceb0bca18172b345307e8f410b8
GoogleDataTransport: 85fd18ff3019bb85d3f2c551d04c481dedf71fc9
GoogleSignIn: 7137d297ddc022a7e0aa4619c86d72c909fa7213
GoogleSignIn: b7779a809e1dfb32ebb43131b10d8bfcd53c1383
GoogleUtilities: eea970f4a389963963bffe8d8fabe43540678b9c
GTMAppAuth: ad5c2b70b9a8689e1a04033c9369c4915bfcbe89
GTMSessionFetcher: 36689134877faeb055b27dfa4ccc9ceaa42e029e
Expand Down
2 changes: 1 addition & 1 deletion authentication/LegacyAuthQuickstart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ a paid service. If you are only using Firebase Authentication this sample will n
- Select the **Auth** panel and then click the **Sign In Method** tab.
- Click **Twitter** and turn on the **Enable** switch, then click **Save**.
- Enter your Twitter **API Key** and **App Secret** and click **Save**.
- Make sure your Firebase OAuth redirect URI (e.g. my-app-12345.firebaseapp.com/__/auth/handler) is set as your
- Make sure your Firebase OAuth redirect URI (e.g. my-app-12345.firebaseapp.com/__/auth/handler) is set as your
Authorization callback URL in your app's settings page on your [Twitter app's config](https://apps.twitter.com).
- Run the app on your device or simulator.
- Select **Sign In** and select Twitter to begin.
Expand Down
Loading