iOS Gmail Login using google SDK and storing data in Firebase.

sunny
3 min readNov 8, 2020

--

This story is a continuation of my previous story here, previous story contains facebook login storing in firebase. This story will discuss Gmail login storing in firebase.

Step 1. Install Google Pods.

pod ‘GoogleSignIn’, ‘5.0’ — This for google login.

pod ‘Firebase/Auth’, ‘6.33.0’ — this for firebase.

Step 2. Add plist from firebase configuration. And the client id setup.

If you follow my previous URL we have download .plist from firebase config, please refer to my previous URL step 5–2.

XCode, you’ll have GoogleService-Info.plist file, open it and find REVERSED_CLIENT_ID, copy it!

Client id Located in plist

Select your app from the TARGETS section, then select the Info tab, and expand the URL Types section, click on + and paste the copied REVERSED_CLIENT_ID here.

Step 3. Write code in the App delegate.

Following code Add-In didFinishLaunchingWithOptions and in open url

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {GIDSignIn.sharedInstance()?.clientID = FirebaseApp.app()?.options.clientIDreturn true}
func
application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
var result = trueresult = GIDSignIn.sharedInstance().handle(url)return result}

I created ‘googleLoginButton’ is from the UIView object in .xib that looks like below.

This is UIView Object

And I created SignInViewController.swift file looks like below.

import UIKitimport FirebaseAuthimport GoogleSignInclass SignInViewController: UIViewController {@IBOutlet weak var googleLoginButton: GIDSignInButton!override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view.view.backgroundColor = .whiteGIDSignIn.sharedInstance()?.presentingViewController = selfGIDSignIn.sharedInstance().delegate = selfconfigureUI()}private func configureUI() {//if user previously sign launch homeif isUserPreviouslySignInWithGmail() {// launch what ever we need to show}}//this check if user is previously login.private func isUserPreviouslySignInWithGmail() -> Bool {if GIDSignIn.sharedInstance().hasPreviousSignIn() {return true}return false}//If user login complete in gmail site follwoing delegate method will get call And storeing user id singin also mentioned in below code.extension SignInViewController: GIDSignInDelegate {func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {guard let idToken = user?.authentication.idToken, let accessToken = user?.authentication.accessToken else {print("Failed to log into Google:  \(error.localizedDescription)")return}let credentials = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)loggingWithGmail(credentials: credentials) { [weak self] (error) inif let error = error {print("Error logging in firebase \(error.localizedDescription)")return}self?.saveUserInfo()}}}public func loggingWithGmail(credentials: AuthCredential, completion: @escaping (Error?) -> Void) {fireBaseSignIn(with: credentials, completion: { [weak self] (data, error) inguard let result = data, error == nil else {print("FB Login Error: \(String(describing: error?.localizedDescription))")completion(error)return}completion(nil)self?.saveUserInfo(authData: result)})}//storing into firebaseprivate func fireBaseSignIn(with credentials: AuthCredential, completion: @escaping (AuthDataResult?, Error?) -> Void) {Auth.auth().signIn(with: credentials) { (result, error) inguard let dataResult = result else {completion(nil, error)print("Firebase login error ")return}completion(dataResult, nil)}}/// Save user info to User Defaultsprivate func saveUserInfo(authData: AuthDataResult) {let userDefaults = UserDefaults.standardlet _ = authData.user.photoURLuserDefaults.setValue(authData.user.displayName, forKey: "name")}

With help of the following URLs, this one is written, please add in comments if you need any help on this. And please clap if you like this.

https://developers.google.com/identity/sign-in/ios/start-integrating

--

--

No responses yet