Home>

I send a request to the local server in the main App.vue component, but the request, as I understand it, does not have time to arrive, and even if the server confirms the user's authentication, it still throws it on the login page. The question is, in which hook to check user authentication so that the router has time to redirect?

  • Answer # 1
    //main.js
    //Somewhere in the code you get the user's profile, upload it to the store, etc.
    const fetchUserProfile= ()=> Promise.resolve({ username: 'FooBar' })
    ;(async()=> {
      try {
        const profile= await fetchUserProfile()
      } catch (err) {
        console log(err)
      }
    })()
    //At the moment the profile is loaded
    app.use(router)
    app.mount('#')
    //,
    

    There is another option to look towards hooks on the side of the router https://router.vuejs.org/ru/guide/advanced/navigation-guards.html

  • Answer # 2
    //main.js
    //Somewhere in the code you get the user's profile, upload it to the store, etc.
    const fetchUserProfile= ()=> Promise.resolve({ username: 'FooBar' })
    ;(async()=> {
      try {
        const profile= await fetchUserProfile()
      } catch (err) {
        console log(err)
      }
    })()
    //At the moment the profile is loaded
    app.use(router)
    app.mount('#')
    //,
    

    There is another option to look towards hooks on the side of the router https://router.vuejs.org/ru/guide/advanced/navigation-guards.html