iOS Firebase Twitter登录每次都需要许可

时间:2018-09-14 05:23:55

标签: swift firebase-authentication

我能够创建一个帐户并通过Twitter登录;但是,每次尝试登录时,都会将用户带到Twitter网页,在该网页上他们必须使用其凭据进行登录。鉴于每次都会发生这种情况,因此否定了这种社交登录的价值。我的Facebook和Google登录在Firebase上没有出现此问题。以下是用于登录的代码:

private func loginWithTwitter() {

        TWTRTwitter.sharedInstance().logIn { (session, error) in
            guard error == nil && session != nil else {
                self.spinner.dismiss()
                Utilities.displayAlert(title: "Login Error", msg: error?.localizedDescription ?? "Could not login with Twitter at this time.", controller: self)
                return
            }
            let credential = TwitterAuthProvider.credential(withToken: session!.authToken, secret: session!.authTokenSecret)
            Auth.auth().signInAndRetrieveData(with: credential, completion: { (result, error) in
                if let error = error {
                    self.spinner.dismiss()
                    self.present(self.sharedManager.getAlertWith(title: "Login Error", andMsg: error.localizedDescription), animated: true)
                    return
                }

                self.loadUserInfo()
            })
        }
    }

我还应该补充一点,我确实在Twitter开发人员页面上选中了“登录使用”框。

更新:

基于下面的Pratik帖子,这是一个确实有效的修改版本。但是,我不确定这是否是正确的长期答案。例如,我想知道令牌到期。关于是否有比这更好的方法的想法?

 private func loginWithTwitter() {

        let token = TWTRTwitter.sharedInstance().sessionStore.session()?.authToken
        let secret = TWTRTwitter.sharedInstance().sessionStore.session()?.authTokenSecret

        guard token != nil && secret != nil else {
            self.present(self.sharedManager.getAlertWith(title: "Login Error", andMsg: "Could not login with Twitter. Please try again."), animated: true)
            return
        }

        let credential = TwitterAuthProvider.credential(withToken: token!, secret: secret!)
        Auth.auth().signInAndRetrieveData(with: credential, completion: { (result, error) in
            if let error = error {
                self.spinner.dismiss()
                self.present(self.sharedManager.getAlertWith(title: "Login Error", andMsg: error.localizedDescription), animated: true)
                return
            }

            self.loadUserInfo()
        })
    }

0 个答案:

没有答案