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

Amplify.Auth.fetchAuthSession return isSigned = false. #2946

Closed
1 task done
Mehdi-android opened this issue Nov 12, 2024 · 3 comments
Closed
1 task done

Amplify.Auth.fetchAuthSession return isSigned = false. #2946

Mehdi-android opened this issue Nov 12, 2024 · 3 comments
Labels
auth Related to the Auth category/plugins bug Something isn't working pending-maintainer-response Issue is pending response from an Amplify team member

Comments

@Mehdi-android
Copy link

Mehdi-android commented Nov 12, 2024

Before opening, please confirm:

Language and Async Model

Kotlin

Amplify Categories

Authentication

Gradle script dependencies

// Put output below this line
 implementation("com.amazonaws:aws-android-sdk-core:2.30.0")
    implementation("com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.30.0")
    implementation("com.amplifyframework:core:2.24.0")
    implementation("com.amplifyframework:aws-auth-cognito:2.24.0")
    implementation("com.amplifyframework:core-kotlin:2.24.0")

Environment information

# Put output below this line


Please include any relevant guides or documentation you're referencing

No response

Describe the bug

I am using Amplify android for authentication in my application. I signed in using Amplify.Auth.signIn but when I close application and start again and call Amplify.Auth.fetchAuthSession I get below error
AWSCognitoAuthSession(isSignedIn=false, identityIdResult=AuthSessionResult{value=ap-southeast-1:xxxxx-xxxx-xxxxxxxxxxxx, error=null, type=SUCCESS},.
Also I use AWSSdk for awssigner authorization
`class AwsInterceptor(
private val awsCredentials: CognitoCachingCredentialsProvider, // AWS credentials
private val serviceName: String, // Service to sign requests for (e.g., execute-api)
region: String // AWS region (e.g., ap-southeast-1)
) : Interceptor {

private val signer: AWS4Signer = AWS4Signer()

init {
    signer.setServiceName(serviceName)
    signer.setRegionName(region)
}

override fun intercept(chain: Interceptor.Chain): Response {
    val signedRequest = signRequest(chain.request())
    return chain.proceed(signedRequest)
}

@Throws(IOException::class)
private fun signRequest(request: Request): Request {
    val builder = request.newBuilder()
    val awsRequest = DefaultRequest<Any>(serviceName)
    builder.addHeader("x-api-key", "I513UYRoQT4SNGrHDzWFS6wfKw1e9b9J9UaT24vk")
    // Set endpoint and add query params
    val url = setEndpoint(builder, awsRequest, request.url)
    setQueryParams(awsRequest, url)

    // Set HTTP method
    setHttpMethod(awsRequest, request.method)

    // Set body (if any)
    setBody(awsRequest, request.body)

    // Sign the request with AWS4Signer and credentials
    signer.sign(awsRequest, awsCredentials.credentials)

    // Apply signed headers to the request
    applyAwsHeaders(builder, awsRequest.headers)

    return builder.build()
}

private fun setEndpoint(
    builder: Request.Builder,
    awsRequest: DefaultRequest<Any>,
    url: HttpUrl
): HttpUrl {
    val canonicalUrl = ensureTrailingSlash(builder, url)
    awsRequest.endpoint = canonicalUrl.toUri()
    return canonicalUrl
}

private fun setQueryParams(awsRequest: DefaultRequest<Any>, url: HttpUrl) {
    for (paramName in url.queryParameterNames) {
        awsRequest.addParameter(paramName, url.queryParameter(paramName))
    }
}

private fun setHttpMethod(awsRequest: DefaultRequest<Any>, method: String) {
    val methodName = HttpMethodName.valueOf(method)
    awsRequest.httpMethod = methodName
}

@Throws(IOException::class)
private fun setBody(awsRequest: DefaultRequest<Any>, body: RequestBody?) {
    if (body == null) return

    val buffer = Buffer()
    body.writeTo(buffer)
    awsRequest.content = ByteArrayInputStream(buffer.readByteArray())
    awsRequest.addHeader("Content-Length", body.contentLength().toString())
    buffer.close()
}

private fun applyAwsHeaders(builder: Request.Builder, headers: Map<String, String>) {
    for ((key, value) in headers) {
        builder.header(key, value)
    }
}

private fun ensureTrailingSlash(builder: Request.Builder, url: HttpUrl): HttpUrl {
    val lastPathSegment = url.pathSegments.lastOrNull()
    if (!lastPathSegment.isNullOrEmpty()) {
        val updatedUrl = url.newBuilder().addPathSegment("").build()
        builder.url(updatedUrl)
        return updatedUrl
    }
    return url
}

}`

Reproduction steps (if applicable)

No response

Code Snippet

// Put your code below this line.

Log output

// Put your logs below this line


amplifyconfiguration.json

_ {
"UserAgent": "aws-amplify-cli/2.0",
"Version": "1.0",
"auth": {
"plugins": {
"awsCognitoAuthPlugin": {
"UserAgent": "aws-amplify-cli/0.1.0",
"Version": "0.1.0",
"IdentityManager": {
"Default": {}
},
"CredentialsProvider": {
"CognitoIdentity": {
"Default": {
"PoolId": "ap-southeast-1:xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx",
"Region": "ap-southeast-1"
}
}
},
"CognitoUserPool": {
"Default": {
"PoolId": "ap-southeast-1_xxxxxxx",
"AppClientId": "xxxxxxxxxxxx",
"AppClientSecret": "xxxxxxxxxxxxxxxxxxx",
"Region": "ap-southeast-1"
}
},
"Auth": {
"Default": {
"authenticationFlowType": "CUSTOM_AUTH",
"socialProviders": [],
"usernameAttributes": [
"EMAIL"
],
"signupAttributes": [
"EMAIL"
],
"passwordProtectionSettings": {
"passwordPolicyMinLength": 8,
"passwordPolicyCharacters": []
},
"mfaConfiguration": "OPTIONAL",
"mfaTypes": [
"EMAIL"
],
"verificationMechanisms": [
"EMAIL"
]
}
}
}
}
}
}_

GraphQL Schema

// Put your schema below this line

Additional information and screenshots

No response

@github-actions github-actions bot added pending-triage Issue is pending triage pending-maintainer-response Issue is pending response from an Amplify team member labels Nov 12, 2024
@edisooon edisooon added bug Something isn't working auth Related to the Auth category/plugins and removed pending-triage Issue is pending triage pending-maintainer-response Issue is pending response from an Amplify team member labels Nov 13, 2024
@edisooon
Copy link
Member

Hi @Mehdi-android, thank you for submitting the issue! One of our team members would investigate it as soon as possible.
In the meantime, could you share your logs as well as session details before and after relaunching the app? This will help our investigation.

@Mehdi-android
Copy link
Author

Mehdi-android commented Nov 14, 2024

Actually issue was I was using amazonawscore as well as amplify sdk, which was causing the issue. I am only using amplify now which resolved the issue.

@github-actions github-actions bot added the pending-maintainer-response Issue is pending response from an Amplify team member label Nov 14, 2024
Copy link
Contributor

This issue is now closed. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth Related to the Auth category/plugins bug Something isn't working pending-maintainer-response Issue is pending response from an Amplify team member
Projects
None yet
Development

No branches or pull requests

2 participants