No FB access token upon initial Parse FB login - facebook

I am logging into my app using PFFacebookUtils.logInWithPermissions however, the first time I log in the actual login works fine but I get the FB error code = 2500 An active access token must be used to query information about the current user. This results in any graphRequests failing.
If I then recompile the code to the device this error does not occur and all graphRequests work fine. Why is the app not getting an access token upon the first login? Also, if I completely delete the app from the device and reinstall this whole process starts again (error code = 2500 on first attempt).
Also maybe worth noting, this didn't happen with the previous versions of Parse and FB SDKs. I am just starting to move this app into iOS9 and that's when this problem popped up.
Xcode 7.0.1, Parse SDK v1.8.5, FacebookSDKs-iOS-20150910

I was able to resolve this issue by first calling the FBSDK login method followed by the Parse SDK login method (inside the FBSDK callback):
#IBAction func facebookButtonAction(sender: AnyObject) {
// Ensure that PFUser == nil - just in here because I heard it can help with some possible issues
PFUser.logOut()
let permissions = ["public_profile", "email", "user_friends"]
// First, login with FB’s SDK
let login: FBSDKLoginManager = FBSDKLoginManager()
login.logInWithReadPermissions(permissions, handler: {(result: FBSDKLoginManagerLoginResult!, error: NSError!) in
if (error != nil) {
NSLog("Process error")
} else {
if result.isCancelled {
NSLog("Cancelled")
} else {
NSLog("Logged in")
// Then with Parse’s, upon FB login success
PFFacebookUtils.logInWithPermissions(permissions) { (user, error) -> Void in
if let user = user {
if user.isNew {
print("User signed up and logged in through Facebook!”)
//...
} else {
print("User logged in through Facebook!")
//...
}
} else {
print("Uh oh. The user cancelled the Facebook login.")
//...
}
}
}
}
})
}

Related

fb login doesn't ask for credentials for login even after logging out iOS swift 4

I am implementing fb login in iOS app, want to login using different fb accounts in same app.
I've installed sdk using cocoapods and not using any third party library. swift 4, Xcode 9,
let manager = LoginManager()
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
if (AccessToken.current == nil){
//loginButton.loginBehavior = .browser
DispatchQueue.main.async(){
// LoginManager.init(loginBehavior: .web, defaultAudience: .everyone)
print(LoginBehavior.self)
self.manager.logIn( permissions: [ .publicProfile, .email ], viewController: self){(result) in
switch result {
case .cancelled:
print("user cancelled login process")
break
case .failed(let err):
print("login failed with error: \(err.localizedDescription)")
break
case .success(let grantedPermissions, let declinedPermissions, let accessToken):
print(accessToken)
//get user profile
}
}
}
print("login")
} else {
print("user is already Logged in")
}
}
func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
AccessToken.current = nil
manager.logOut()
}
I can login and logout. but it doesn't ask me to enter password next time I try to login. means I can not login using different fb account.
also in AppDelegate file I am getting error implementing fbsdk functions:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}
When you login into the Facebook application or in mobile browser it keeps an active session until and unless you explicitly logout. When any user tries to log in with Facebook in the third party application then if the user is logged in from the Facebook application or through site in mobile browser then it will not ask the user to log in and that logged in account will be directly used. You have to logout from the Facebook app or mobile browser to login with different account. From the first time from your application if user logs in to the Facebook and enters the Facebook credentials then it will save that session in the mobile browser or Facebook application(if available) so next time it won't ask for the credentials again.
Call below function to logout :
FB.logout(function(response) {
// user is now logged out
});
Please refer different cases in the logout scenarios from the below link :
https://developers.facebook.com/docs/reference/javascript/FB.logout
Hope this helps you.
You must have it saved in key chain thats why it is not asking for credentials again. Please try deleting or logging out your account from your native fb app and try again.

Firebase not storing logged in user

Was working with Firebase for almost half year now and never had this issue. Using code just like before to create new user, he shows up in console, when i close app i can sign in but using same code as always it keeps prompting me for password. In first VC i have:
if let user = Auth.auth().currentUser {
performSegue(withIdentifier: "authorized", sender: self)
}
Which will check if there is auth token stored and perform segue to loading screen. However this never happens. The code is ran but doesn't do anything, no errors in compiler and no errors in console.
EDIT 1: also tried to create new project where i rewrote all the code getting same result, but my other project works without any issues, also i'm able to do anything else besides this using firebase such as access Firestore just not storing the user, went over all the documentation but couldn't find any solution.
EDIT 2: sign in code
if(email.text != "" && password.text != ""){
Auth.auth().signIn(withEmail: email.text!, password: password.text!) { user, error in
if(error == nil && user != nil) {
userID = (Auth.auth().currentUser?.uid)!
print("Signed in as: \(userID)")
self.performSegue(withIdentifier: "load", sender: self)
} else {
print("Error found: \(error?.localizedDescription)")
}
}
}
Solution:
DispatchQueue.main.async(){
if let user = Auth.auth().currentUser {
userID = (Auth.auth().currentUser?.uid)!
self.performSegue(withIdentifier: "authorized", sender: self)
}
}
Loaded with async so firebase will configure before checking for auth
The issue is likely that you are calling your code before firebase has properly initialized. I recommend using the entry view controller's viewDidAppear for things such as these. If you were using viewDidLoad, it is likely that firebase did not load yet.

How do you connect to AWS Cognito in Swift for user signing up and logging in?

I'm going to start this question by apologizing for such an open ended question, but I am really struggling and all the documentation is outdated.
Amazon's provided sample app hasn't been updated and I get nothing but errors when attempting to run it. I have a login page setup and ready to go with email and password, and I have tried this code:
#IBAction func signInButtonTouched(sender: UIButton) {
if (emailTextField.text != nil) && (passwordTextField.text != nil) {
let user = (UIApplication.sharedApplication().delegate as! AppDelegate).userPool!.getUser(emailTextField.text!)
user.getSession(emailTextField.text!, password: passwordTextField.text!, validationData: nil, scopes: nil).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: {
(task:AWSTask!) -> AnyObject! in
if task.error == nil {
// user is logged in - show logged in UI
} else {
// error
}
return nil
})
} else {
// email or password not set
}
}
but unfortunately I'm getting App Delegate has no member "userPool" errors. It does though! I am very new at this, and I have searched github and read through all of Amazon's samples, but the documentation is either in Objective-C, or outdated and doesn't work properly.
Any help with this would be vastly appreciated.

Parse Facebook login shows always Facebook page "you have already authorized this app"

In my app, I can log into Parse via Facebook. However after the 1st login, further login always show a Facebook page saying "you have already authorized this app“, and I have to press OK. This is useless and annoying.
According to the information on the web, this happens now for more than 2 years, and many questions like this have been asked but no solution has been given.
Opening of the Facebook page is apparently done by the statement
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {…
Now, Facebook https://developers.facebook.com/docs/facebook-login/ios/v2.3 says:
Check for Existing Tokens
After you add the button, you should update your view controller to check for an existing token at load. This eliminates an unnecessary app switch to Facebook if someone already granted permissions to your app:
- (void)viewDidLoad
{
[super viewDidLoad];
if ([FBSDKAccessToken currentAccessToken]) {
// User is logged in, do work such as go to next view controller.
}
}
But this check is apparently not done in the PFFacebookUtils method.
So, is this a Parse problem (why has it not been solved in the last 2 years?), or is there a way to avoid this annoying roundtrip to the Facebook page?
Any help is appreeciated.
This is the solution that worked for me. Although it still goes to Facebook page if user is logged out and then logged in again...
if let accessToken: FBSDKAccessToken = FBSDKAccessToken.currentAccessToken() {
PFFacebookUtils.logInInBackgroundWithAccessToken(accessToken, block: {
(user: PFUser?, error: NSError?) -> Void in
if user != nil {
println("User logged in through Facebook!")
} else {
println("Uh oh. There was an error logging in.")
}
})
} else {
let permissions = ["public_profile", "email"]
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions, block: {
(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.")
}
})
}

Swift: The supplied Facebook session token is expired or invalid

I am trying to use FacebookLogin with Parse however i am receiving an error that,
The supplied Facebook session token is expired or invalid
every time I try to login using the code below.
import Foundation
import UIKit
class LoginViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let permissions = ["public_profile", "email"]
PFFacebookUtils.logInWithPermissions(permissions) {
(user, error) in
if (user == nil) {
if (error == nil) {
println("User cancelled FB login")
}else{
println("FB login error: \(error)")
}
} else if user.isNew {
println("User signed up and logged in with Facebook")
} else {
println("User logged in via Facebook")
}
}
}
I had this error once and had to put this line before the login call in Objective-C
[FBSession.activeSession closeAndClearTokenInformation];
I can't seem to find the swift equivalent in the Parse SDK, and the Facebook SDK is still only in Objective-C.
You may have to add this line to an Objective-C file, and then call on it from your swift file. Check out this question on how to do that
How to call Objective-C code from Swift