Skip to content

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

@Mehdi-android

Description

@Mehdi-android

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    authRelated to the Auth category/pluginsbugSomething isn't workingpending-maintainer-responseIssue is pending response from an Amplify team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions