Before calling the Keying APIs, check whether Keyring is supported by your browser. The sample code is as follows:
if (navigator.sharedCredentials) // Check whether Keyring is supported by your browser.
{
navigator.sharedCredentials.isSupported().then((supported)=>{
// Check whether Keyring is supported by HMS Core.
console.info("SharedCredentials isSupported : " + supported);
}, (err) => {
console.error("SharedCredentials Error: ", err);
});
}
else
{
console.info("SharedCredentials is not supported on HuaweiBrowser");
} Your web app stores a signed-in user's credentials in Keyring for automatic sign-in later. You can also share the credentials to your other Android apps, quick apps, and web apps. When the user opens the associated apps, the app will sign them in seamlessly. To share the credentials to your Android apps and quick apps, you need to know the package names and certificate fingerprints of the apps. For details about how to generate a certificate fingerprint, please refer to the related development guide. The following sample code saves the credential to Keyring:
var android = new AndroidAppIdentity({name:"your android app name", packageName:"your android app package name",certHash:"0E:01:BA:63….4F"});
var quick = new QuickAppIdentity({name:"your quick app name",packageName:"your quick app package name", certHash: "E3:04:02:C3….1E"});
var web = new WebAppIdentity({name:"your web app name", domain:"domain:port"});
var trustedApps = [android,web,quick];
var cre = new SharedCredential({
username: 'John',
type: SharedCredential.PASSWORD,
content:(new TextEncoder()).encode("John's password").buffer,
userAuth: true,
displayName: "John's nickname",
avatarUrl: 'xxxx.html',
tag: 'John password',
syncAble: false,
sharedWith: trustedApps
});
navigator.sharedCredentials.save(cre).then(() => {
console.info("SharedCredential saved successfully.");
}, (err) => {
console.error("Error while saving the SharedCredential: ", err);
}); After a user opens your web app, you can query whether a credential is available in Keyring, and notify the user whether they can use the available credential for sign-in. The following sample code queries and uses a credential in Keyring:
var android = new AndroidAppIdentity({name:"your android app name", packageName:"your android app package name",certHash:"0E:01:BA:63….4F"});
var quick = new QuickAppIdentity({name:"your quick app name",packageName:"your quick app package name", certHash: "E3:04:02:C3….1E"});
var web = new WebAppIdentity({name:"your web app name", domain:"domain:port"});
var trustedApps = [android,web,quick];
navigator.sharedCredentials.find(trustedApps).then((credentials)=>{
for (var credential of credentials) {
console.info("username : " + credential.username);
console.info("type : " + credential.type);
console.info("displayName : " + credential.displayName);
credential.getContent().then((content)=>{
// handle content buffer
//ConvertToString(content)
})
}
}) After a user signs out or deregisters, the credentials in Keyring will be automatically deleted. The sample code is as follows:
var android = new AndroidAppIdentity({name:"your android app name", packageName:"your android app package name",certHash:"0E:01:BA:63….4F"});
var quick = new QuickAppIdentity({name:"your quick app name",packageName:"your quick app package name", certHash: "E3:04:02:C3….1E"});
var web = new WebAppIdentity({name:"your web app name", domain:"domain:port"});
var trustedApps = [android,web,quick];
navigator.sharedCredentials.find(trustedApps).then((credentials)=>{
for (var credential of credentials) {
if(credential.username=="your username") {
navigator.sharedCredentials.delete(credential).then(() => {
console.info(credential.username + "deleted.");
}, (err) => {
console.error("Error while deleting the SharedCredential: ", err);
});
}
}
})