Umar Yaqub
Umar Yaqub

Reputation: 86

Getting and saving Google profile picture into Firebase

I am unable to get users google profile picture and save it to the Firebase database. it just saves an empty picture to the Firebase database. My code looks like this:

Appdelegate.swift

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
    if let err = error {
        print ("Failed to log in with Google: ", err)
        return
    }

    print ("Successfully logged in with Google", user)


    guard let idToken = user.authentication.idToken else { return }
    guard let accessToken = user.authentication.accessToken else { return }

    let credentials = FIRGoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)
    FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
        if let err = error {
            print("Failed to create a Firebase User with Google: ", err)
            return
        } else {


        guard let uid = user?.uid else { return }
        print("Sucessfully logged in to Firebase with Google", uid)
            //sends user to homepage VC after a successful login
            let myStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let homePage = myStoryboard.instantiateViewController(withIdentifier: "homePageVC") as! homePageViewController


            let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.window?.rootViewController = homePage

        }

    })

homepageViewController.swift

@IBOutlet weak var profilePic: UIImageView!

func firebaseStorage() {

    let user = FIRAuth.auth()?.currentUser
    if let user = user {

        let name = user.displayName

        self.profileName.text = name

    }
    // Get a reference to the storage service using the default Firebase App
    let storage = FIRStorage.storage()

    // Create a storage reference
    let storageRef = storage.reference()

    //points to the child directory where the profile picture will be saved on firebase
    let profilePicRef = storageRef.child("/User Profile Pictures/"+(user?.uid)!+"/profile_pic.jpg")

 if (GIDSignIn.sharedInstance().currentUser != nil) {


            let data = Data()

            //upload image to storage
            let uploadTask = profilePicRef.put(data, metadata: nil) { (metadata, error) in
                guard let metadata = metadata else {
                    // Uh-oh, an error occurred!
                    return
                }
                // Metadata contains file metadata such as size, content-type, and download URL.
                let downloadURL = metadata.downloadURL
            }

            self.profilePic.image = UIImage(data: Data)

        }

   }

Upvotes: 0

Views: 2306

Answers (1)

Umar Yaqub
Umar Yaqub

Reputation: 86

Never mind guys! I managed to achieve it by modifying the view controller code as follows for anyone whose interested:

if (GIDSignIn.sharedInstance().currentUser != nil) {

            let imageUrl = GIDSignIn.sharedInstance().currentUser.profile.imageURL(withDimension: 400).absoluteString
            let url  = NSURL(string: imageUrl) as! URL
            let data = NSData(contentsOf: url)

            //upload image to storage
            let uploadTask = profilePicRef.put(data as! Data, metadata: nil) { (metadata, error) in
                guard let metadata = metadata else {
                    // Uh-oh, an error occurred!
                    return
                }
                // Metadata contains file metadata such as size, content-type, and download URL.
                let downloadURL = metadata.downloadURL
            }

            self.profilePic.image = UIImage(data: data as! Data)

        }


    }

Upvotes: 3

Related Questions