Swift: The supplied Facebook session token is expired or invalid - swift

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

Related

Microsoft Sign-In Through Firebase

I am very new to app development so please forgive me. I have researched far too long and cannot seem to understand how to code my login button for Microsoft sign-in using Firebase. Here is my code so far:
import UIKit
import FirebaseAuth
class LoginViewController: UIViewController {
var ref : DatabaseAuth!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func logIntapped(_ sender: Any) {
let provider = OAuthProvider(providerID: "microsoft.com")
provider.getCredentialWith(nil) { (credential, error) in
if error != nil {
// Handle error.
print("Failed to retreive credential.")
return
}
if credential != nil {
Auth.auth().signIn(with:completion:)(<#AuthCredential#>) { authResult, error in
if error != nil {
// Handle error.
}
}
} else {
print("Credential is nil.")
}
}
}
}
You need to pass the credential that you get back from Microsoft, into your call to Firebase Authentication. So something like:
Auth.auth().signIn(with: credential) { authResult, error in
...
Also see the Firebase documentation on handling the sign-in flow with the Firebase SDK.

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.

Unable to pass Facebook User properties to Firebase FIRAuth in Swift 3

I'm using the following function to login a User with a Facebook Token and the Firebase FIRAuth function. There is no error and no print of email property. As if the function wouldn't be called.
var login = FBSDKLoginManager()
#IBOutlet weak var fbBtnPressed: UIButton!
#IBAction func fbBtnPressed(_ sender: AnyObject) {
login.logIn(withReadPermissions: ["public_profile", "email", "user_friends"], from: self) { (FBSDKLoginManagerLoginResult, Error) in
if Error != nil {
print("The login with Facebook don't working. Erro: \(Error)")
} else {
let accessToken = FBSDKAccessToken.current().tokenString
print("Login with Facebook ok. \(accessToken)")
let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
FIRAuth.auth()?.signIn(with: credential) { (email, error) in
if (error != nil) {
print("The login in the Firebase broke \(error)")
} else {
print("Login in the Firebase ok \(email)")
}
}
}
}
}
After a lot of research, it seems to be a change in Swift3 in combination with the Keychain swap.
You have to enable the Keychange Sharing in your project under capabilities.

Swift 2.0 working Facebook custom login button

I beg all of thee. I searched everywhere but i can't find working swift 2.0 code for Facebook custom login button with returning Access Token. My code simply don't work. (token is not returning Facebook token)
Considering that you have done all the setup as described by the Facebook developer page, you need to do the following in your login view controller:
// Import the following on top below "import UIKit" in your login view controller
import FBSDKCoreKit
import FBSDKLoginKit
// This is your action code for your custom login button for login using facebook in Swift
#IBAction func fbLoginBtn(sender: AnyObject) {
let permisions = ["public_profile", "email"]
PFFacebookUtils.logInInBackgroundWithReadPermissions(permisions) {
(user: PFUser?, error: NSError?) -> Void in
if let error = error {
print(error)
} else {
if let user = user {
print(user)
// "yourSegue" below will be the segue identifier for your new view.
self.performSegueWithIdentifier("yourSegue", sender: self)
}
}
}
}
In case you have not setup your app at Facebook developer, please following the simple steps as mentioned in Facebook Developer Page.
1 - Add a normal button in your view then add the FB login class "FBSDKLoginButton"
2 - Declare your button in your ViewController
#IBOutlet weak var FBLoginButton: FBSDKLoginButton!
3 - Add in your ViewDidload methode
if (FBSDKAccessToken.currentAccessToken() != nil)
{
// User is already logged in, do work such as go to next view controller.
}
else
{
self.FBLoginButton.delegate = self
FBLoginButton.readPermissions = ["public_profile", "email", "user_friends"]
}
4-Add Delegate Login and logout button funcutions
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
loginButton.readPermissions = ["public_profile", "email", "user_friends"]
let fbAccessToken = FBSDKAccessToken.currentAccessToken().tokenString
FBSDKGraphRequest(graphPath: "me?fields=id,name,email,gender,first_name,last_name,middle_name,birthday&access_token=\(fbAccessToken)", parameters: nil).startWithCompletionHandler({ (connection, result, error) in
if ((error) != nil) {
// Process error
print("Error: \(error)")
} else {
print("fetched user: \(result)")
}
})
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
print("User Logged Out")
}

No FB access token upon initial Parse FB login

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.")
//...
}
}
}
}
})
}