Firebase authentication: linking multiple accounts in Swift -
i've set firebase authentication ios app using facebook, google & email/password sign in , it's working fine. authentication happens when user wants access high-priority parts of app (i.e. don't require users sign in start using app).
on app start up, sign users in anonymously in background , that's working fine too.
i've read documentation i'm struggling understand code required enable me link anonymous account facebook/email signed in account in following flow:
- new user opens app
- user signed in anonymously in background (new user.uid "a" created)
- low priority data stored against anonymous user in firebase realtime db
- user hits high-priority area needs authenticate
- user signs in using facebook (new user.uid "b" created)
- previous user.uid "a" needs linked user.uid "b"
my method looks this:
func signupwithfacebook(){ // track anonymous user link later let prevuser = firauth.auth()?.currentuser fbsdkloginmanager().loginwithreadpermissions(["public_profile", "email"], fromviewcontroller: self) { (result, error) in if let token = result?.token?.tokenstring { let credential = firfacebookauthprovider.credentialwithaccesstoken(token) firauth.auth()?.signinwithcredential(credential, completion: { (user, error) in if user != nil && error == nil { // success self.success?(user: user!) dispatch_async(dispatch_get_main_queue(), { self.dismissviewcontrolleranimated(true, completion: nil) }) } }) } } }
any pointers remove confusion great.
update: i've realised confused app logic because of users being created during testing. instead of 2 separate users being created above scenario (one authenticated via facebook , anonymously), happens original anonymous user.uid "a" "linked" facebook authentication credentials. in firebase console shown anonymous uid changing anonymous 1 facebook logo next it.
this working method looks like:
func signupwithfacebook(){ fbsdkloginmanager().loginwithreadpermissions(["public_profile", "email"], fromviewcontroller: self) { (result, error) in if let token = result?.token?.tokenstring { let credential = firfacebookauthprovider.credentialwithaccesstoken(token) firauth.auth()?.currentuser!.linkwithcredential(credential) { (user, error) in if user != nil && error == nil { // success self.success?(user: user!) dispatch_async(dispatch_get_main_queue(), { self.dismissviewcontrolleranimated(true, completion: nil) }) } else { print("linkwithcredential error:", error) } } } } }
so code follows first 2 steps in this link. documentation explicity says not call signinwithcredential
instead call
firauth.auth()?.currentuser.linkwithcredential(credential) { (user, error) in // ... }
after getting credential facebook's sdk.
quote link: "if call linkwithcredential:completion: succeeds, user's new account can access anonymous account's firebase data."
Comments
Post a Comment