Всем привет
Я пытаюсь внедрить активный каталог Azure B2c в свое приложение nuxt 3. Поскольку @nuxtjs/auth-next еще не работает для nuxt 3, я пытаюсь сделать свой собственный компонуемый, который использует пакет npm @azure/msal-browser.
Причина, по которой я пишу эту статью, заключается в том, что она не работает. Код, который я создал, можно увидеть ниже:
Терминал
[nitro] [dev] [unhandledRejection] BrowserAuthError: non_browser_environment: запросы на вход и токены не поддерживаются в средах без браузера. 21:07:32
at BrowserAuthError.AuthError [как конструктор]
Браузерная консоль
PerformanceClient.ts:100 Uncaught (в обещании) TypeError: this.startPerformanceMeasurement не является функцией
в PerformanceClient2.startMeasurement
файл: /composables/useAuth.js
import * as msal from '@azure/msal-browser'
let state = {
applicationInstance: null,
}
export const useAuth = () => {
//config auth
const msalConfig = {
auth: {
clientId: '',
authority: '',
knownAuthorities: [``],
redirectUri: '',
knownAuthorities: ['']
},
cache: {
cacheLocation: "sessionStorage", // This configures where your cache will be stored
storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
},
}
state.applicationInstance = new msal.PublicClientApplication(msalConfig);
return {
signIn
}
}
const signIn = () => {
//handle redirect
state.applicationInstance
.addEventCallback(event => {
if (event.type == "msal:loginSuccess" && event.payload.account)
{
const account = event.payload.account
state.applicationInstance.setActiveAccount(account)
console.info(account)
}
})
//handle auth redirect
state.applicationInstance
.handleRedirectPromise()
.then(() => {
const account = state.applicationInstance.getActiveAccount()
if (!account) {
const requestParams = {
scopes: ['openid', 'offline_access', 'User.Read'],
}
state.applicationInstance.loginRedirect(requestParams)
}
})
}
файл: index.vue
<script setup>
const auth = useAuth();
auth.signIn()
</script>





Вам нужно убедиться, что вы пытаетесь войти в систему только в браузере, потому что Nuxt также работает на стороне сервера.
Вы можете проверить, являетесь ли вы клиентом, с помощью process.client или process.server для серверной части.
<script setup>
if (process.client) {
const auth = useAuth();
auth.signIn() // Try to sign in but only on client.
}
</script>
NuxtJS/VueJS: как узнать, отображалась ли страница только на стороне клиента?
Привет, спасибо за ваш ответ, и он действительно помог с моей первой ошибкой, он все еще дает мне ошибку консоли браузера:
PerformanceClient.ts:100 Uncaught (in promise) TypeError: this.startPerformanceMeasurement is not a function at PerformanceClient2.startMeasurementна msal.loginRedirect. Любые идеи?