Swift - AWS Cognito-Facebook Login

回眸只為那壹抹淺笑 提交于 2021-02-08 08:01:27

问题


I want to use Facebook for my login process with Cognito, and I've followed a lot of AWS documentation and look at tutorials and questions in Stackoverflow, but I've not found a solution for my problem.

When the user opens the app, it will check if the user is logged in using IdentityManager. If not, it will open a new view where the user can sign in using Facebook using Facebook SDK. After that, I stored the token with a custom IdentityProvider as the documentation said (credentialsProvider.logins is deprecated). Everything seems to work fine, but every time I reopen the application, my session isn't restored.

I found out that if I use AWSIdentityManager.defaultIdentityManager().resumeSessionWithCompletionHandler(handler)I restored my session, but in case the user isn't logged in, it doesn't show my custom login screen as expected, but a Safari web view to Facebook.

Here is my code:

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let identityManager = AWSIdentityManager.defaultIdentityManager()

    identityManager.resumeSessionWithCompletionHandler({
        (result, error) -> Void in

        if !identityManager.loggedIn {
            let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewControlleripad : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SignIn") as UIViewController
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            self.window?.rootViewController = initialViewControlleripad
            self.window?.makeKeyAndVisible()
        }
    })

    return AWSMobileClient.sharedInstance.didFinishLaunching(application, withOptions: launchOptions)
}

SignInViewController.swift

@IBAction func openFacebookLoginScreen(sender: AnyObject) {
    FBSDKLoginManager().logInWithReadPermissions(FACEBOOK_PERMISSIONS, fromViewController: self, handler: { (result, error) -> Void in

        if error == nil {
            let fbLoginResult : FBSDKLoginManagerLoginResult = result

            if fbLoginResult.isCancelled {
                print("Cancelled")
            }
            else {
                if FBSDKAccessToken.currentAccessToken() != nil {
                    self.signInFacebook(FBSDKAccessToken.currentAccessToken().tokenString)
                    self.dismissSignInView()
                }
            }
        }
    })
}

func signInFacebook(fbToken: String){

    let logins = [AWSIdentityProviderFacebook : fbToken]

    let customProviderManager = CustomIdentityProvider(tokens: logins)

    let credentialsProvider = AWSCognitoCredentialsProvider(
        regionType:.USEast1,
        identityPoolId: COGNITO_IDENTITY_POOL_ID,
        identityProviderManager: customProviderManager)

    let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider: credentialsProvider)
    AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
}

class CustomIdentityProvider: NSObject, AWSIdentityProviderManager {
    var tokens : [NSString : NSString]?

    init(tokens: [NSString : NSString]) {
        self.tokens = tokens
    }

    @objc func logins() -> AWSTask {
       return AWSTask(result: tokens)
    }
}

回答1:


Apparently my problem was that I was calling IdentityManager.loggedIn inside AppDelegate.swift, so I move it to viewDidLoad() on my main view controller.

Also I changed my sign in code to:

@IBAction func openFacebookLoginScreen(_: AnyObject) {
    handleLoginWithSignInProvider(AWSFacebookSignInProvider.sharedInstance())
}

func handleLoginWithSignInProvider(signProvider: AWSSignInProvider){
    AWSIdentityManager.defaultIdentityManager().loginWithSignInProvider(signProvider) { (result, error) in
        if(error == nil){
            let logins = [AWSIdentityProviderFacebook : FBSDKAccessToken.currentAccessToken().tokenString!]

            let customProviderManager = CustomIdentityProvider(tokens: logins)

            let credentialsProvider = AWSCognitoCredentialsProvider(
                regionType:.USEast1,
                identityPoolId: self.COGNITO_IDENTITY_POOL_ID,
                identityProviderManager: customProviderManager)

            let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider: credentialsProvider)
            AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

            self.dismissSignInView()
        }
    }
}


来源:https://stackoverflow.com/questions/39807894/swift-aws-cognito-facebook-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!