eng.gergely.zsolt
eng.gergely.zsolt

Reputation: 7

Is it possible to share credentials between apps with Credential Manager?

The idea is that I save a username, password credential in app 1.

suspend fun signUp(username: String, password: String): SignUpResult {
    return try {
        credentialManager.createCredential(
            context = context,
            request = CreatePasswordRequest(
                id = username,
                password = password
            )
        )
    
        SignUpResult.Success(username)
    
    } catch (e: Exception) {
        e.printStackTrace()
        SignUpResult.Failure
    }
}

Then somehow get those credentials in app 2 and offer them for the user to use them.

suspend fun signIn(): SignInResult {
    return try {
        val credentialResponse = credentialManager.getCredential(
            context = context,
            request = GetCredentialRequest(
                credentialOptions = listOf(GetPasswordOption())
            )
        )
        
        val credential = credentialResponse.credential as? PasswordCredential
            ?: return SignInResult.Failure
        
        SignInResult.Success(credential.id)
        
    } catch (e: Exception) {
        e.printStackTrace()
        SignInResult.Failure
    }
}

Upvotes: 0

Views: 16

Answers (0)

Related Questions