How to check current user session using AWS Amplify?

Viewed 5090

I am following along with AWS Amplify documentation and the example code given to check the current auth session is

 func fetchCurrentAuthSession() {
    _ = Amplify.Auth.fetchAuthSession { (result) in
        switch result {
        case .success(let session):
            print("Is user signed in - \(session.isSignedIn)")
        case .failure(let error):
            print("Fetch session failed with error \(error)")
        }
    }
}

after calling this func in a viewDidLoad I get this error Thread 1: Fatal error: Authentication category is not configured. Call Amplify.configure() before using any methods on the category. So I then changed the code to this

      func fetchCurrentAuthSession() {
            do {
                try Amplify.configure()
            _ = Amplify.Auth.fetchAuthSession { (result) in
                switch result {
                case .success(let session):
                    print("Is user signed in - \(session.isSignedIn)")
                case .failure(let error):
                    print("Fetch session failed with error \(error)")
                }
            }
        }catch{

        }

    }

It runs with no errors but the authSession is not printed. What is the proper way to fix this? here is the link to their docs https://docs.amplify.aws/lib/auth/getting-started/q/platform/ios#check-the-current-auth-session

Here is my awsconfiguration.json

{
    "UserAgent": "aws-amplify/cli",
    "Version": "0.1.0",
    "IdentityManager": {
        "Default": {}
    },
    "CredentialsProvider": {
        "CognitoIdentity": {
            "Default": {
                "PoolId": "removed",
                "Region": "removed"
            }
        }
    },
    "CognitoUserPool": {
        "Default": {
            "PoolId": "removed",
            "AppClientId": "removed",
            "AppClientSecret": "removed",
            "Region": "removed"
        }
    },
    "FacebookSignIn": {
        "AppId": "removed",
        "Permissions": "public_profile"
    },
    "Auth": {
        "Default": {
            "authenticationFlowType": "USER_SRP_AUTH"
        }
    }
}

Here is my amplifyconfiguration.json

{
    "UserAgent": "aws-amplify-cli/2.0",
    "Version": "1.0"
}
3 Answers

This is a common error associated with older versions of the Amplify CLI.

1.Update it in the terminal using npm install -g @aws-amplify/cli .

2.Once that is done remove Amplify Auth from the project directory by amplify remove auth.

3.Enter amplify push to configure changes within your backend.

4.Now you can successfully add auth back to your project amplify add auth

5.Enter amplify push to configure changes within your backend.

6.Now run your project and see what happens ;)

You have to import the Amplify and AmplifyPlugin packages in the AppDelegate.swift in your root folder.

import UIKit
import Amplify
import AmplifyPlugins

Then add the following function

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
     do {
            try Amplify.add(plugin: AWSCognitoAuthPlugin())
            try Amplify.configure()
            print("Amplify configured with auth plugin")
        } catch {
            print("An error occurred setting up Amplify: \(error)")
        }
    return true
}

And most important,

you must check whether the user's email is confirmed ?.

If the email is not confirmed, then isSignedIn will always be false. You can check this for the particular emailId in the userPool

Your modified fetchCurrentAuthSession is hitting your catch block but you aren't doing anything to show the error. If you add something like print("Inside catch: (error)") inside the catch you should see the error which will likely be that you are calling Amplify.configure() again. What is in your awsconfiguration.json and amplifyconfiguration.json files (mask out your poolId, appClientID and region before posting)?

Related