Hi @Sandip Bhesaniya Thank you for reaching out.
It seems you want to be able to validate if authentication succeeded prior to interacting with objects via asynchronous actions.
However it seems that the ClientSecretCredential class does not throw any exceptions/errors to catch by itself if you just call the constructor, however the class does provide a method called getToken which allows you to call an actual authentication call which throws an AuthenticationRequiredError or CredentialUnavailableError which you can catch.
The Troubleshooting example also follows the same approach of creating the credential and then using the actual object to perform work on in the try-catch block.
I have written a small example program to illustrate the concept for you.
import { ClientSecretCredential } from "@azure/identity";
const clientId = "some"
const clientSecret = "wrong"
const tenantId= "IDs"
const subscriptionId = "even more wrong"
const credential = new ClientSecretCredential(tenantId, clientId, clientSecret)
console.log("Azure credentials and subscription are maybe valid?")
async function validateCredentials() {
try {
console.log(await credential.getToken("https://management.azure.com/.default"))
console.log("Azure credentials and subscription are valid")
} catch(error) {
console.log("Azure credentials and subscription are invalid")
}
}
validateCredentials();
You can see the output of the small test program below.
Please "Accept the answer" if the information helped you. This will help us and others in the community as well.