Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having issue with refresh token, getting Nil while calling awsmobileclient.default().gettokens in swift #3431

Closed
abdurrazax opened this issue Dec 20, 2023 · 6 comments
Labels
auth Issues related to the Auth category question General question

Comments

@abdurrazax
Copy link

Describe the bug

while calling this method in swift on Xcode "awsmobileclient.default().gettokens" I am getting nil value.
I am calling after the successful login with Amplify.Auth.signIn
after that when I am trying to call any mutation I'm getting Auth error

Steps To Reproduce

Steps to reproduce the behavior:
1. Login with amplify Amplify.Auth.signIn
2. after successfully login call awsmobileclient.default().gettokens
3. in the result I'm getting Nil instead of token

Expected behavior

There should be an token in the return

Amplify Framework Version

2.25.0

Amplify Categories

API, Auth

Dependency manager

Swift PM

Swift version

5.8

CLI version

10.8.1

Xcode version

15.0.1

Relevant log output

<details>
<summary>Log Messages</summary>


Error: notSignedIn(message: "User is not signed in to Cognito User Pool, please sign in to use this API.")
```

Is this a regression?

Yes

Regression additional context

No response

Platforms

iOS

OS Version

iOS 17.0

Device

iPhone 14 Pro Max

Specific to simulators

No response

Additional context

No response

@thisisabhash thisisabhash added the auth Issues related to the Auth category label Dec 21, 2023
@thisisabhash
Copy link
Member

thisisabhash commented Dec 21, 2023

I see you're using Amplify version 2.25.0 - AWSMobileClient is no longer used in Amplify v2.

After calling Amplify.Auth.signIn(), you may fetch the credentials/tokens by following the documentation here:

https://docs.amplify.aws/swift/build-a-backend/auth/accessing-credentials/

@thisisabhash thisisabhash added question General question pending-community-response Issue is pending response from the issue requestor labels Dec 21, 2023
@abdurrazax
Copy link
Author

I see you're using Amplify version 2.25.0 - AWSMobileClient is no longer used in Amplify v2.

After calling Amplify.Auth.signIn(), you may fetch the credentials/tokens by following the documentation here:

https://docs.amplify.aws/swift/build-a-backend/auth/accessing-credentials/

After calling the documentation method I'm unable to call any mutation with this method

appDelegate.appSyncClient?.fetch(query: query, cachePolicy: .fetchIgnoringCacheData) { [weak self] (result, error) in

after getting the token AWSMobileClient is setting the auth token and can we do in the amplify V2 ?

@github-actions github-actions bot removed the pending-community-response Issue is pending response from the issue requestor label Dec 22, 2023
@thisisabhash
Copy link
Member

After calling Amplify.Auth.signIn(), Amplify automatically fetches tokens from Cognito UserPool and Identity Pool and saves it in Keychain.
To get the aws credentials/cognito user tokens, you can call like this:

import AWSPluginsCore

do {
    let session = try await Amplify.Auth.fetchAuthSession()

    // Get user sub or identity id
    if let identityProvider = session as? AuthCognitoIdentityProvider {
        let usersub = try identityProvider.getUserSub().get()
        let identityId = try identityProvider.getIdentityId().get()
        print("User sub - \(usersub) and identity id \(identityId)")
    }

    // Get AWS credentials
    if let awsCredentialsProvider = session as? AuthAWSCredentialsProvider {
        let credentials = try awsCredentialsProvider.getAWSCredentials().get()
        // Do something with the credentials
    }

    // Get cognito user pool token
    if let cognitoTokenProvider = session as? AuthCognitoTokensProvider {
        let tokens = try cognitoTokenProvider.getCognitoTokens().get()
        // Do something with the JWT tokens
    }
} catch let error as AuthError {
    print("Fetch auth session failed with error - \(error)")
} catch {
}

@thisisabhash thisisabhash added the closing soon This issue will be closed in 7 days unless further comments are made. label Jan 4, 2024
@IvanSolaris
Copy link

IvanSolaris commented Jan 12, 2024

I'm facing a similar issue, but in my case, the whole cast is returning nil. When doing:

    // Get cognito user pool token
    if let cognitoTokenProvider = session as? AuthCognitoTokensProvider {
        let tokens = try cognitoTokenProvider.getCognitoTokens().get()
        // Do something with the JWT tokens
    }

the cast on the iflet cognitoTokenProvider is nil so it won't work, but interesting thing is that if i debug it and do it in the debugger the cast works

Captura de pantalla 2024-01-12 a las 10 36 50

@thisisabhash
Copy link
Member

thisisabhash commented Jan 19, 2024

Hi @IvanSolaris ,
If you have enabled guest user in Cognito Identity Pool and no user is signed in, you will be able to access only identityId and AWS credentials. All other session details will give you an error.
https://docs.amplify.aws/swift/build-a-backend/auth/accessing-credentials/

@github-actions github-actions bot removed the closing soon This issue will be closed in 7 days unless further comments are made. label Jan 19, 2024
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth Issues related to the Auth category question General question
Projects
None yet
Development

No branches or pull requests

3 participants