vue-adal
vue-adal copied to clipboard
requireAuthOnInitialize : false doesnt seem to work
Was trying to play around and turn off auth on initialize, so I could set it per route, but setting the flag to false doesnt seem to prevent it still going off to login when going to the site for the first time.
requireAuthOnInitialize : false
is there anything else I need to do?
Can you send code example? Make sure you are not initialize the same route that require auth
This is my main.ts import '@babel/polyfill' import Vue from 'vue' import './plugins/vuetify' import App from './App.vue' import router from './router' import { default as Adal, AxiosAuthHttp } from 'vue-adal' import axios from 'axios'
Vue.config.productionTip = false;
const graphApiBase = https://graph.windows.net
const graphApiResource = '00000002-0000-0000-c000-000000000000'
Vue.use(Adal, { config: { tenant: 'xxx', clientId: 'xxx', redirectUri: 'xxx', cacheLocation: 'localStorage' }, requireAuthOnInitialize: false, router: router })
Vue.use({ install (vue, opts = {}) { // Configures an axios http client with a interceptor to auto-acquire tokens vue.prototype.$graphApi = AxiosAuthHttp.createNewClient({ // Required Params axios: axios, resourceId: graphApiResource, // Resource id to get a token against
// Optional Params
router: router, // Enables a router hook to auto-acquire a token for the specific resource
baseUrl: graphApiBase, // Base url to configure the client with
onTokenSuccess (http, context, token) { // Token success hook
// When an attempt to retrieve a token is successful, this will get called.
// This enables modification of the client after a successful call.
if (context.user) {
// Setup the client to talk with the Microsoft Graph API
http.defaults.baseURL = `${graphApiBase}/${context.user.profile.tid}`
}
},
onTokenFailure (error) { // Token failure hook
// When an attempt to retrieve a token is not successful, this will get called.
console.log(error)
}
})
} })
new Vue({ router, render: h => h(App) }).$mount('#app')
And my my router.ts import Vue from 'vue' import Router from 'vue-router' import Home from './views/Home.vue' import About from './views/About.vue' import Products from './views/products/products' import Products2 from './views/products2/products.vue'
Vue.use(Router)
export default new Router({ mode: 'history', routes: [ { path: '/', name: 'home', component: Home }, { path: '/about', name: 'about', component: About }, { path: '/products', name: 'products', component: Products, meta: { requireAuth: true } }, { path: '/products2', name: 'products2', component: Products2 } ] })
Ill go back through your demo setup again incase i missed anything...
I've noticed this behaviour too. It seems to be when you configure an axios client interceptor in the way demonstrated in the sample you will be redirected to login immediately even if requireAuthOnInitialize is set to false. Is there an alternative way of setting up the interceptor after the user has manually logged in?
I believe this behavior is due to caching. Have you tried with a clear browser cache?
I don't think this is caching, as I'm testing from a fresh Chrome incognito window. I'm seeing the same behavior. I want to make one of my routes not require a login.
Do yourself a favor, move to msal. adal is a nightmare...
On Thu, Mar 7, 2019, 13:09 dan-sls [email protected] wrote:
I don't think this is caching, as I'm testing from a fresh Chrome incognito window. I'm seeing the same behavior. I want to make one of my routes not require a login.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/survirtual/vue-adal/issues/14#issuecomment-470485523, or mute the thread https://github.com/notifications/unsubscribe-auth/AGFipr-6YFvMBM_DrxTIIMhCRIqMz25eks5vUPN5gaJpZM4VmZOf .
I am also experiencing this issue. Caching does not seem to be the problem.
I think that this behavior is because of calling some function which requires Auth. Specifically in sample/example code: In Home.vue, there is: let userInfo = await this.getUserInfo() Calling this API requires Auth. So the page is implicitly redirected to login page despite the requireAuthOnInitialize set to false. Just remove the line mentioned above and page won't be redirected.
What I did: isAnonymousRoute is a function to validate if the current route requires Auth, ((you can made your own logic here))
for example:
path: '/about', name: 'About', component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'), meta: { requireAuth: false }
then in the vue-adal config:
` import { AuthenticationContext } from 'vue-adal' // This will be populated with a valid context after initialization
AuthenticationContext.acquireToken(resource, (err, token) => {
if (err) {
let errCode = err.split(':')[0]
switch (errCode) {
case 'AADSTS50058': // Need to prompt for user sign in
if (!isAnonymosRoute)
AuthenticationContext.login();
break
case 'AADSTS65001': // Token is invalid; grab a new one
AuthenticationContext.acquireTokenRedirect(config.clientid);
break
case 'AADSTS16000': // No Access
default: // Need a pop-up forcing a login
if (!isAnonymosRoute)
AuthenticationContext.login();
break
}
return
}
const headers = {
'Authorization': BEARER ${token}
}
})
`
@AngelReyesEspinal How did you actually wire in the AuthenticationContext.acquireToken in your vue-adal config? Could you share that code?