Nuxt.js 2升级Auth V5后登录回调无法跳转至指定路由
问题描述
- 应用基于Nuxt.js 2.15.6,因刷新令牌问题(用户登录后关闭标签页再进入应用时令牌未刷新,导致服务器错误页),将nuxt auth从V4.9.1升级至V5
- 登录/注册流程采用Azure AD(对Azure相关操作经验有限)
- 升级后使用回调组件跳转至用户指定路由(如
dashboard或surveys),但回调后无法跳转至目标路由 - 已排查确认:Azure AD的令牌端点和授权端点调用无异常,Azure侧重定向URI已配置,回调组件中
authmiddleware设为false
相关配置
nuxt.config.js 中的auth配置
auth: { strategies: { local: false, azureAD: { scheme: 'oauth2', endpoints: { authorization: `https://${process.env.AZURE_B2C_ORG}.b2clogin.com/${process.env.AZURE_B2C_ORG}.onmicrosoft.com/${process.env.SIGNIN_POLICY}/oauth2/v2.0/authorize`, token: `https://${process.env.AZURE_B2C_ORG}.b2clogin.com/${process.env.AZURE_B2C_ORG}.onmicrosoft.com/${process.env.SIGNIN_POLICY}/oauth2/v2.0/token`, userInfo: `${process.env.API_HOST}/api/bff-admin/reports/users`, logout: `https://${process.env.AZURE_B2C_ORG}.b2clogin.com/${process.env.AZURE_B2C_ORG}.onmicrosoft.com/${process.env.SIGNIN_POLICY}/oauth2/v2.0/logout`, }, clientId: `${process.env.CLIENT_ID}`, token: { property: 'access_token', type: 'Bearer', name: 'Authorization', maxAge: 1800, }, refreshToken: { property: 'refresh_token', maxAge: 60 * 60 * 24 * 30, }, user: { property: 'data.user', }, responseType: 'code', grantType: 'authorization_code', accessType: 'offline', codeChallengeMethod: 'S256', scope: ['openid', 'profile', 'email'], autoLogout: true, }, }, redirect: { login: '/', logout: `https://${process.env.AZURE_B2C_ORG}.b2clogin.com/${process.env.AZURE_B2C_ORG}.onmicrosoft.com/oauth2/v2.0/authorize?p=${process.env.SIGNIN_POLICY}&client_id=${process.env.CLIENT_ID}&nonce=defaultNonce&redirect_uri=${process.env.REDIRECT_URI}&scope=openid&response_type=code&prompt=login`, home: '/surveys', callback: '/callback', }, localStorage: false, },
VueX store的index.js
import jwtDecode from 'jwt-decode' export const state = () => ({ accessToken: { emails: [], }, isNotificationSeen: false, loading: false, }) export const mutations = { setLoader(state, bool) { state.loading = bool }, SET_TOKEN(state, token) { state.accessToken = token }, } export const actions = { nuxtServerInit({ commit }, { app }) { console.log(app.$auth.loggedIn) if (app.$auth.loggedIn) { // const token = app.$auth.getToken('azureAD') const token = app.$auth.strategies.azureAD.token.get() console.log('token', token) const accessToken = jwtDecode(token) commit('SET_TOKEN', accessToken) } else { console.log('token', 'no token') } }, }
回调组件代码
<template> <div class="flex flex-col items-center justify-center w-full h-screen"> <h3 class="text-lg text-gray-700 font-semibold mb-4"> Authorization in Progress </h3> <div class="loadingio-spinner-wedges-jg70r74cjmn"> <div class="ldio-bi654gh6r3i"> <div> <div><div></div></div> <div><div></div></div> <div><div></div></div> <div><div></div></div> </div> </div> </div> </div> </template> <script> // import jwtDecode from 'jwt-decode' export default { layout: 'default', auth: false, } </script>
内容的提问来源于stack exchange,提问作者Hashan94
相关产品推荐
相关产品推荐

