iOS GoogleSignIn is returning an expired id token - ios

I am trying to retrieve from Google an id token to send to my backend server.
The problem is that the id token that I am receiving is already expired, so it is not valid to my server.
// MARK: - Google auth
extension LoginView: GIDSignInDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
// Check for sign in error
if let error = error {
if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
print("The user has not signed in before or they have since signed out.")
} else {
print("\(error.localizedDescription)")
}
return
}
guard let u = GIDSignIn.sharedInstance()?.currentUser, let email = u.profile.email, let eDate = u.authentication.idTokenExpirationDate, let idToken = u.authentication.idToken else { return }
print(">> ", eDate)
self.register(email: email, token: idToken)
}
}
The date: >> 2020-10-01 08:09:19 +0000(Currently is 09:10 hours)
Thanks

Related

Firebase google signin authentication AppDelegate- Use of unresolved identifier 'isMFAEnabled'

I'm new to iOS development. I am trying to add google sign in to my app but i am facing an some problems.Code shows some "Use of unresolved identifier 'isMFAEnabled"and "Value of type 'AppDelegate' has no member 'showTextInputPrompt'".Please help me.I'm following this doc- https://firebase.google.com/docs/auth/ios/google-signin#swift_9 enter image description here
import UIKit
import Firebase
import GoogleSignIn
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return GIDSignIn.sharedInstance().handle(url)
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
print(error.localizedDescription)
return
}
guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
Auth.auth().signIn(with: credential) { (authResult, error) in
if let error = error {
let authError = error as NSError
if (isMFAEnabled && authError.code == AuthErrorCode.secondFactorRequired.rawValue) {
// The user is a multi-factor user. Second factor challenge is required.
let resolver = authError.userInfo[AuthErrorUserInfoMultiFactorResolverKey] as! MultiFactorResolver
var displayNameString = ""
for tmpFactorInfo in (resolver.hints) {
displayNameString += tmpFactorInfo.displayName ?? ""
displayNameString += " "
}
self.showTextInputPrompt(withMessage: "Select factor to sign in\n\(displayNameString)", completionBlock: { userPressedOK, displayName in
var selectedHint: PhoneMultiFactorInfo?
for tmpFactorInfo in resolver.hints {
if (displayName == tmpFactorInfo.displayName) {
selectedHint = tmpFactorInfo as? PhoneMultiFactorInfo
}
}
PhoneAuthProvider.provider().verifyPhoneNumber(with: selectedHint!, uiDelegate: nil, multiFactorSession: resolver.session) { verificationID, error in
if error != nil {
print("Multi factor start sign in failed. Error: \(error.debugDescription)")
} else {
self.showTextInputPrompt(withMessage: "Verification code for \(selectedHint?.displayName ?? "")", completionBlock: { userPressedOK, verificationCode in
let credential: PhoneAuthCredential? = PhoneAuthProvider.provider().credential(withVerificationID: verificationID!, verificationCode: verificationCode!)
let assertion: MultiFactorAssertion? = PhoneMultiFactorGenerator.assertion(with: credential!)
resolver.resolveSignIn(with: assertion!) { authResult, error in
if error != nil {
print("Multi factor finanlize sign in failed. Error: \(error.debugDescription)")
} else {
self.navigationController?.popViewController(animated: true)
}
}
})
}
}
})
} else {
print(error.localizedDescription)
return
}
// ...
return
}
// User is signed in
// ...
}
}
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
let firebaseAuth = Auth.auth()
do {
try firebaseAuth.signOut()
} catch let signOutError as NSError {
print ("Error signing out: %#", signOutError)
}
So the boilerplate is wayyyy more code than you need. This is my review of your AppDelegate.
didFinishLaunchingWithOptions looks good as-is.
Pro tip: add this line for staying logged in GIDSignIn.sharedInstance()?.restorePreviousSignIn()
open,options looks good as-is.
didSignInFor user is where it gets tangled. Delete the entire function and replace it with the following extension (outside the class' brackets):
(also delete GIDSignInDelegate in the class protocols)
extension AppDelegate: GIDSignInDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
//handle sign-in errors
if let error = error {
if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
print("The user has not signed in before or they have since signed out.")
} else {
print("error signing into Google \(error.localizedDescription)")
}
return
}
// Get credential object using Google ID token and Google access token
guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
// Authenticate with Firebase using the credential object
Auth.auth().signIn(with: credential) { (authResult, error) in
if let error = error {
print("authentication error \(error.localizedDescription)")
}
}
}
}
I have tested this in today and it's currently working (Swift 5/ios 13.6).
TL;DR; you can just delete that variable if you don't want to enable multi-factor authentication.
The Firebase documentation provides the variable as a way for you to enable/disable multi-factor (MF) authentication (i.e. when facebook sends you a text message for you to verify). It's more like they are giving you an incomplete template that doesn't compile unless you declare and set this variable (among other things like implementing showTextInputPrompt).
The code provided in firebase documentation is an example, so don't expect it to work out of the box.

Firebase google auth (swift)

Following the firebase docs i added the authentication with google account and this is part of the code that i have in my app delegate
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
if let error = error {
print("Failed to log into Google: ", error)
return
}
print("Successfully logged into Google", user)
guard let idToken = user.authentication.idToken else { return }
guard let accessToken = user.authentication.accessToken else { return }
let credentials = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)
Auth.auth().signIn(with: credentials, completion: { (user, error) in
if let err = error {
print("Failed to create a Firebase User with Google account: ", err)
return
}
guard let uid = user?.uid else { return }
print("Successfully logged into Firebase with Google", uid)
})
}
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
// Perform any operations when the user disconnects from app here.
// ...
}
but the google button
fileprivate func setupGoogleButtons() {
googleButton.frame = CGRect(x: 189, y: 532, width: 118, height: 41)
view.addSubview(googleButton)
GIDSignIn.sharedInstance().uiDelegate = self
}
is obviously in a viewController, what i would like to do is an automatically self.performSegue(withIdentifier: "goToHome1", sender: self) as soon as the user logs in with his google account, because at the moment after login the user always finds on the same VC. How can i do this?
UPDATE
I solve my problem following this question Google and Facebook Firebase Auth using swift 3
If there is no error then the user signed in successfully so you should segue. The button itself just calls the sign in function. Depending on whether the sign in fails or succeeds you alert or segue.
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
if let error = error {
print("Failed to log into Google: ", error)
return
}
print("Successfully logged into Google", user)
guard let idToken = user.authentication.idToken else { return }
guard let accessToken = user.authentication.accessToken else { return }
let credentials = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)
Auth.auth().signIn(with: credentials, completion: { (user, error) in
if let err = error {
print("Failed to create a Firebase User with Google account: ", err)
return
}
guard let uid = user?.uid else { return }
print("Successfully logged into Firebase with Google", uid)
// segue here
DispatchQueue.main.async {
self.performSegue(withIdentifier: "goToHome1", sender: self)
}
})
}
The Auth class has a function addAuthStateDidChangeListener:, check it here.
It will trigger any time the user changes, particularly when the user logs in, the callback will return a non-nil user. That's the time you want to perform the segue.

Google Sign In Crashes If Declined

I'm using a google sign-in and when the user clicks the button but, does not sign in with google (Clicking the button and then cancelling), the app crashes. I'm not sure why.
Here's the code behind my button:
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let authentication = user.authentication {
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)
Auth.auth().signIn(with: credential, completion: { (user, error) -> Void in
if error != nil {
print("Problem at signing in with google with error : \(String(describing: error))")
} else if error == nil {
print("user successfully signed in through GOOGLE! uid:\(Auth.auth().currentUser!.uid)")
print("signed in")
self.firebaseAuth(credential)
}
})
}
}
The error I get is:
"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"
at this line:
if let authentication = user.authentication {
As a good sign of developer always check for values which you want to use.
like if you want to user user object then check if user != nil don't go with if error == nil.
Here is my code for google sign in with firebase. Hope it'll help you.
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
// ...
if error != nil {
// ...
return
}
guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
Auth.auth().signIn(with: credential) { (user, error) in
if user == nil {
// ...
return
} else {
print(user!.displayName ?? "")
print(user!.email ?? "")
print(user!.phoneNumber ?? "")
// User is signed in
// ...
}
}
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error?) {
if user == nil {
print("Error, user pressed cancel button")
} else {
// Start your code here.......
}
}

Firebase Google Sign in Crash Swift

#IBAction func GoogleLoginDidTapped(_ sender: Any) {
print("Google login completed")
try? GIDSignIn.sharedInstance().signIn()
try? GIDSignIn.sharedInstance().uiDelegate = self
try? GIDSignIn.sharedInstance().delegate = self
}
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
try? auth(authentication: user.authentication)
}
func auth(authentication: GIDAuthentication)
{
let credential = FIRGoogleAuthProvider.credential(withIDToken: authentication.idToken , accessToken: authentication.accessToken)
FIRAuth.auth()?.signIn(with: credential, completion: { (user, error) in
if error != nil{
print(error?.localizedDescription)
}
else
{
print(user?.email)
print(user?.displayName)
x = true
name = (user?.displayName)!
self.movespecial()
}
})
}
I can login, but when the google window opens and if I hit the done the app crashes. Can somebody please help me? Thanks in advance.
I have been working on this app for a while and this is the final step.
The error I get is
fatal error: unexpectedly found nil while unwrapping an Optional value
I found the solution
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if error == nil
{
auth(authentication: user.authentication)
}
else
{
print(error.localizedDescription)
}
}

how do I perform segue after log in with google account?

I've been working on sign in with Google account.
I basically followed the guide from Friebase (https://firebase.google.com/docs/auth/ios/google-signin)
It worked, but I have trouble performing segue to my main page from sign in page
here's the code in my signin View controller :
class SignInController: UIViewController, GIDSignInUIDelegate{
#IBOutlet weak var signInButton: GIDSignInButton!
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().signInSilently()
}
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
if let err = error {
print(error)
}
else {
performSegue(withIdentifier: "GoToMain", sender: self)
}
}
it should work if i put performSegue in the signIn function but it didn't
after log in with google successfully, it goes back to protected (sign in) page
I've been working and trying to shoot this problem for almost 6 hours, still can't figure it out !!!
self learning is a lot of pain.
any help is appreciated!!!
Thanks!!
Ian.
update
so now my code is like the following :
class SignInController: UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {
#IBOutlet weak var signInButton: GIDSignInButton!
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().signInSilently()
GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()?.options.clientID
}
public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let authentication = user.authentication
{
let credential = FIRGoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)
FIRAuth.auth()?.signIn(with: credential, completion: { (user, error) -> Void in
if error != nil
{
print("Problem at signing in with google with error : \(error)")
}
else if error == nil{
print("user successfully signed in through GOOGLE! uid:\(FIRAuth.auth()!.currentUser!.uid)")
}
})
}
}
func signIn(signIn: GIDSignIn!, didDisconnectWithUser user: GIDGoogleUser!, withError error: NSError!) {
}
I added GIDsignDeligate protocol otherwise there will be an error at line
"GIDSignIn.sharedInstance().delegate = self"
When I run it, it crashed with "unexpectedly found nil while unwrapping an Optional value error" (seem to happen at "if let authentication = user.authentication"
)
Am i missing anything?
I feel I'm almost there!
Just add GIDSignIn.sharedInstance().delegate = self to your viewDidLoad function:-
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().signInSilently()
GIDSignIn.sharedInstance().clientID = FIRApp.defaultApp()!.options.clientID
GIDSignIn.sharedInstance().delegate = self
}
And change your sign in user function to:-
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
if let authentication = user.authentication
{
let credential = FIRGoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)
FIRAuth.auth()?.signIn(with: credential, completion: { (user, error) -> Void in
if error != nil
{
print("Problem at signing in with google with error : \(error)")
}
else if error == nil{
print("user successfully signed in through GOOGLE! uid:\(FIRAuth.auth()!.currentUser!.uid)")
}
})
}
}
PS:- This is swift3 version, so don't just copy paste it. Try writing it and autoComplete will help.

Resources