How can I detect if the user was deleted from Firebase auth? - swift

Two apps are in the same Firebase project. One app deleted a user from Firebase Auth. But another app still get user uid from somewhere, maybe cache, and access the data in Firebase database with Auth's uid too.
I mean I can still get "user.uid" by let user = FIRAuth.auth().currentUser, if keeping sign-in.
The apps both set FIRDatabase.database().persistenceEnabled = true.
I would like to sing out or refresh cache at all apps if the user was deleted from Auth accordingly.

You can achieve it by doing this inside appDelegate:
//Check if user does exists
func checkUserAgainstDatabase(completion: #escaping (_ success: Bool, _ error: NSError?) -> Void) {
guard let currentUser = Auth.auth()?.currentUser else { return }
currentUser.getTokenForcingRefresh(true) { (idToken, error) in
if let error = error {
completion(false, error as NSError?)
print(error.localizedDescription)
} else {
completion(true, nil)
}
}
}
And you can do something like this after checking with the above function in didFinishLaunchingWithOptions:
If user does exist:
self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "CustomTabBarViewController")
else:
self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "WelcomeViewController")
And to test if it did work simply remove user manually from the Auth manager in the Firebase Console. So like this it should just show the welcome screen if user has been deleted.

with Firebase 4.0 it would look like this:
func checkUserAgainstDatabase(completion: #escaping (_ success: Bool, _ error: NSError?) -> Void) {
guard let currentUser = Auth.auth().currentUser else { return }
currentUser.getIDTokenForcingRefresh(true, completion: { (idToken, error) in
if let error = error {
completion(false, error as NSError?)
print(error.localizedDescription)
} else {
completion(true, nil)
}
})
}

Related

Swift Facebook Login sdk error with code 2

I am getting an error while logging in with Facebook. And it was working about 3-4 months ago.
Now I am back at developing this app again and I changed the development target to iOS 13.0 from iOS 12.0 and add the SceneDelegate and other necessary things. Actually, I don't think this is the reason but good to mention.
Now I am getting this error:
FBSDKLog: Cannot login without a valid login configuration. Please
make sure the LoginConfiguration provided is non-nil Error
Domain=com.facebook.sdk.core Code=2 "(null)"
UserInfo={com.facebook.sdk:FBSDKErrorDeveloperMessageKey=Cannot login
without a valid login configuration. Please make sure the
LoginConfiguration provided is non-nil}
while running this:
fbLoginManager.logIn(permissions: ["email, public_profile"], from: self) { (loginResult, error) in
if error != nil {
print(error!)
}
if let currentToken = AccessToken.current {
let credential = FacebookAuthProvider.credential(withAccessToken: currentToken.tokenString)
Auth.auth().signIn(with: credential) { (authResult, error) in
if let error = error {
let authError = error as NSError
print(authError)
} else {
DispatchQueue.main.async {
self.setupTabbar()
self.dismiss(animated: true, completion: nil)
}
}
}
}
}
I checked info.plist several times and apply the instructions from scratch but I am still getting this error.
I am using XCode 12.5 and Swift 5
Edit: When I remove the permissions it works but I actually need those permissions.
Solution
I changed permissions from ["email, public_profile"] to:
["email", "public_profile"]
so it looks like this:
fbLoginManager.logIn(permissions: ["email", "public_profile"], from: self) { (loginResult, error) in
if error != nil {
print(error!)
}
if let currentToken = AccessToken.current {
let credential = FacebookAuthProvider.credential(withAccessToken: currentToken.tokenString)
Auth.auth().signIn(with: credential) { (authResult, error) in
if let error = error as NSError? {
print(error)
} else {
DispatchQueue.main.async {
self.setupTabbar()
self.dismiss(animated: true, completion: nil)
}
}
}
}
}

AWS Cognito GetDetails() Method not calling

I'm using AWS Cognito for my app Sign In and Sign up. In my app first the user register with email and phone number. After that, I'm redirecting to Verification Screen(Here OTP is sending by Cognito) After Verifying the OTP user will create some stores and then enter into the Dashboard. In this flow, I want to get the User details Attribute from Cognito in Verification code success. I've implemented the getDetails() method to get the userAttributes in Verification code success but it is not calling. I need the userAttributes when the time of store creation. Any help appreciated.
Here is my code:
#IBAction func submitButtonAction(_ sender: GradientButton) {
let code = firstChar+secondChar+thirdChar+fourthChar+fifthChar+sixthChar
guard code.count == 6 else{
self.showAlert(message: ErrorMessages.kEnterValidOTP)
return
}
let currentUser = self.userPool?.getUser("xxxx#gmail.com")
currentUser?.confirmSignUp(code, forceAliasCreation: true).continueWith(block: { [weak self] (task) -> Any? in
guard let strongSelf = self else { return nil }
DispatchQueue.main.async {
if let error = task.error as NSError? {
if let message = error.userInfo["message"] as? String{
self?.showAlert(message: message, onOkAction: {
strongSelf.clearTextFieldData()
})
}else{
self?.showAlert(message: error.localizedDescription, onOkAction: {
strongSelf.clearTextFieldData()
})
}
}else{
print(task.result)
strongSelf.clearTextFieldData()
print(AWSUserDetails.shared.userPool.currentUser()?.username)
let user = AWSUserDetails.shared.userPool.currentUser()
//I've tried the above `user` and `currentUser`. But not working.
user?.getDetails().continueOnSuccessWith(block: { (task) -> Any? in
DispatchQueue.main.async {
if task.error == nil{
print(task.error)
}else{
print(task.result)
}
}
})
// strongSelf.performSegue(withIdentifier: SegueIdentifiers.createStoreSegue, sender: self)
}
}
return nil
})
}

(Firebase) After login with facebook, it won't return to my app

When I log in with Facebook in on a Real Device It will be stuck on the Facebook login page and don' return back to my app.
I have put facebook code in URL Schemes and put all code that need to put in info.plist.
This is all the code that i use to login Facebook by using firebase.
In Viewdidload
facebookButton.addTarget(self, action: #selector(handleCustomFBLogin), for: .touchUpInside)
Code that i use in Viewcontroller.
#objc func handleCustomFBLogin(sender:UIButton!){
LoginManager().logIn(permissions: ["email", "public_profile"], from: self) { (result, error) in
guard let result = result else {
print("No result found")
return
}
if result.isCancelled {
print("Facebook Login Cancelled")
} else if let error = error {
print("Process error \(error.localizedDescription)")
} else {
print("Logged in")
self.showEmailAddress()
self.actionfb()
}
}
}
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?){
if(error != nil){
print(error!)
return
}else{
print("Successfully Logged in using facebook")
showEmailAddress()
}
}
func actionfb(){
let accessToken = AccessToken.current
guard (accessToken?.tokenString) != nil else {return}
let credential = FacebookAuthProvider.credential(withAccessToken: accessToken!.tokenString)
// Perform login by calling Firebase APIs
Auth.auth().signInAndRetrieveData(with: credential) { (authResult, error) in
if let error = error {
self.showAlert(title: "error", message: "\(error.localizedDescription)")
return
}
self.performSegue(withIdentifier: "GoGo", sender: nil)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
}
func showEmailAddress(){
GraphRequest(graphPath: "/me", parameters: ["fields" : "id, name, first_name, last_name, email, birthday, picture"]).start { (connection, result, err) in
if(err != nil){
print("Failed to start GraphRequest", err ?? "")
return
}
print(result ?? "")
}
}
func loginButtonDidLogOut(_ loginButton: FBLoginButton){
print("Logged out of Facebook")
}
In Appdelegate
ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
This will happen only when i had test on real device!!, but in simulation this can work normally
I'm not exactly sure if this is the solution, but this fixed it for me.
I copied the OAuth redirect URI from my firebase, under Authentication -> Facebook
And then on my Facebook developer page, under Products -> Facebook Login -> Settings, I pasted it into Valid OAuth Redirect URIs
I then logged in by entering in my username and password into of clicking "Login with Facebook" and the error stopped happening.
I have no idea if the error will return, maybe this will be helpful to someone though.

App freezes when requesting access to addressbook

func getContacts() {
let store = CNContactStore()
if CNContactStore.authorizationStatus(for: .contacts) == .notDetermined {
store.requestAccess(for: .contacts, completionHandler: { (authorized: Bool, error: NSError?) -> Void in
if authorized {
self.retrieveContactsWithStore(store: store)
}
} as! (Bool, Error?) -> Void)
} else if CNContactStore.authorizationStatus(for: .contacts) == .authorized {
self.retrieveContactsWithStore(store: store)
}
}
func retrieveContactsWithStore(store: CNContactStore) {
do {
let groups = try store.groups(matching: nil)
let predicate = CNContact.predicateForContactsInGroup(withIdentifier: groups[0].identifier)
//let predicate = CNContact.predicateForContactsMatchingName("John")
let keysToFetch = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactEmailAddressesKey] as [Any]
let contacts = try store.unifiedContacts(matching: predicate, keysToFetch: keysToFetch as! [CNKeyDescriptor])
self.objects = contacts
DispatchQueue.main.async(execute: { () -> Void in
self.myTableView.reloadData()
})
} catch {
print(error)
}
}
I was trying to retrieve contacts from address book, but whenever I go to the view calling getContacts(), the app freezes. It wouldn't proceed anymore, but it didn't crash either. I wonder what went wrong here?
Your code for the call to requestAccess isn't correct. The syntax for the completion handler isn't valid. You need this:
func getContacts() {
let store = CNContactStore()
let status = CNContactStore.authorizationStatus(for: .contacts)
if status == .notDetermined {
store.requestAccess(for: .contacts, completionHandler: { (authorized: Bool, error: Error?) in
if authorized {
self.retrieveContactsWithStore(store: store)
}
})
} else if status == .authorized {
self.retrieveContactsWithStore(store: store)
}
}
Also note the change to use the status variable. This is cleaner and easier to read than calling authorizationStatus over and over. Call it once and then check the value over and over as needed.

Get Facebook user details with swift and parse

i need to get the Facebook user information details when i login a new user through parse. at present i am logging in the new user below. can't seem to get the user details though. I've seen some code written on objective - c. most of the functions don't work anymore
The Facebook iOS sdks i am running is v4.3.0.
#IBAction func facebookButton(sender: AnyObject) {
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
(user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user.isNew {
println("User signed up and logged in through Facebook!")
} else {
println("User logged in through Facebook!")
}
} else {
println("Uh oh. The user cancelled the Facebook login.")
}
}
}
To get the user details you have to send a FBSDKGraphRequest after the login request.
This can be done inside the if let user = user {...} block.
// Create request for user's Facebook data
let request = FBSDKGraphRequest(graphPath:"me", parameters:nil)
// Send request to Facebook
request.startWithCompletionHandler {
(connection, result, error) in
if error != nil {
// Some error checking here
}
else if let userData = result as? [String:AnyObject] {
// Access user data
let username = userData["name"] as? String
// ....
}
}
For the new Facebook API and version of Swift 3.1 you can do something like this:
if((FBSDKAccessToken.current()) != nil) {
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id,name,first_name,last_name,email"]).start(completionHandler: {(connection, result, error) -> Void in
if(error != nil) {
print("Some error occurred.");
} else {
print(result!)
}
})
}