我已成功在使用Firebase的应用中实施了Google登录。当我第一次使用谷歌帐户登录时,会出现一个选择谷歌帐户的对话框。
但在我成功登录然后退出后,当我再次尝试登录时,选择谷歌帐户的对话框不再出现,只是直接将我发送到主菜单。
我希望每次用户通过谷歌登录时都会显示该对话框。这是我使用的代码,在我的VC中有谷歌登录按钮
import UIKit
import Firebase
import GoogleSignIn
import SVProgressHUD
class AuthenticationVC : UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {
var userID : String?
override func viewDidLoad() {
super.viewDidLoad()
// delegate declaration
GIDSignIn.sharedInstance().uiDelegate = self
// For Google SignIn Using Firebase
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
}
@IBAction func googleButtonDidPressed(_ sender: Any) {
GIDSignIn.sharedInstance().signIn()
}
}
extension AuthenticationVC {
// MARK: - Firebase Google Sign In Authentication Methods
// The methods below will be triggered if the user want to login via google account
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url,sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: [:])
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return GIDSignIn.sharedInstance().handle(url,sourceApplication: sourceApplication,annotation: annotation)
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
SVProgressHUD.show(withStatus: "Harap Tunggu")
if let error = error {
SVProgressHUD.dismiss()
print("failed to login into google")
print("\(error.localizedDescription)")
return
}
print("user successfully signin into google")
guard let authentication = user.authentication else {
SVProgressHUD.dismiss()
return
}
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
Auth.auth().signIn(with: credential) { (user, error) in
if let error = error {
SVProgressHUD.dismiss()
print("failed to create firebase user using google account")
print("\(error.localizedDescription)")
return
}
print("user successfully logged into firebase")
guard let userKMFromGoogleSignIn = user else {
SVProgressHUD.dismiss()
return
}
self.userID = userKMFromGoogleSignIn.uid
// if the user sign Up for the very first time using google account, then user Basic Information stored in the firestore will be nil, therefore we create user basic data to firestore
// if user has signed up before via google account, then directly send to mainTabBar (it means he/she has picked a city before), otherwise pickCity first
self.checkUserBasicInformationInFirestore(userKMFromGoogleSignIn)
}
}
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
// Perform any operations when the user disconnects from app here.
// ...
}
}
以下是用户点击退出按钮时的代码
@IBAction func logOutButtonDidPressed(_ sender: Any) {
do {
try Auth.auth().signOut()
print("User Did Log Out")
performSegue(withIdentifier: "Back", sender: nil)
} catch {
showAlert(alertTitle: "Sorry", alertMessage: "\(error)", actionTitle: "Back")
}
}
我该怎么办?
答案 0 :(得分:4)
尝试使用google登录时输入您的视图控制器。退出将使当前令牌失效,您将被要求再次使用帐户列表登录。
override func viewDidLoad() {
//Configure and set delegate
...
GIDSignIn.sharedInstance().signOut()
GIDSignIn.sharedInstance().disconnect()
}
@IBAction func logOutButtonDidPressed(_ sender: Any) {
GIDSignIn.sharedInstance().signOut()
GIDSignIn.sharedInstance().disconnect()
}
答案 1 :(得分:0)
点击Google登录按钮时,我们应该删除之前的共享实例。
@IBAction func googleLoginAction(_ sender: Any) {
signOut()
let sighIn:GIDSignIn = GIDSignIn.sharedInstance()
sighIn.delegate = self;
sighIn.uiDelegate = self;
sighIn.shouldFetchBasicProfile = true
sighIn.scopes = ["https://www.googleapis.com/auth/plus.login","https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/userinfo.profile","https://www.googleapis.com/auth/plus.me"];
sighIn.clientID = ""
sighIn.signIn()
}
func signOut() -> Void {
GIDSignIn.sharedInstance().signOut()
}
func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {
}
func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!) {
self.present(viewController, animated: true, completion: nil)
}
func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
self.dismiss(animated: true, completion: nil)
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if (error != nil) {
return
}
reportAuthStatus()
socialId = user.userID
socialMail = user.profile.email
socialFirstName = user.profile.givenName
socialLastName = user.profile.familyName
socialLoginFunction(socialName: "google", socialID: self.socialId)
}
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
if (error != nil) {
} else {
}
}