N
N
Nayob2020-09-24 15:52:41
Vue.js
Nayob, 2020-09-24 15:52:41

Where is it customary to place functions for checking the presence of user authorization in the application when it starts, using the example of vue cli?

Actually, at first I thought in the life cycle hooks, but then I realized that at this moment the cookies have not yet been read. And then I can't imagine.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arsync, 2020-09-24
@Arsync

In the navigation hooks of the router (vue-router). Any navigation through the application, including the home page, goes through the router (if one is used). And there is beforeEach((to, from, next) => {}), which can be made asynchronous. This hook is capable of blocking the opening of the landing page. If the route is not authorized, call next with the login page path instead of the requested one. Moreover, since we are talking about authorization, you can add meta: { myFieldName: "admin" } to the path in the router configuration and then understand what role the specified route requires. To do this, in a loop, you need to go through all the parts of to.matched and see if there is one among them that has meta specified with myFieldName filled in.
Under this case, even an official examplethere is, although it is written to check for successful user authentication.

N
Nayob, 2020-09-25
@Nayob

Figured out why checkSession didn't work in the beforeCreate hook. When I screwed Vuex I forgot to specify As a result, I put this option in the config. Thanks to all who responded!
axios.defaults.withCredentials = true;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question