question

youssef125-3012 avatar image
0 Votes"
youssef125-3012 asked alfredorevilla-msft commented

Why I'm getting an empty access token from azure b2c using vuejs 3

'm using the last version of MSAL.js (@azure/msal-browser": "^2.23.0"),I can successfully authnenticat but the access token is empty I dont know why. I'm posting the code here I hope anyone could help please ?

export const useAuthUserStore = defineStore("auth_user", {

state: () => ({
msalConfig: {
auth: {
clientId: "xxx",
authority: xxxxxxxx,
knownAuthorities: [ xxxxxxx.onmicrosoft.com],
redirectUri: http://localhost:8080/
},
cache: {
cacheLocation: 'localStorage',
},
},
accessToken: "",

isAuthenticated:false,
}),


get the accesstoken method :

async getAccessToken(){

let request = {
scopes: [ https://graph.microsoft.com/offline_access,https://graph.microsoft.com/openid],
// scopes: ['openid', 'offline_access' ],
extraScopesToConsent:['<tenant>. onmicrosoft.com/api/read']
};
const msalInstance = new PublicClientApplication(
this.authStore.$state.msalConfig
);
try {
let tokenResponse = await msalInstance.acquireTokenSilent({
account: this.account ,
scopes: [ https://graph.microsoft.com/offline_access,https://graph.microsoft.com/openid]

});
console.log('tokenResponse :',tokenResponse)
return tokenResponse
} catch (error) {
console.error( 'Silent token acquisition failed. Using interactive mode',error );
let tokenResponse = await msalInstance.acquireTokenPopup(request);
console.log(`Access token acquired via interactive auth ${tokenResponse.accessToken}`)
}

},

handleResponse(response) {

console.log('handleResponse.......')

let accountId = "";

const loginRequest = {

scopes: [ https://graph.microsoft.com/offline_access,https://graph.microsoft.com/openid],
}

console.log('handleResponse.......',response)

if (response !== null) {
accountId = response.account.homeAccountId;
console.log(accountId)
// Display signed-in user content, call API, etc.

} else {

// In case multiple accounts exist, you can select

const currentAccounts = this.$msalInstance.getAllAccounts();
if (currentAccounts.length === 0) {
// no accounts signed-in, attempt to sign a user in
this.$msalInstance.loginRedirect(loginRequest);
} else if (currentAccounts.length > 1) {
// console.log('handleResponse.......96')

// Add choose account code here

} else if (currentAccounts.length === 1) {

// console.log('handleResponse.......23')

accountId = currentAccounts[0].homeAccountId;

// console.log('handleResponse 111.......',accountId)

}

}

the result : ![192636-image.png][1]

could you help please ? is there any missing code that I have to add ?


[1]: /answers/storage/attachments/192636-image.png
azure-ad-b2cazure-ad-msal
image.png (16.6 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @youssef125-3012, can you share the get and set code for this.account?

0 Votes 0 ·

Hello @youssef125-3012, do you still need help for this issue?

0 Votes 0 ·

0 Answers