Answer the question
In order to leave comments, you need to log in
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
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question