Coder Social home page Coder Social logo

Comments (6)

thisisabhash avatar thisisabhash commented on June 12, 2024

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/

from amplify-swift.

abdurrazax avatar abdurrazax commented on June 12, 2024

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 ?

from amplify-swift.

thisisabhash avatar thisisabhash commented on June 12, 2024

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 {
}

from amplify-swift.

IvanSolaris avatar IvanSolaris commented on June 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

from amplify-swift.

thisisabhash avatar thisisabhash commented on June 12, 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/

from amplify-swift.

github-actions avatar github-actions commented on June 12, 2024

⚠️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.

from amplify-swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.